PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/2.0.1/wp-includes/gettext.php

#
PHP | 361 lines | 190 code | 33 blank | 138 comment | 53 complexity | fd42f3cf7352f15013075cdfc005ee18 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-2.0
  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. $stream = $this->STREAM->read(4);
  54. if ($this->BYTEORDER == 0) {
  55. // low endian
  56. $unpacked = unpack('V',$stream);
  57. return array_shift($unpacked);
  58. } else {
  59. // big endian
  60. $unpacked = unpack('N',$stream);
  61. return array_shift($unpacked);
  62. }
  63. }
  64. /**
  65. * Reads an array of Integers from the Stream
  66. *
  67. * @param int count How many elements should be read
  68. * @return Array of Integers
  69. */
  70. function readintarray($count) {
  71. if ($this->BYTEORDER == 0) {
  72. // low endian
  73. return unpack('V'.$count, $this->STREAM->read(4 * $count));
  74. } else {
  75. // big endian
  76. return unpack('N'.$count, $this->STREAM->read(4 * $count));
  77. }
  78. }
  79. /**
  80. * Constructor
  81. *
  82. * @param object Reader the StreamReader object
  83. * @param boolean enable_cache Enable or disable caching of strings (default on)
  84. */
  85. function gettext_reader($Reader, $enable_cache = true) {
  86. // If there isn't a StreamReader, turn on short circuit mode.
  87. if (! $Reader) {
  88. $this->short_circuit = true;
  89. return;
  90. }
  91. // Caching can be turned off
  92. $this->enable_cache = $enable_cache;
  93. // $MAGIC1 = (int)0x950412de; //bug in PHP 5
  94. $MAGIC1 = (int) - 1794895138;
  95. // $MAGIC2 = (int)0xde120495; //bug
  96. $MAGIC2 = (int) - 569244523;
  97. $this->STREAM = $Reader;
  98. $magic = $this->readint();
  99. if ($magic == $MAGIC1) {
  100. $this->BYTEORDER = 0;
  101. } elseif ($magic == $MAGIC2) {
  102. $this->BYTEORDER = 1;
  103. } else {
  104. $this->error = 1; // not MO file
  105. return false;
  106. }
  107. // FIXME: Do we care about revision? We should.
  108. $revision = $this->readint();
  109. $this->total = $this->readint();
  110. $this->originals = $this->readint();
  111. $this->translations = $this->readint();
  112. }
  113. /**
  114. * Loads the translation tables from the MO file into the cache
  115. * If caching is enabled, also loads all strings into a cache
  116. * to speed up translation lookups
  117. *
  118. * @access private
  119. */
  120. function load_tables() {
  121. if (is_array($this->cache_translations) &&
  122. is_array($this->table_originals) &&
  123. is_array($this->table_translations))
  124. return;
  125. /* get original and translations tables */
  126. $this->STREAM->seekto($this->originals);
  127. $this->table_originals = $this->readintarray($this->total * 2);
  128. $this->STREAM->seekto($this->translations);
  129. $this->table_translations = $this->readintarray($this->total * 2);
  130. if ($this->enable_cache) {
  131. $this->cache_translations = array ();
  132. /* read all strings in the cache */
  133. for ($i = 0; $i < $this->total; $i++) {
  134. $this->STREAM->seekto($this->table_originals[$i * 2 + 2]);
  135. $original = $this->STREAM->read($this->table_originals[$i * 2 + 1]);
  136. $this->STREAM->seekto($this->table_translations[$i * 2 + 2]);
  137. $translation = $this->STREAM->read($this->table_translations[$i * 2 + 1]);
  138. $this->cache_translations[$original] = $translation;
  139. }
  140. }
  141. }
  142. /**
  143. * Returns a string from the "originals" table
  144. *
  145. * @access private
  146. * @param int num Offset number of original string
  147. * @return string Requested string if found, otherwise ''
  148. */
  149. function get_original_string($num) {
  150. $length = $this->table_originals[$num * 2 + 1];
  151. $offset = $this->table_originals[$num * 2 + 2];
  152. if (! $length)
  153. return '';
  154. $this->STREAM->seekto($offset);
  155. $data = $this->STREAM->read($length);
  156. return (string)$data;
  157. }
  158. /**
  159. * Returns a string from the "translations" table
  160. *
  161. * @access private
  162. * @param int num Offset number of original string
  163. * @return string Requested string if found, otherwise ''
  164. */
  165. function get_translation_string($num) {
  166. $length = $this->table_translations[$num * 2 + 1];
  167. $offset = $this->table_translations[$num * 2 + 2];
  168. if (! $length)
  169. return '';
  170. $this->STREAM->seekto($offset);
  171. $data = $this->STREAM->read($length);
  172. return (string)$data;
  173. }
  174. /**
  175. * Binary search for string
  176. *
  177. * @access private
  178. * @param string string
  179. * @param int start (internally used in recursive function)
  180. * @param int end (internally used in recursive function)
  181. * @return int string number (offset in originals table)
  182. */
  183. function find_string($string, $start = -1, $end = -1) {
  184. if (($start == -1) or ($end == -1)) {
  185. // find_string is called with only one parameter, set start end end
  186. $start = 0;
  187. $end = $this->total;
  188. }
  189. if (abs($start - $end) <= 1) {
  190. // We're done, now we either found the string, or it doesn't exist
  191. $txt = $this->get_original_string($start);
  192. if ($string == $txt)
  193. return $start;
  194. else
  195. return -1;
  196. } else if ($start > $end) {
  197. // start > end -> turn around and start over
  198. return $this->find_string($string, $end, $start);
  199. } else {
  200. // Divide table in two parts
  201. $half = (int)(($start + $end) / 2);
  202. $cmp = strcmp($string, $this->get_original_string($half));
  203. if ($cmp == 0)
  204. // string is exactly in the middle => return it
  205. return $half;
  206. else if ($cmp < 0)
  207. // The string is in the upper half
  208. return $this->find_string($string, $start, $half);
  209. else
  210. // The string is in the lower half
  211. return $this->find_string($string, $half, $end);
  212. }
  213. }
  214. /**
  215. * Translates a string
  216. *
  217. * @access public
  218. * @param string string to be translated
  219. * @return string translated string (or original, if not found)
  220. */
  221. function translate($string) {
  222. if ($this->short_circuit)
  223. return $string;
  224. $this->load_tables();
  225. if ($this->enable_cache) {
  226. // Caching enabled, get translated string from cache
  227. if (array_key_exists($string, $this->cache_translations))
  228. return $this->cache_translations[$string];
  229. else
  230. return $string;
  231. } else {
  232. // Caching not enabled, try to find string
  233. $num = $this->find_string($string);
  234. if ($num == -1)
  235. return $string;
  236. else
  237. return $this->get_translation_string($num);
  238. }
  239. }
  240. /**
  241. * Get possible plural forms from MO header
  242. *
  243. * @access private
  244. * @return string plural form header
  245. */
  246. function get_plural_forms() {
  247. // lets assume message number 0 is header
  248. // this is true, right?
  249. $this->load_tables();
  250. // cache header field for plural forms
  251. if (! is_string($this->pluralheader)) {
  252. if ($this->enable_cache) {
  253. $header = $this->cache_translations[""];
  254. } else {
  255. $header = $this->get_translation_string(0);
  256. }
  257. if (eregi("plural-forms: (.*)\n", $header, $regs))
  258. $expr = $regs[1];
  259. else
  260. $expr = "nplurals=2; plural=n == 1 ? 0 : 1;";
  261. $this->pluralheader = $expr;
  262. }
  263. return $this->pluralheader;
  264. }
  265. /**
  266. * Detects which plural form to take
  267. *
  268. * @access private
  269. * @param n count
  270. * @return int array index of the right plural form
  271. */
  272. function select_string($n) {
  273. $string = $this->get_plural_forms();
  274. $string = str_replace('nplurals',"\$total",$string);
  275. $string = str_replace("n",$n,$string);
  276. $string = str_replace('plural',"\$plural",$string);
  277. $total = 0;
  278. $plural = 0;
  279. eval("$string");
  280. if ($plural >= $total) $plural = 0;
  281. return $plural;
  282. }
  283. /**
  284. * Plural version of gettext
  285. *
  286. * @access public
  287. * @param string single
  288. * @param string plural
  289. * @param string number
  290. * @return translated plural form
  291. */
  292. function ngettext($single, $plural, $number) {
  293. if ($this->short_circuit) {
  294. if ($number != 1)
  295. return $plural;
  296. else
  297. return $single;
  298. }
  299. // find out the appropriate form
  300. $select = $this->select_string($number);
  301. // this should contains all strings separated by NULLs
  302. $key = $single.chr(0).$plural;
  303. if ($this->enable_cache) {
  304. if (! array_key_exists($key, $this->cache_translations)) {
  305. return ($number != 1) ? $plural : $single;
  306. } else {
  307. $result = $this->cache_translations[$key];
  308. $list = explode(chr(0), $result);
  309. return $list[$select];
  310. }
  311. } else {
  312. $num = $this->find_string($key);
  313. if ($num == -1) {
  314. return ($number != 1) ? $plural : $single;
  315. } else {
  316. $result = $this->get_translation_string($num);
  317. $list = explode(chr(0), $result);
  318. return $list[$select];
  319. }
  320. }
  321. }
  322. }
  323. ?>