A tumblelog CMS built on AJAX, PHP and MySQL.

gettext.class.php 11KB

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