gettext.inc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /*
  3. Copyright (c) 2005 Steven Armstrong <sa at c-area dot ch>
  4. Drop in replacement for native gettext.
  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. LC_CTYPE 0
  20. LC_NUMERIC 1
  21. LC_TIME 2
  22. LC_COLLATE 3
  23. LC_MONETARY 4
  24. LC_MESSAGES 5
  25. LC_ALL 6
  26. */
  27. require('streams.php');
  28. require('gettext.php');
  29. // Variables
  30. global $text_domains, $default_domain, $LC_CATEGORIES, $EMULATEGETTEXT, $CURRENTLOCALE;
  31. $text_domains = array();
  32. $default_domain = 'messages';
  33. $LC_CATEGORIES = array('LC_CTYPE', 'LC_NUMERIC', 'LC_TIME', 'LC_COLLATE', 'LC_MONETARY', 'LC_MESSAGES', 'LC_ALL');
  34. $EMULATEGETTEXT = 0;
  35. $CURRENTLOCALE = '';
  36. // Utility functions
  37. /**
  38. * Utility function to get a StreamReader for the given text domain.
  39. */
  40. function _get_reader($domain=null, $category=5, $enable_cache=true) {
  41. global $text_domains, $default_domain, $LC_CATEGORIES;
  42. if (!isset($domain)) $domain = $default_domain;
  43. if (!isset($text_domains[$domain]->l10n)) {
  44. // get the current locale
  45. $locale = _setlocale(LC_MESSAGES, 0);
  46. $p = isset($text_domains[$domain]->path) ? $text_domains[$domain]->path : './';
  47. $path = $p . "$locale/". $LC_CATEGORIES[$category] ."/$domain.mo";
  48. if (file_exists($path)) {
  49. $input = new FileReader($path);
  50. }
  51. else {
  52. $input = null;
  53. }
  54. $text_domains[$domain]->l10n = new gettext_reader($input, $enable_cache);
  55. }
  56. return $text_domains[$domain]->l10n;
  57. }
  58. /**
  59. * Returns whether we are using our emulated gettext API or PHP built-in one.
  60. */
  61. function locale_emulation() {
  62. global $EMULATEGETTEXT;
  63. return $EMULATEGETTEXT;
  64. }
  65. /**
  66. * Checks if the current locale is supported on this system.
  67. */
  68. function _check_locale() {
  69. global $EMULATEGETTEXT;
  70. return !$EMULATEGETTEXT;
  71. }
  72. /**
  73. * Get the codeset for the given domain.
  74. */
  75. function _get_codeset($domain=null) {
  76. global $text_domains, $default_domain, $LC_CATEGORIES;
  77. if (!isset($domain)) $domain = $default_domain;
  78. return (isset($text_domains[$domain]->codeset))? $text_domains[$domain]->codeset : ini_get('mbstring.internal_encoding');
  79. }
  80. /**
  81. * Convert the given string to the encoding set by bind_textdomain_codeset.
  82. */
  83. function _encode($text) {
  84. $source_encoding = mb_detect_encoding($text);
  85. $target_encoding = _get_codeset();
  86. if ($source_encoding != $target_encoding) {
  87. return mb_convert_encoding($text, $target_encoding, $source_encoding);
  88. }
  89. else {
  90. return $text;
  91. }
  92. }
  93. // Custom implementation of the standard gettext related functions
  94. /**
  95. * Sets a requested locale, if needed emulates it.
  96. */
  97. function _setlocale($category, $locale) {
  98. global $CURRENTLOCALE, $EMULATEGETTEXT;
  99. if ($locale === 0) { // use === to differentiate between string "0"
  100. if ($CURRENTLOCALE != '')
  101. return $CURRENTLOCALE;
  102. else
  103. // obey LANG variable, maybe extend to support all of LC_* vars
  104. // even if we tried to read locale without setting it first
  105. return _setlocale($category, $CURRENTLOCALE);
  106. } else {
  107. $ret = 0;
  108. if (function_exists('setlocale')) // I don't know if this ever happens ;)
  109. $ret = setlocale($category, $locale);
  110. if ($ret and ($locale == '' or $ret == $locale)) {
  111. $EMULATEGETTEXT = 0;
  112. $CURRENTLOCALE = $ret;
  113. } else {
  114. if ($locale == '') // emulate variable support
  115. $CURRENTLOCALE = getenv('LANG');
  116. else
  117. $CURRENTLOCALE = $locale;
  118. $EMULATEGETTEXT = 1;
  119. }
  120. return $CURRENTLOCALE;
  121. }
  122. }
  123. /**
  124. * Sets the path for a domain.
  125. */
  126. function _bindtextdomain($domain, $path) {
  127. global $text_domains;
  128. // ensure $path ends with a slash
  129. if ($path[strlen($path) - 1] != '/') $path .= '/';
  130. elseif ($path[strlen($path) - 1] != '\\') $path .= '\\';
  131. $text_domains[$domain]->path = $path;
  132. }
  133. /**
  134. * Specify the character encoding in which the messages from the DOMAIN message catalog will be returned.
  135. */
  136. function _bind_textdomain_codeset($domain, $codeset) {
  137. global $text_domains;
  138. $text_domains[$domain]->codeset = $codeset;
  139. }
  140. /**
  141. * Sets the default domain.
  142. */
  143. function _textdomain($domain) {
  144. global $default_domain;
  145. $default_domain = $domain;
  146. }
  147. /**
  148. * Lookup a message in the current domain.
  149. */
  150. function _gettext($msgid) {
  151. $l10n = _get_reader();
  152. //return $l10n->translate($msgid);
  153. return _encode($l10n->translate($msgid));
  154. }
  155. /**
  156. * Alias for gettext.
  157. */
  158. function __($msgid) {
  159. return _gettext($msgid);
  160. }
  161. /**
  162. * Plural version of gettext.
  163. */
  164. function _ngettext($single, $plural, $number) {
  165. $l10n = _get_reader();
  166. //return $l10n->ngettext($single, $plural, $number);
  167. return _encode($l10n->ngettext($single, $plural, $number));
  168. }
  169. /**
  170. * Override the current domain.
  171. */
  172. function _dgettext($domain, $msgid) {
  173. $l10n = _get_reader($domain);
  174. //return $l10n->translate($msgid);
  175. return _encode($l10n->translate($msgid));
  176. }
  177. /**
  178. * Plural version of dgettext.
  179. */
  180. function _dngettext($domain, $single, $plural, $number) {
  181. $l10n = _get_reader($domain);
  182. //return $l10n->ngettext($single, $plural, $number);
  183. return _encode($l10n->ngettext($single, $plural, $number));
  184. }
  185. /**
  186. * Overrides the domain and category for a single lookup.
  187. */
  188. function _dcgettext($domain, $msgid, $category) {
  189. $l10n = _get_reader($domain, $category);
  190. //return $l10n->translate($msgid);
  191. return _encode($l10n->translate($msgid));
  192. }
  193. /**
  194. * Plural version of dcgettext.
  195. */
  196. function _dcngettext($domain, $single, $plural, $number, $category) {
  197. $l10n = _get_reader($domain, $category);
  198. //return $l10n->ngettext($single, $plural, $number);
  199. return _encode($l10n->ngettext($single, $plural, $number));
  200. }
  201. // Wrappers to use if the standard gettext functions are available, but the current locale is not supported by the system.
  202. // Use the standard impl if the current locale is supported, use the custom impl otherwise.
  203. function T_setlocale($category, $locale) {
  204. return _setlocale($category, $locale);
  205. }
  206. function T_bindtextdomain($domain, $path) {
  207. if (_check_locale()) return bindtextdomain($domain, $path);
  208. else return _bindtextdomain($domain, $path);
  209. }
  210. function T_bind_textdomain_codeset($domain, $codeset) {
  211. // bind_textdomain_codeset is available only in PHP 4.2.0+
  212. if (_check_locale() and function_exists('bind_textdomain_codeset')) return bind_textdomain_codeset($domain, $codeset);
  213. else return _bind_textdomain_codeset($domain, $codeset);
  214. }
  215. function T_textdomain($domain) {
  216. if (_check_locale()) return textdomain($domain);
  217. else return _textdomain($domain);
  218. }
  219. function T_gettext($msgid) {
  220. if (_check_locale()) return gettext($msgid);
  221. else return _gettext($msgid);
  222. }
  223. function T_($msgid) {
  224. if (_check_locale()) return _($msgid);
  225. return __($msgid);
  226. }
  227. function T_ngettext($single, $plural, $number) {
  228. if (_check_locale()) return ngettext($single, $plural, $number);
  229. else return _ngettext($single, $plural, $number);
  230. }
  231. function T_dgettext($domain, $msgid) {
  232. if (_check_locale()) return dgettext($domain, $msgid);
  233. else return _dgettext($domain, $msgid);
  234. }
  235. function T_dngettext($domain, $single, $plural, $number) {
  236. if (_check_locale()) return dngettext($domain, $single, $plural, $number);
  237. else return _dngettext($domain, $single, $plural, $number);
  238. }
  239. function T_dcgettext($domain, $msgid, $category) {
  240. if (_check_locale()) return dcgettext($domain, $msgid, $category);
  241. else return _dcgettext($domain, $msgid, $category);
  242. }
  243. function T_dcngettext($domain, $single, $plural, $number, $category) {
  244. if (_check_locale()) return dcngettext($domain, $single, $plural, $number, $category);
  245. else return _dcngettext($domain, $single, $plural, $number, $category);
  246. }
  247. // Wrappers used as a drop in replacement for the standard gettext functions
  248. if (!function_exists('gettext')) {
  249. function bindtextdomain($domain, $path) {
  250. return _bindtextdomain($domain, $path);
  251. }
  252. function bind_textdomain_codeset($domain, $codeset) {
  253. return _bind_textdomain_codeset($domain, $codeset);
  254. }
  255. function textdomain($domain) {
  256. return _textdomain($domain);
  257. }
  258. function gettext($msgid) {
  259. return _gettext($msgid);
  260. }
  261. function _($msgid) {
  262. return __($msgid);
  263. }
  264. function ngettext($single, $plural, $number) {
  265. return _ngettext($single, $plural, $number);
  266. }
  267. function dgettext($domain, $msgid) {
  268. return _dgettext($domain, $msgid);
  269. }
  270. function dngettext($domain, $single, $plural, $number) {
  271. return _dngettext($domain, $single, $plural, $number);
  272. }
  273. function dcgettext($domain, $msgid, $category) {
  274. return _dcgettext($domain, $msgid, $category);
  275. }
  276. function dcngettext($domain, $single, $plural, $number, $category) {
  277. return _dcngettext($domain, $single, $plural, $number, $category);
  278. }
  279. }
  280. ?>