gettext.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?php
  2. /*
  3. Copyright (c) 2005 Steven Armstrong <sa at c-area dot ch>
  4. Copyright (c) 2009 Danilo Segan <danilo@kvota.net>
  5. Drop in replacement for native gettext.
  6. This file is part of PHP-gettext.
  7. PHP-gettext is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. PHP-gettext is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with PHP-gettext; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /*
  20. LC_CTYPE 0
  21. LC_NUMERIC 1
  22. LC_TIME 2
  23. LC_COLLATE 3
  24. LC_MONETARY 4
  25. LC_MESSAGES 5
  26. LC_ALL 6
  27. */
  28. // LC_MESSAGES is not available if php-gettext is not loaded
  29. // while the other constants are already available from session extension.
  30. if (!defined('LC_MESSAGES')) {
  31. define('LC_MESSAGES', 5);
  32. }
  33. require('streams.php');
  34. require('gettext.php');
  35. // Variables
  36. global $text_domains, $default_domain, $LC_CATEGORIES, $EMULATEGETTEXT, $CURRENTLOCALE;
  37. $text_domains = array();
  38. $default_domain = 'messages';
  39. $LC_CATEGORIES = array('LC_CTYPE', 'LC_NUMERIC', 'LC_TIME', 'LC_COLLATE', 'LC_MONETARY', 'LC_MESSAGES', 'LC_ALL');
  40. $EMULATEGETTEXT = 0;
  41. $CURRENTLOCALE = '';
  42. /* Class to hold a single domain included in $text_domains. */
  43. class domain {
  44. var $l10n;
  45. var $path;
  46. var $codeset;
  47. }
  48. // Utility functions
  49. /**
  50. * Return a list of locales to try for any POSIX-style locale specification.
  51. */
  52. function get_list_of_locales($locale) {
  53. /* Figure out all possible locale names and start with the most
  54. * specific ones. I.e. for sr_CS.UTF-8@latin, look through all of
  55. * sr_CS.UTF-8@latin, sr_CS@latin, sr@latin, sr_CS.UTF-8, sr_CS, sr.
  56. */
  57. $locale_names = array();
  58. $lang = NULL;
  59. $country = NULL;
  60. $charset = NULL;
  61. $modifier = NULL;
  62. if ($locale) {
  63. if (preg_match("/^(?P<lang>[a-z]{2,3})" // language code
  64. ."(?:_(?P<country>[A-Z]{2}))?" // country code
  65. ."(?:\.(?P<charset>[-A-Za-z0-9_]+))?" // charset
  66. ."(?:@(?P<modifier>[-A-Za-z0-9_]+))?$/", // @ modifier
  67. $locale, $matches)) {
  68. if (isset($matches["lang"])) $lang = $matches["lang"];
  69. if (isset($matches["country"])) $country = $matches["country"];
  70. if (isset($matches["charset"])) $charset = $matches["charset"];
  71. if (isset($matches["modifier"])) $modifier = $matches["modifier"];
  72. if ($modifier) {
  73. if ($country) {
  74. if ($charset)
  75. array_push($locale_names, "${lang}_$country.$charset@$modifier");
  76. array_push($locale_names, "${lang}_$country@$modifier");
  77. } elseif ($charset)
  78. array_push($locale_names, "${lang}.$charset@$modifier");
  79. array_push($locale_names, "$lang@$modifier");
  80. }
  81. if ($country) {
  82. if ($charset)
  83. array_push($locale_names, "${lang}_$country.$charset");
  84. array_push($locale_names, "${lang}_$country");
  85. } elseif ($charset)
  86. array_push($locale_names, "${lang}.$charset");
  87. array_push($locale_names, $lang);
  88. }
  89. // If the locale name doesn't match POSIX style, just include it as-is.
  90. if (!in_array($locale, $locale_names))
  91. array_push($locale_names, $locale);
  92. }
  93. return $locale_names;
  94. }
  95. /**
  96. * Utility function to get a StreamReader for the given text domain.
  97. */
  98. function _get_reader($domain=null, $category=5, $enable_cache=true) {
  99. global $text_domains, $default_domain, $LC_CATEGORIES;
  100. if (!isset($domain)) $domain = $default_domain;
  101. if (!isset($text_domains[$domain]->l10n)) {
  102. // get the current locale
  103. $locale = _setlocale(LC_MESSAGES, 0);
  104. $bound_path = isset($text_domains[$domain]->path) ?
  105. $text_domains[$domain]->path : './';
  106. $subpath = $LC_CATEGORIES[$category] ."/$domain.mo";
  107. $locale_names = get_list_of_locales($locale);
  108. $input = null;
  109. foreach ($locale_names as $locale) {
  110. $full_path = $bound_path . $locale . "/" . $subpath;
  111. if (file_exists($full_path)) {
  112. $input = new FileReader($full_path);
  113. break;
  114. }
  115. }
  116. if (!array_key_exists($domain, $text_domains)) {
  117. // Initialize an empty domain object.
  118. $text_domains[$domain] = new domain();
  119. }
  120. $text_domains[$domain]->l10n = new gettext_reader($input,
  121. $enable_cache);
  122. }
  123. return $text_domains[$domain]->l10n;
  124. }
  125. /**
  126. * Returns whether we are using our emulated gettext API or PHP built-in one.
  127. */
  128. function locale_emulation() {
  129. global $EMULATEGETTEXT;
  130. return $EMULATEGETTEXT;
  131. }
  132. /**
  133. * Checks if the current locale is supported on this system.
  134. */
  135. function _check_locale_and_function($function=false) {
  136. global $EMULATEGETTEXT;
  137. if ($function and !function_exists($function))
  138. return false;
  139. return !$EMULATEGETTEXT;
  140. }
  141. /**
  142. * Get the codeset for the given domain.
  143. */
  144. function _get_codeset($domain=null) {
  145. global $text_domains, $default_domain, $LC_CATEGORIES;
  146. if (!isset($domain)) $domain = $default_domain;
  147. return (isset($text_domains[$domain]->codeset))? $text_domains[$domain]->codeset : ini_get('mbstring.internal_encoding');
  148. }
  149. /**
  150. * Convert the given string to the encoding set by bind_textdomain_codeset.
  151. */
  152. function _encode($text) {
  153. $target_encoding = _get_codeset();
  154. if ($source_encoding != $target_encoding) {
  155. $text = htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
  156. }
  157. return $text;
  158. }
  159. // Custom implementation of the standard gettext related functions
  160. /**
  161. * Returns passed in $locale, or environment variable $LANG if $locale == ''.
  162. */
  163. function _get_default_locale($locale) {
  164. if ($locale == '') // emulate variable support
  165. return getenv('LANG');
  166. else
  167. return $locale;
  168. }
  169. /**
  170. * Sets a requested locale, if needed emulates it.
  171. */
  172. function _setlocale($category, $locale) {
  173. global $CURRENTLOCALE, $EMULATEGETTEXT;
  174. if ($locale === 0) { // use === to differentiate between string "0"
  175. if ($CURRENTLOCALE != '')
  176. return $CURRENTLOCALE;
  177. else
  178. // obey LANG variable, maybe extend to support all of LC_* vars
  179. // even if we tried to read locale without setting it first
  180. return _setlocale($category, $CURRENTLOCALE);
  181. } else {
  182. if (function_exists('setlocale')) {
  183. $ret = setlocale($category, $locale);
  184. if (($locale == '' and !$ret) or // failed setting it by env
  185. ($locale != '' and $ret != $locale)) { // failed setting it
  186. // Failed setting it according to environment.
  187. $CURRENTLOCALE = _get_default_locale($locale);
  188. $EMULATEGETTEXT = 1;
  189. } else {
  190. $CURRENTLOCALE = $ret;
  191. $EMULATEGETTEXT = 0;
  192. }
  193. } else {
  194. // No function setlocale(), emulate it all.
  195. $CURRENTLOCALE = _get_default_locale($locale);
  196. $EMULATEGETTEXT = 1;
  197. }
  198. // Allow locale to be changed on the go for one translation domain.
  199. global $text_domains, $default_domain;
  200. if (array_key_exists($default_domain, $text_domains)) {
  201. unset($text_domains[$default_domain]->l10n);
  202. }
  203. return $CURRENTLOCALE;
  204. }
  205. }
  206. /**
  207. * Sets the path for a domain.
  208. */
  209. function _bindtextdomain($domain, $path) {
  210. global $text_domains;
  211. // ensure $path ends with a slash ('/' should work for both, but lets still play nice)
  212. if (substr(php_uname(), 0, 7) == "Windows") {
  213. if ($path[strlen($path)-1] != '\\' and $path[strlen($path)-1] != '/')
  214. $path .= '\\';
  215. } else {
  216. if ($path[strlen($path)-1] != '/')
  217. $path .= '/';
  218. }
  219. if (!array_key_exists($domain, $text_domains)) {
  220. // Initialize an empty domain object.
  221. $text_domains[$domain] = new domain();
  222. }
  223. $text_domains[$domain]->path = $path;
  224. }
  225. /**
  226. * Specify the character encoding in which the messages from the DOMAIN message catalog will be returned.
  227. */
  228. function _bind_textdomain_codeset($domain, $codeset) {
  229. global $text_domains;
  230. $text_domains[$domain]->codeset = $codeset;
  231. }
  232. /**
  233. * Sets the default domain.
  234. */
  235. function _textdomain($domain) {
  236. global $default_domain;
  237. $default_domain = $domain;
  238. }
  239. /**
  240. * Lookup a message in the current domain.
  241. */
  242. function _gettext($msgid) {
  243. $l10n = _get_reader();
  244. return _encode($l10n->translate($msgid));
  245. }
  246. /**
  247. * Alias for gettext.
  248. */
  249. function __($msgid) {
  250. return _gettext($msgid);
  251. }
  252. /**
  253. * Plural version of gettext.
  254. */
  255. function _ngettext($singular, $plural, $number) {
  256. $l10n = _get_reader();
  257. return _encode($l10n->ngettext($singular, $plural, $number));
  258. }
  259. /**
  260. * Override the current domain.
  261. */
  262. function _dgettext($domain, $msgid) {
  263. $l10n = _get_reader($domain);
  264. return _encode($l10n->translate($msgid));
  265. }
  266. /**
  267. * Plural version of dgettext.
  268. */
  269. function _dngettext($domain, $singular, $plural, $number) {
  270. $l10n = _get_reader($domain);
  271. return _encode($l10n->ngettext($singular, $plural, $number));
  272. }
  273. /**
  274. * Overrides the domain and category for a single lookup.
  275. */
  276. function _dcgettext($domain, $msgid, $category) {
  277. $l10n = _get_reader($domain, $category);
  278. return _encode($l10n->translate($msgid));
  279. }
  280. /**
  281. * Plural version of dcgettext.
  282. */
  283. function _dcngettext($domain, $singular, $plural, $number, $category) {
  284. $l10n = _get_reader($domain, $category);
  285. return _encode($l10n->ngettext($singular, $plural, $number));
  286. }
  287. /**
  288. * Context version of gettext.
  289. */
  290. function _pgettext($context, $msgid) {
  291. $l10n = _get_reader();
  292. return _encode($l10n->pgettext($context, $msgid));
  293. }
  294. /**
  295. * Override the current domain in a context gettext call.
  296. */
  297. function _dpgettext($domain, $context, $msgid) {
  298. $l10n = _get_reader($domain);
  299. return _encode($l10n->pgettext($context, $msgid));
  300. }
  301. /**
  302. * Overrides the domain and category for a single context-based lookup.
  303. */
  304. function _dcpgettext($domain, $context, $msgid, $category) {
  305. $l10n = _get_reader($domain, $category);
  306. return _encode($l10n->pgettext($context, $msgid));
  307. }
  308. /**
  309. * Context version of ngettext.
  310. */
  311. function _npgettext($context, $singular, $plural) {
  312. $l10n = _get_reader();
  313. return _encode($l10n->npgettext($context, $singular, $plural));
  314. }
  315. /**
  316. * Override the current domain in a context ngettext call.
  317. */
  318. function _dnpgettext($domain, $context, $singular, $plural) {
  319. $l10n = _get_reader($domain);
  320. return _encode($l10n->npgettext($context, $singular, $plural));
  321. }
  322. /**
  323. * Overrides the domain and category for a plural context-based lookup.
  324. */
  325. function _dcnpgettext($domain, $context, $singular, $plural, $category) {
  326. $l10n = _get_reader($domain, $category);
  327. return _encode($l10n->npgettext($context, $singular, $plural));
  328. }
  329. // Wrappers to use if the standard gettext functions are available,
  330. // but the current locale is not supported by the system.
  331. // Use the standard impl if the current locale is supported, use the
  332. // custom impl otherwise.
  333. function T_setlocale($category, $locale) {
  334. return _setlocale($category, $locale);
  335. }
  336. function T_bindtextdomain($domain, $path) {
  337. if (_check_locale_and_function()) return bindtextdomain($domain, $path);
  338. else return _bindtextdomain($domain, $path);
  339. }
  340. function T_bind_textdomain_codeset($domain, $codeset) {
  341. // bind_textdomain_codeset is available only in PHP 4.2.0+
  342. if (_check_locale_and_function('bind_textdomain_codeset'))
  343. return bind_textdomain_codeset($domain, $codeset);
  344. else return _bind_textdomain_codeset($domain, $codeset);
  345. }
  346. function T_textdomain($domain) {
  347. if (_check_locale_and_function()) return textdomain($domain);
  348. else return _textdomain($domain);
  349. }
  350. function T_gettext($msgid) {
  351. if (_check_locale_and_function()) return gettext($msgid);
  352. else return _gettext($msgid);
  353. }
  354. function T_($msgid) {
  355. if (_check_locale_and_function()) return _($msgid);
  356. return __($msgid);
  357. }
  358. function T_ngettext($singular, $plural, $number) {
  359. if (_check_locale_and_function())
  360. return ngettext($singular, $plural, $number);
  361. else return _ngettext($singular, $plural, $number);
  362. }
  363. function T_dgettext($domain, $msgid) {
  364. if (_check_locale_and_function()) return dgettext($domain, $msgid);
  365. else return _dgettext($domain, $msgid);
  366. }
  367. function T_dngettext($domain, $singular, $plural, $number) {
  368. if (_check_locale_and_function())
  369. return dngettext($domain, $singular, $plural, $number);
  370. else return _dngettext($domain, $singular, $plural, $number);
  371. }
  372. function T_dcgettext($domain, $msgid, $category) {
  373. if (_check_locale_and_function())
  374. return dcgettext($domain, $msgid, $category);
  375. else return _dcgettext($domain, $msgid, $category);
  376. }
  377. function T_dcngettext($domain, $singular, $plural, $number, $category) {
  378. if (_check_locale_and_function())
  379. return dcngettext($domain, $singular, $plural, $number, $category);
  380. else return _dcngettext($domain, $singular, $plural, $number, $category);
  381. }
  382. function T_pgettext($context, $msgid) {
  383. if (_check_locale_and_function('pgettext'))
  384. return pgettext($context, $msgid);
  385. else
  386. return _pgettext($context, $msgid);
  387. }
  388. function T_dpgettext($domain, $context, $msgid) {
  389. if (_check_locale_and_function('dpgettext'))
  390. return dpgettext($domain, $context, $msgid);
  391. else
  392. return _dpgettext($domain, $context, $msgid);
  393. }
  394. function T_dcpgettext($domain, $context, $msgid, $category) {
  395. if (_check_locale_and_function('dcpgettext'))
  396. return dcpgettext($domain, $context, $msgid, $category);
  397. else
  398. return _dcpgettext($domain, $context, $msgid, $category);
  399. }
  400. function T_npgettext($context, $singular, $plural, $number) {
  401. if (_check_locale_and_function('npgettext'))
  402. return npgettext($context, $singular, $plural, $number);
  403. else
  404. return _npgettext($context, $singular, $plural, $number);
  405. }
  406. function T_dnpgettext($domain, $context, $singular, $plural, $number) {
  407. if (_check_locale_and_function('dnpgettext'))
  408. return dnpgettext($domain, $context, $singular, $plural, $number);
  409. else
  410. return _dnpgettext($domain, $context, $singular, $plural, $number);
  411. }
  412. function T_dcnpgettext($domain, $context, $singular, $plural,
  413. $number, $category) {
  414. if (_check_locale_and_function('dcnpgettext'))
  415. return dcnpgettext($domain, $context, $singular,
  416. $plural, $number, $category);
  417. else
  418. return _dcnpgettext($domain, $context, $singular,
  419. $plural, $number, $category);
  420. }
  421. // Wrappers used as a drop in replacement for the standard gettext functions
  422. if (!function_exists('gettext')) {
  423. function bindtextdomain($domain, $path) {
  424. return _bindtextdomain($domain, $path);
  425. }
  426. function bind_textdomain_codeset($domain, $codeset) {
  427. return _bind_textdomain_codeset($domain, $codeset);
  428. }
  429. function textdomain($domain) {
  430. return _textdomain($domain);
  431. }
  432. function gettext($msgid) {
  433. return _gettext($msgid);
  434. }
  435. function _($msgid) {
  436. return __($msgid);
  437. }
  438. function ngettext($singular, $plural, $number) {
  439. return _ngettext($singular, $plural, $number);
  440. }
  441. function dgettext($domain, $msgid) {
  442. return _dgettext($domain, $msgid);
  443. }
  444. function dngettext($domain, $singular, $plural, $number) {
  445. return _dngettext($domain, $singular, $plural, $number);
  446. }
  447. function dcgettext($domain, $msgid, $category) {
  448. return _dcgettext($domain, $msgid, $category);
  449. }
  450. function dcngettext($domain, $singular, $plural, $number, $category) {
  451. return _dcngettext($domain, $singular, $plural, $number, $category);
  452. }
  453. function pgettext($context, $msgid) {
  454. return _pgettext($context, $msgid);
  455. }
  456. function npgettext($context, $singular, $plural, $number) {
  457. return _npgettext($context, $singular, $plural, $number);
  458. }
  459. function dpgettext($domain, $context, $msgid) {
  460. return _dpgettext($domain, $context, $msgid);
  461. }
  462. function dnpgettext($domain, $context, $singular, $plural, $number) {
  463. return _dnpgettext($domain, $context, $singular, $plural, $number);
  464. }
  465. function dcpgettext($domain, $context, $msgid, $category) {
  466. return _dcpgettext($domain, $context, $msgid, $category);
  467. }
  468. function dcnpgettext($domain, $context, $singular, $plural,
  469. $number, $category) {
  470. return _dcnpgettext($domain, $context, $singular, $plural,
  471. $number, $category);
  472. }
  473. }
  474. ?>