<?php
{
}
function _p($string)
{
print (isset($
{BABEL
})) ? $
{BABEL
}->translate($string) : $string; }
function _r($string)
{
return (isset($
{BABEL
})) ? $
{BABEL
}->translate($string) : $string; }
class Babel
{
const className = 'Babel';
const classVersion = '1.0.0';
const classRelase = 1140634709;
const classVendor = 'Vertis';
const classVendorURL = 'www.vertis.com.pl';
var $errno = 0;
var $errstr = null;
var $error = false;
var $mo_dir = 'i18n';
private $translations = array(); private $translations_size = 0;
private $mo_file = null;
private $mo_position = 0;
private $mo_length = 0;
private $mo_order = 'V';
private $mo_info = null;
public function mo($filename)
{
$this->errno = 0;
$this->error = false;
$this->mo_position = 0;
$path = $this->mo_dir.DIRECTORY_SEPARATOR.$filename;
{
$this->_err(1, "class5.Babel : File $path not found");
return false;
}
{
$this->_err(2, "class5.Babel : File $path is not readable");
return false;
}
$handle = fopen($path, 'rb'); $this->mo_file = fread($handle, $this->mo_length);
if ($this->_fix_endian())
{
$this->mo_info = unpack($this->mo_order.'4',$this->_read
(16
)); $this->_translate();
return true;
}
$this->_err(3, "class5.Babel : $path is not *.mo file");
return false;
}
public function translate($string)
{
if ($this->translations_size == 0)
{
return $string;
}
{
return $this->translations[$string];
} else
{
return $string;
}
}
private function _translate()
{
$count = $this->mo_info[2] * 2;
$this->_seek($this->mo_info[3]);
$this->mo_originals = unpack($this->mo_order.$count, $this->_read
(4
* $count)); $this->_seek($this->mo_info[4]);
$this->mo_translated = unpack($this->mo_order.$count, $this->_read
(4
* $count)); for ($i = 0; $i < $this->mo_info[2]; $i++)
{
$j = $i*2 + 1;
$this->_seek($this->mo_originals[$j+1]);
$original = $this->_read($this->mo_originals[$j]);
$this->_seek($this->mo_translated[$j+1]);
$translation = $this->_read($this->mo_translated[$j]);
$this->translations[$original] = $translation;
}
$this->translations_size = count($this->translations); }
private function _fix_endian()
{
$val01 = (int)-569244523;
$val02 = (int)-1794895138;
$unpacked = unpack($this->mo_order, $this->_read
(4
)); if ($unpacked[1] == $val01)
{
$this->mo_order = 'N';
return true;
}
if ($unpacked[1] == $val02)
{
$this->mo_order = 'V';
return true;
}
return false;
}
private function _read($bytes)
{
$data = substr($this->mo_file, $this->mo_position, $bytes); $this->mo_position += $bytes;
if ($this->mo_length < $this->mo_position)
{
$this->mo_position = $this->mo_length;
}
return $data;
}
private function _seek($pos)
{
$this->mo_position = $pos;
if ($this->mo_length < $this->mo_position)
{
$this->mo_position = $this->mo_length;
}
return $this->mo_position;
}
private function _err($errno, $errstr)
{
$this->errno = $errno;
$this->errstr = $errstr;
if (BABEL_DEBUG == 1)
{
print 'WARN! '.$errstr.'<br />'; }
}
public function __construct()
{
if (empty($_inited_modules[self::className])) {
$_inited_modules[self::className] = array('version' => self::classVersion, 'relase' => self::classRelase,
'vendor' => self::classVendor,
'vendorURL' => self::classVendorURL,
'copies' => 1);
} else
{
$_inited_modules[self::className]['copies']++;
}
}
}
?>
Klasa PHP5
<?php
require_once('libs/class5.babel.php');
$lang = new Babel;
$lang -> mo_dir = 'i18n';
$lang -> mo('pl.mo');
// $lang -> mo('pl2.mo'); mozna dolaczyc nastepny
print $lang->translate('Welcome on EclipseX').'<br />'; // odwolanie obiektowe
_p('Welcome on EclipseX');
?>
Przyklad zastosowania
Kod
Witamy w EclipseX
Witamy w EclipseX
Please login
Wynik
Pliki mo/po :
http://www.vertis.com.pl/i18n/Klasa do pobrania :
http://www.vertis.com.pl/-------- added
aha, w sposob banalny mozna wygenerowac plik *.mo z pliku php, uzywamy w skrypcie _r() i _p() potem :
xgettext -L php php.php -o lang.po --keyword=_p --keyword=_r
i wuala
Ten post edytował Bastion 27.06.2006, 14:54:34