gettext.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?php
  2. /*
  3. Copyright (c) 2005 Steven Armstrong <sa at c-area dot ch>
  4. Copyright (c) 2009 Danilo Segan <[email protected]>
  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. $source_encoding = mb_detect_encoding($text);
  154. $target_encoding = _get_codeset();
  155. if ($source_encoding != $target_encoding) {
  156. return mb_convert_encoding($text, $target_encoding, $source_encoding);
  157. }
  158. else {
  159. return $text;
  160. }
  161. }
  162. // Custom implementation of the standard gettext related functions
  163. /**
  164. * Returns passed in $locale, or environment variable $LANG if $locale == ''.
  165. */
  166. function _get_default_locale($locale) {
  167. if ($locale == '') // emulate variable support
  168. return getenv('LANG');
  169. else
  170. return $locale;
  171. }
  172. /**
  173. * Sets a requested locale, if needed emulates it.
  174. */
  175. function _setlocale($category, $locale) {
  176. global $CURRENTLOCALE, $EMULATEGETTEXT;
  177. if ($locale === 0) { // use === to differentiate between string "0"
  178. if ($CURRENTLOCALE != '')
  179. return $CURRENTLOCALE;
  180. else
  181. // obey LANG variable, maybe extend to support all of LC_* vars
  182. // even if we tried to read locale without setting it first
  183. return _setlocale($category, $CURRENTLOCALE);
  184. } else {
  185. if (function_exists('setlocale')) {
  186. $ret = setlocale($category, $locale);
  187. if (($locale == '' and !$ret) or // failed setting it by env
  188. ($locale != '' and $ret != $locale)) { // failed setting it
  189. // Failed setting it according to environment.
  190. $CURRENTLOCALE = _get_default_locale($locale);
  191. $EMULATEGETTEXT = 1;
  192. } else {
  193. $CURRENTLOCALE = $ret;
  194. $EMULATEGETTEXT = 0;
  195. }
  196. } else {
  197. // No function setlocale(), emulate it all.
  198. $CURRENTLOCALE = _get_default_locale($locale);
  199. $EMULATEGETTEXT = 1;
  200. }
  201. // Allow locale to be changed on the go for one translation domain.
  202. global $text_domains, $default_domain;
  203. if (array_key_exists($default_domain, $text_domains)) {
  204. unset($text_domains[$default_domain]->l10n);
  205. }
  206. return $CURRENTLOCALE;
  207. }
  208. }
  209. /**
  210. * Sets the path for a domain.
  211. */
  212. function _bindtextdomain($domain, $path) {
  213. global $text_domains;
  214. // ensure $path ends with a slash ('/' should work for both, but lets still play nice)
  215. if (substr(php_uname(), 0, 7) == "Windows") {
  216. if ($path[strlen($path)-1] != '\\' and $path[strlen($path)-1] != '/')
  217. $path .= '\\';
  218. } else {
  219. if ($path[strlen($path)-1] != '/')
  220. $path .= '/';
  221. }
  222. if (!array_key_exists($domain, $text_domains)) {
  223. // Initialize an empty domain object.
  224. $text_domains[$domain] = new domain();
  225. }
  226. $text_domains[$domain]->path = $path;
  227. }
  228. /**
  229. * Specify the character encoding in which the messages from the DOMAIN message catalog will be returned.
  230. */
  231. function _bind_textdomain_codeset($domain, $codeset) {
  232. global $text_domains;
  233. $text_domains[$domain]->codeset = $codeset;
  234. }
  235. /**
  236. * Sets the default domain.
  237. */
  238. function _textdomain($domain) {
  239. global $default_domain;
  240. $default_domain = $domain;
  241. }
  242. /**
  243. * Lookup a message in the current domain.
  244. */
  245. function _gettext($msgid) {
  246. $l10n = _get_reader();
  247. return _encode($l10n->translate($msgid));
  248. }
  249. /**
  250. * Alias for gettext.
  251. */
  252. function __($msgid) {
  253. return _gettext($msgid);
  254. }
  255. /**
  256. * Plural version of gettext.
  257. */
  258. function _ngettext($singular, $plural, $number) {
  259. $l10n = _get_reader();
  260. return _encode($l10n->ngettext($singular, $plural, $number));
  261. }
  262. /**
  263. * Override the current domain.
  264. */
  265. function _dgettext($domain, $msgid) {
  266. $l10n = _get_reader($domain);
  267. return _encode($l10n->translate($msgid));
  268. }
  269. /**
  270. * Plural version of dgettext.
  271. */
  272. function _dngettext($domain, $singular, $plural, $number) {
  273. $l10n = _get_reader($domain);
  274. return _encode($l10n->ngettext($singular, $plural, $number));
  275. }
  276. /**
  277. * Overrides the domain and category for a single lookup.
  278. */
  279. function _dcgettext($domain, $msgid, $category) {
  280. $l10n = _get_reader($domain, $category);
  281. return _encode($l10n->translate($msgid));
  282. }
  283. /**
  284. * Plural version of dcgettext.
  285. */
  286. function _dcngettext($domain, $singular, $plural, $number, $category) {
  287. $l10n = _get_reader($domain, $category);
  288. return _encode($l10n->ngettext($singular, $plural, $number));
  289. }
  290. /**
  291. * Context version of gettext.
  292. */
  293. function _pgettext($context, $msgid) {
  294. $l10n = _get_reader();
  295. return _encode($l10n->pgettext($context, $msgid));
  296. }
  297. /**
  298. * Override the current domain in a context gettext call.
  299. */
  300. function _dpgettext($domain, $context, $msgid) {
  301. $l10n = _get_reader($domain);
  302. return _encode($l10n->pgettext($context, $msgid));
  303. }
  304. /**
  305. * Overrides the domain and category for a single context-based lookup.
  306. */
  307. function _dcpgettext($domain, $context, $msgid, $category) {
  308. $l10n = _get_reader($domain, $category);
  309. return _encode($l10n->pgettext($context, $msgid));
  310. }
  311. /**
  312. * Context version of ngettext.
  313. */
  314. function _npgettext($context, $singular, $plural) {
  315. $l10n = _get_reader();
  316. return _encode($l10n->npgettext($context, $singular, $plural));
  317. }
  318. /**
  319. * Override the current domain in a context ngettext call.
  320. */
  321. function _dnpgettext($domain, $context, $singular, $plural) {
  322. $l10n = _get_reader($domain);
  323. return _encode($l10n->npgettext($context, $singular, $plural));
  324. }
  325. /**
  326. * Overrides the domain and category for a plural context-based lookup.
  327. */
  328. function _dcnpgettext($domain, $context, $singular, $plural, $category) {
  329. $l10n = _get_reader($domain, $category);
  330. return _encode($l10n->npgettext($context, $singular, $plural));
  331. }
  332. // Wrappers to use if the standard gettext functions are available,
  333. // but the current locale is not supported by the system.
  334. // Use the standard impl if the current locale is supported, use the
  335. // custom impl otherwise.
  336. function T_setlocale($category, $locale) {
  337. return _setlocale($category, $locale);
  338. }
  339. function T_bindtextdomain($domain, $path) {
  340. if (_check_locale_and_function()) return bindtextdomain($domain, $path);
  341. else return _bindtextdomain($domain, $path);
  342. }
  343. function T_bind_textdomain_codeset($domain, $codeset) {
  344. // bind_textdomain_codeset is available only in PHP 4.2.0+
  345. if (_check_locale_and_function('bind_textdomain_codeset'))
  346. return bind_textdomain_codeset($domain, $codeset);
  347. else return _bind_textdomain_codeset($domain, $codeset);
  348. }
  349. function T_textdomain($domain) {
  350. if (_check_locale_and_function()) return textdomain($domain);
  351. else return _textdomain($domain);
  352. }
  353. function T_gettext($msgid) {
  354. if (_check_locale_and_function()) return gettext($msgid);
  355. else return _gettext($msgid);
  356. }
  357. function T_($msgid) {
  358. if (_check_locale_and_function()) return _($msgid);
  359. return __($msgid);
  360. }
  361. function T_ngettext($singular, $plural, $number) {
  362. if (_check_locale_and_function())
  363. return ngettext($singular, $plural, $number);
  364. else return _ngettext($singular, $plural, $number);
  365. }
  366. function T_dgettext($domain, $msgid) {
  367. if (_check_locale_and_function()) return dgettext($domain, $msgid);
  368. else return _dgettext($domain, $msgid);
  369. }
  370. function T_dngettext($domain, $singular, $plural, $number) {
  371. if (_check_locale_and_function())
  372. return dngettext($domain, $singular, $plural, $number);
  373. else return _dngettext($domain, $singular, $plural, $number);
  374. }
  375. function T_dcgettext($domain, $msgid, $category) {
  376. if (_check_locale_and_function())
  377. return dcgettext($domain, $msgid, $category);
  378. else return _dcgettext($domain, $msgid, $category);
  379. }
  380. function T_dcngettext($domain, $singular, $plural, $number, $category) {
  381. if (_check_locale_and_function())
  382. return dcngettext($domain, $singular, $plural, $number, $category);
  383. else return _dcngettext($domain, $singular, $plural, $number, $category);
  384. }
  385. function T_pgettext($context, $msgid) {
  386. if (_check_locale_and_function('pgettext'))
  387. return pgettext($context, $msgid);
  388. else
  389. return _pgettext($context, $msgid);
  390. }
  391. function T_dpgettext($domain, $context, $msgid) {
  392. if (_check_locale_and_function('dpgettext'))
  393. return dpgettext($domain, $context, $msgid);
  394. else
  395. return _dpgettext($domain, $context, $msgid);
  396. }
  397. function T_dcpgettext($domain, $context, $msgid, $category) {
  398. if (_check_locale_and_function('dcpgettext'))
  399. return dcpgettext($domain, $context, $msgid, $category);
  400. else
  401. return _dcpgettext($domain, $context, $msgid, $category);
  402. }
  403. function T_npgettext($context, $singular, $plural, $number) {
  404. if (_check_locale_and_function('npgettext'))
  405. return npgettext($context, $singular, $plural, $number);
  406. else
  407. return _npgettext($context, $singular, $plural, $number);
  408. }
  409. function T_dnpgettext($domain, $context, $singular, $plural, $number) {
  410. if (_check_locale_and_function('dnpgettext'))
  411. return dnpgettext($domain, $context, $singular, $plural, $number);
  412. else
  413. return _dnpgettext($domain, $context, $singular, $plural, $number);
  414. }
  415. function T_dcnpgettext($domain, $context, $singular, $plural,
  416. $number, $category) {
  417. if (_check_locale_and_function('dcnpgettext'))
  418. return dcnpgettext($domain, $context, $singular,
  419. $plural, $number, $category);
  420. else
  421. return _dcnpgettext($domain, $context, $singular,
  422. $plural, $number, $category);
  423. }
  424. // Wrappers used as a drop in replacement for the standard gettext functions
  425. if (!function_exists('gettext')) {
  426. function bindtextdomain($domain, $path) {
  427. return _bindtextdomain($domain, $path);
  428. }
  429. function bind_textdomain_codeset($domain, $codeset) {
  430. return _bind_textdomain_codeset($domain, $codeset);
  431. }
  432. function textdomain($domain) {
  433. return _textdomain($domain);
  434. }
  435. function gettext($msgid) {
  436. return _gettext($msgid);
  437. }
  438. function _($msgid) {
  439. return __($msgid);
  440. }
  441. function ngettext($singular, $plural, $number) {
  442. return _ngettext($singular, $plural, $number);
  443. }
  444. function dgettext($domain, $msgid) {
  445. return _dgettext($domain, $msgid);
  446. }
  447. function dngettext($domain, $singular, $plural, $number) {
  448. return _dngettext($domain, $singular, $plural, $number);
  449. }
  450. function dcgettext($domain, $msgid, $category) {
  451. return _dcgettext($domain, $msgid, $category);
  452. }
  453. function dcngettext($domain, $singular, $plural, $number, $category) {
  454. return _dcngettext($domain, $singular, $plural, $number, $category);
  455. }
  456. function pgettext($context, $msgid) {
  457. return _pgettext($context, $msgid);
  458. }
  459. function npgettext($context, $singular, $plural, $number) {
  460. return _npgettext($context, $singular, $plural, $number);
  461. }
  462. function dpgettext($domain, $context, $msgid) {
  463. return _dpgettext($domain, $context, $msgid);
  464. }
  465. function dnpgettext($domain, $context, $singular, $plural, $number) {
  466. return _dnpgettext($domain, $context, $singular, $plural, $number);
  467. }
  468. function dcpgettext($domain, $context, $msgid, $category) {
  469. return _dcpgettext($domain, $context, $msgid, $category);
  470. }
  471. function dcnpgettext($domain, $context, $singular, $plural,
  472. $number, $category) {
  473. return _dcnpgettext($domain, $context, $singular, $plural,
  474. $number, $category);
  475. }
  476. }
  477. ?>