Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Klasa] Babel 1.3.0, i18n z plików *.mo/*.po
Bastion
post
Post #1





Grupa: Zarejestrowani
Postów: 505
Pomógł: 0
Dołączył: 8.01.2005

Ostrzeżenie: (0%)
-----


  1. <?php
  2.  
  3. if (!defined('BABEL_DEBUG'))
  4. {
  5. define('BABEL_DEBUG', 1);
  6. }
  7.  
  8. function _p($string)
  9. {
  10. global ${BABEL};
  11. print (isset(${BABEL})) ? ${BABEL}->translate($string) : $string;
  12. }
  13.  
  14. function _r($string)
  15. {
  16. global ${BABEL};
  17. return (isset(${BABEL})) ? ${BABEL}->translate($string) : $string;
  18. }
  19.  
  20. class Babel
  21. {
  22. const className = 'Babel';
  23. const classVersion = '1.0.0';
  24. const classRelase = 1140634709;
  25. const classVendor = 'Vertis';
  26. const classVendorURL = 'www.vertis.com.pl';
  27.  
  28. var $errno = 0;
  29. var $errstr = null;
  30. var $error = false;
  31. var $mo_dir = 'i18n';
  32.  
  33. private $translations = array();
  34. private $translations_size = 0;
  35.  
  36. private $mo_file = null;
  37. private $mo_position = 0;
  38. private $mo_length = 0;
  39. private $mo_order = 'V';
  40. private $mo_info = null;
  41.  
  42. public function mo($filename)
  43. {
  44. $this->errno = 0;
  45. $this->error = false;
  46. $this->mo_position = 0;
  47.  
  48. $path = $this->mo_dir.DIRECTORY_SEPARATOR.$filename;
  49.  
  50. if (!file_exists($path))
  51. {
  52. $this->_err(1, "class5.Babel : File $path not found");
  53. return false;
  54. }
  55.  
  56. if (!is_readable($path))
  57. {
  58. $this->_err(2, "class5.Babel : File $path is not readable");
  59. return false;
  60. }
  61.  
  62. $this->mo_length = filesize($path);
  63. $handle = fopen($path, 'rb');
  64. $this->mo_file = fread($handle, $this->mo_length);
  65. fclose($handle);
  66.  
  67. if ($this->_fix_endian())
  68. {
  69. $this->mo_info = unpack($this->mo_order.'4',$this->_read(16));
  70. $this->_translate();
  71. return true;
  72. }
  73.  
  74. $this->_err(3, "class5.Babel : $path is not *.mo file");
  75. return false;
  76. }
  77.  
  78. public function translate($string)
  79. {
  80. if ($this->translations_size == 0)
  81. {
  82. return $string;
  83. }
  84.  
  85. if (array_key_exists($string, $this->translations))
  86. {
  87. return $this->translations[$string];
  88. } else
  89. {
  90. return $string;
  91. }
  92. }
  93.  
  94. private function _translate()
  95. {
  96. $count = $this->mo_info[2] * 2;
  97. $this->_seek($this->mo_info[3]);
  98. $this->mo_originals = unpack($this->mo_order.$count, $this->_read(* $count));
  99. $this->_seek($this->mo_info[4]);
  100. $this->mo_translated = unpack($this->mo_order.$count, $this->_read(* $count));
  101. for ($i = 0; $i < $this->mo_info[2]; $i++)
  102. {
  103. $j = $i*+ 1;
  104. $this->_seek($this->mo_originals[$j+1]);
  105. $original = $this->_read($this->mo_originals[$j]);
  106. $this->_seek($this->mo_translated[$j+1]);
  107. $translation = $this->_read($this->mo_translated[$j]);
  108. $this->translations[$original] = $translation;
  109. }
  110. $this->translations_size = count($this->translations);
  111. }
  112.  
  113. private function _fix_endian()
  114. {
  115. $val01 = (int)-569244523;
  116. $val02 = (int)-1794895138;
  117. $unpacked = unpack($this->mo_order, $this->_read(4));
  118. if ($unpacked[1] == $val01)
  119. {
  120. $this->mo_order = 'N';
  121. return true;
  122. }
  123. if ($unpacked[1] == $val02)
  124. {
  125. $this->mo_order = 'V';
  126. return true;
  127. }
  128. return false;
  129. }
  130.  
  131. private function _read($bytes)
  132. {
  133. $data = substr($this->mo_file, $this->mo_position, $bytes);
  134. $this->mo_position += $bytes;
  135. if ($this->mo_length < $this->mo_position)
  136. {
  137. $this->mo_position = $this->mo_length;
  138. }
  139. return $data;
  140. }
  141.  
  142. private function _seek($pos)
  143. {
  144. $this->mo_position = $pos;
  145. if ($this->mo_length < $this->mo_position)
  146. {
  147. $this->mo_position = $this->mo_length;
  148. }
  149. return $this->mo_position;
  150. }
  151.  
  152. private function _err($errno, $errstr)
  153. {
  154. $this->errno = $errno;
  155. $this->errstr = $errstr;
  156. if (BABEL_DEBUG == 1)
  157. {
  158. print 'WARN! '.$errstr.'<br />';
  159. }
  160. }
  161.  
  162. public function __construct()
  163. {
  164. global $_inited_modules;
  165.  
  166. if (empty($_inited_modules[self::className]))
  167. {
  168. $_inited_modules[self::className] = array('version' => self::classVersion,
  169. 'relase' => self::classRelase,
  170. 'vendor' => self::classVendor,
  171. 'vendorURL' => self::classVendorURL,
  172. 'copies' => 1);
  173. } else
  174. {
  175. $_inited_modules[self::className]['copies']++;
  176. }
  177. }
  178. }
  179.  
  180. ?>

Klasa PHP5

  1. <?php
  2.  
  3. require_once('libs/class5.babel.php');
  4.  
  5. $lang = new Babel;
  6. $lang -> mo_dir = 'i18n';
  7. $lang -> mo('pl.mo');
  8. // $lang -> mo('pl2.mo'); mozna dolaczyc nastepny
  9.  
  10. print $lang->translate('Welcome on EclipseX').'<br />'; // odwolanie obiektowe
  11.  
  12. define('BABEL', 'lang');
  13.  
  14. _p('Welcome on EclipseX');
  15. print '<br />';
  16. print _r('Please login')
  17.  
  18. ?>

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 smile.gif

Ten post edytował Bastion 27.06.2006, 14:54:34


--------------------
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 19.08.2025 - 21:52