gettext.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /*
  3. Copyright (c) 2003 Danilo Segan <danilo@kvota.net>.
  4. Copyright (c) 2005 Nico Kaiser <nico@siriux.net>
  5. This file is part of PHP-gettext.
  6. PHP-gettext is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. PHP-gettext is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with PHP-gettext; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /**
  19. * Provides a simple gettext replacement that works independently from
  20. * the system's gettext abilities.
  21. * It can read MO files and use them for translating strings.
  22. * The files are passed to gettext_reader as a Stream (see streams.php)
  23. *
  24. * This version has the ability to cache all strings and translations to
  25. * speed up the string lookup.
  26. * While the cache is enabled by default, it can be switched off with the
  27. * second parameter in the constructor (e.g. whenusing very large MO files
  28. * that you don't want to keep in memory)
  29. */
  30. class gettext_reader {
  31. //public:
  32. var $error = 0; // public variable that holds error code (0 if no error)
  33. //private:
  34. var $BYTEORDER = 0; // 0: low endian, 1: big endian
  35. var $STREAM = NULL;
  36. var $short_circuit = false;
  37. var $enable_cache = false;
  38. var $originals = NULL; // offset of original table
  39. var $translations = NULL; // offset of translation table
  40. var $pluralheader = NULL; // cache header field for plural forms
  41. var $total = 0; // total string count
  42. var $table_originals = NULL; // table for original strings (offsets)
  43. var $table_translations = NULL; // table for translated strings (offsets)
  44. var $cache_translations = NULL; // original -> translation mapping
  45. /* Methods */
  46. /**
  47. * Reads a 32bit Integer from the Stream
  48. *
  49. * @access private
  50. * @return Integer from the Stream
  51. */
  52. function readint() {
  53. if ($this->BYTEORDER == 0) {
  54. // low endian
  55. return array_shift(unpack('V', $this->STREAM->read(4)));
  56. } else {
  57. // big endian
  58. return array_shift(unpack('N', $this->STREAM->read(4)));
  59. }
  60. }
  61. /**
  62. * Reads an array of Integers from the Stream
  63. *
  64. * @param int count How many elements should be read
  65. * @return Array of Integers
  66. */
  67. function readintarray($count) {
  68. if ($this->BYTEORDER == 0) {
  69. // low endian
  70. return unpack('V'.$count, $this->STREAM->read(4 * $count));
  71. } else {
  72. // big endian
  73. return unpack('N'.$count, $this->STREAM->read(4 * $count));
  74. }
  75. }
  76. /**
  77. * Constructor
  78. *
  79. * @param object Reader the StreamReader object
  80. * @param boolean enable_cache Enable or disable caching of strings (default on)
  81. */
  82. function gettext_reader($Reader, $enable_cache = true) {
  83. // If there isn't a StreamReader, turn on short circuit mode.
  84. if (! $Reader || isset($Reader->error) ) {
  85. $this->short_circuit = true;
  86. return;
  87. }
  88. // Caching can be turned off
  89. $this->enable_cache = $enable_cache;
  90. // $MAGIC1 = (int)0x950412de; //bug in PHP 5.0.2, see https://savannah.nongnu.org/bugs/?func=detailitem&item_id=10565
  91. $MAGIC1 = (int) - 1794895138;
  92. // $MAGIC2 = (int)0xde120495; //bug
  93. $MAGIC2 = (int) - 569244523;
  94. $this->STREAM = $Reader;
  95. $magic = $this->readint();
  96. if ($magic == ($MAGIC1 & 0xFFFFFFFF)) { // to make sure it works for 64-bit platforms
  97. $this->BYTEORDER = 0;
  98. } elseif ($magic == ($MAGIC2 & 0xFFFFFFFF)) {
  99. $this->BYTEORDER = 1;
  100. } else {
  101. $this->error = 1; // not MO file
  102. return false;
  103. }
  104. // FIXME: Do we care about revision? We should.
  105. $revision = $this->readint();
  106. $this->total = $this->readint();
  107. $this->originals = $this->readint();
  108. $this->translations = $this->readint();
  109. }
  110. /**
  111. * Loads the translation tables from the MO file into the cache
  112. * If caching is enabled, also loads all strings into a cache
  113. * to speed up translation lookups
  114. *
  115. * @access private
  116. */
  117. function load_tables() {
  118. if (is_array($this->cache_translations) &&
  119. is_array($this->table_originals) &&
  120. is_array($this->table_translations))
  121. return;
  122. /* get original and translations tables */
  123. $this->STREAM->seekto($this->originals);
  124. $this->table_originals = $this->readintarray($this->total * 2);
  125. $this->STREAM->seekto($this->translations);
  126. $this->table_translations = $this->readintarray($this->total * 2);
  127. if ($this->enable_cache) {
  128. $this->cache_translations = array ();
  129. /* read all strings in the cache */
  130. for ($i = 0; $i < $this->total; $i++) {
  131. $this->STREAM->seekto($this->table_originals[$i * 2 + 2]);
  132. $original = $this->STREAM->read($this->table_originals[$i * 2 + 1]);
  133. $this->STREAM->seekto($this->table_translations[$i * 2 + 2]);
  134. $translation = $this->STREAM->read($this->table_translations[$i * 2 + 1]);
  135. $this->cache_translations[$original] = $translation;
  136. }
  137. }
  138. }
  139. /**
  140. * Returns a string from the "originals" table
  141. *
  142. * @access private
  143. * @param int num Offset number of original string
  144. * @return string Requested string if found, otherwise ''
  145. */
  146. function get_original_string($num) {
  147. $length = $this->table_originals[$num * 2 + 1];
  148. $offset = $this->table_originals[$num * 2 + 2];
  149. if (! $length)
  150. return '';
  151. $this->STREAM->seekto($offset);
  152. $data = $this->STREAM->read($length);
  153. return (string)$data;
  154. }
  155. /**
  156. * Returns a string from the "translations" table
  157. *
  158. * @access private
  159. * @param int num Offset number of original string
  160. * @return string Requested string if found, otherwise ''
  161. */
  162. function get_translation_string($num) {
  163. $length = $this->table_translations[$num * 2 + 1];
  164. $offset = $this->table_translations[$num * 2 + 2];
  165. if (! $length)
  166. return '';
  167. $this->STREAM->seekto($offset);
  168. $data = $this->STREAM->read($length);
  169. return (string)$data;
  170. }
  171. /**
  172. * Binary search for string
  173. *
  174. * @access private
  175. * @param string string
  176. * @param int start (internally used in recursive function)
  177. * @param int end (internally used in recursive function)
  178. * @return int string number (offset in originals table)
  179. */
  180. function find_string($string, $start = -1, $end = -1) {
  181. if (($start == -1) or ($end == -1)) {
  182. // find_string is called with only one parameter, set start end end
  183. $start = 0;
  184. $end = $this->total;
  185. }
  186. if (abs($start - $end) <= 1) {
  187. // We're done, now we either found the string, or it doesn't exist
  188. $txt = $this->get_original_string($start);
  189. if ($string == $txt)
  190. return $start;
  191. else
  192. return -1;
  193. } else if ($start > $end) {
  194. // start > end -> turn around and start over
  195. return $this->find_string($string, $end, $start);
  196. } else {
  197. // Divide table in two parts
  198. $half = (int)(($start + $end) / 2);
  199. $cmp = strcmp($string, $this->get_original_string($half));
  200. if ($cmp == 0)
  201. // string is exactly in the middle => return it
  202. return $half;
  203. else if ($cmp < 0)
  204. // The string is in the upper half
  205. return $this->find_string($string, $start, $half);
  206. else
  207. // The string is in the lower half
  208. return $this->find_string($string, $half, $end);
  209. }
  210. }
  211. /**
  212. * Translates a string
  213. *
  214. * @access public
  215. * @param string string to be translated
  216. * @return string translated string (or original, if not found)
  217. */
  218. function translate($string) {
  219. if ($this->short_circuit)
  220. return $string;
  221. $this->load_tables();
  222. if ($this->enable_cache) {
  223. // Caching enabled, get translated string from cache
  224. if (array_key_exists($string, $this->cache_translations))
  225. return $this->cache_translations[$string];
  226. else
  227. return $string;
  228. } else {
  229. // Caching not enabled, try to find string
  230. $num = $this->find_string($string);
  231. if ($num == -1)
  232. return $string;
  233. else
  234. return $this->get_translation_string($num);
  235. }
  236. }
  237. /**
  238. * Get possible plural forms from MO header
  239. *
  240. * @access private
  241. * @return string plural form header
  242. */
  243. function get_plural_forms() {
  244. // lets assume message number 0 is header
  245. // this is true, right?
  246. $this->load_tables();
  247. // cache header field for plural forms
  248. if (! is_string($this->pluralheader)) {
  249. if ($this->enable_cache) {
  250. $header = $this->cache_translations[""];
  251. } else {
  252. $header = $this->get_translation_string(0);
  253. }
  254. if (eregi("plural-forms: ([^\n]*)\n", $header, $regs))
  255. $expr = $regs[1];
  256. else
  257. $expr = "nplurals=2; plural=n == 1 ? 0 : 1;";
  258. $this->pluralheader = $expr;
  259. }
  260. return $this->pluralheader;
  261. }
  262. /**
  263. * Detects which plural form to take
  264. *
  265. * @access private
  266. * @param n count
  267. * @return int array index of the right plural form
  268. */
  269. function select_string($n) {
  270. $string = $this->get_plural_forms();
  271. $string = str_replace('nplurals',"\$total",$string);
  272. $string = str_replace("n",$n,$string);
  273. $string = str_replace('plural',"\$plural",$string);
  274. $total = 0;
  275. $plural = 0;
  276. eval("$string");
  277. if ($plural >= $total) $plural = $total - 1;
  278. return $plural;
  279. }
  280. /**
  281. * Plural version of gettext
  282. *
  283. * @access public
  284. * @param string single
  285. * @param string plural
  286. * @param string number
  287. * @return translated plural form
  288. */
  289. function ngettext($single, $plural, $number) {
  290. if ($this->short_circuit) {
  291. if ($number != 1)
  292. return $plural;
  293. else
  294. return $single;
  295. }
  296. // find out the appropriate form
  297. $select = $this->select_string($number);
  298. // this should contains all strings separated by NULLs
  299. $key = $single.chr(0).$plural;
  300. if ($this->enable_cache) {
  301. if (! array_key_exists($key, $this->cache_translations)) {
  302. return ($number != 1) ? $plural : $single;
  303. } else {
  304. $result = $this->cache_translations[$key];
  305. $list = explode(chr(0), $result);
  306. return $list[$select];
  307. }
  308. } else {
  309. $num = $this->find_string($key);
  310. if ($num == -1) {
  311. return ($number != 1) ? $plural : $single;
  312. } else {
  313. $result = $this->get_translation_string($num);
  314. $list = explode(chr(0), $result);
  315. return $list[$select];
  316. }
  317. }
  318. }
  319. }
  320. ?>