Zamieszczam prostą bardzo uniwersalną klasę (z zastosowaniem do małych projektów) i może komuś się przyda a za dodatkowe sugestie, pomysły lub wytkanie jakiś błędów będę wdzięczny.
Wszystko opiera się na plikach i zczytaniu pliku do tablicy, zapisaniu tłumaczeń wiersz po wierszu a następnie pobraniu wybranego wiersza i wypisaniu go na stronie.
Klasa nadaje się najbardziej na tłumaczenie już gotowego projektu gdyż użycie polega na tłumaczeniu jakiegoś fragmentu tekstu, zapisanie do pliku(ów) przetłumaczenia a następnie wywołanie metody.
<?php
class Language
{
/*******************************
* Author: Rafal @ Pawlukiewicz . com
********************************/
/**
* License
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* If you modify this class, or have any ideas to improve it, please contact me!
* You are allowed to redistribute this class, if you keep my name and my contac
on it.
*
*
**/
public $language; //actual language set
public $folder; // catalog where we storage file
public $myfile; // file with translation
public $default; // file with translation
public $extension; // language file extention
public $error; // if some error appear
public $show_error; // if we want to show error message on website
function __construct($set_default = "en") // default language
{
$this->folder = "include/language/";
$this->extension = ".lang";
$this->default = $set_default;
$this->error = false;
$this->show_error = true; // change it if you do not want show error message
}
function e($text, $n)
{
if($this->language == '')
$this->language = $this->default;
$source = $this->folder . $this->language . $this->extension;
$this->myfile = file($source); else
$this->error = true;
if(($n > 0
) AND
($n <= count($this->myfile)) AND
!($this->error)) {
$text = $this->myfile[intval($n)-1
]; return($text);
}
if($this->error AND $this->show_error)
$this->ShowError();
}
function ShowError()
{
echo "[We have a problem with (" . $this->language . ") translation file]"; $this->show_error = false; // show only one error message per website
}
}
/*
===========
== USING: ==
===========
$lang = new Language("pl"); // inicjacja z domyślnym językiem
$lang->language = $some_session->lang; // ustawienie aktualnego języka pobranego np. z sesji
$lang->language = "pl"; // ustawienie ręczne aktualnego języka
echo $lang->e("arbitrary_text", 1); // wstawienie tekstu gdzie "arbitrary_text" oznacza naszą interpterację/nazwę pola który chcemy wyświetlić a drugi parametr (1) znacza numer wiersza z pliku tłumaczeń który chcemy wstawić.
*/
?>
Ten post edytował rafalp 17.07.2008, 12:44:24