PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/php-gettext/gettext.inc

https://bitbucket.org/fieldsofview/scuttle-fork
PHP | 534 lines | 350 code | 54 blank | 130 comment | 69 complexity | 9b17c70e06048b279ad559b6feff079f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  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. $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. unset($text_domains[$default_domain]->l10n);
  204. return $CURRENTLOCALE;
  205. }
  206. }
  207. /**
  208. * Sets the path for a domain.
  209. */
  210. function _bindtextdomain($domain, $path) {
  211. global $text_domains;
  212. // ensure $path ends with a slash ('/' should work for both, but lets still play nice)
  213. if (substr(php_uname(), 0, 7) == "Windows") {
  214. if ($path[strlen($path)-1] != '\\' and $path[strlen($path)-1] != '/')
  215. $path .= '\\';
  216. } else {
  217. if ($path[strlen($path)-1] != '/')
  218. $path .= '/';
  219. }
  220. if (!array_key_exists($domain, $text_domains)) {
  221. // Initialize an empty domain object.
  222. $text_domains[$domain] = new domain();
  223. }
  224. $text_domains[$domain]->path = $path;
  225. }
  226. /**
  227. * Specify the character encoding in which the messages from the DOMAIN message catalog will be returned.
  228. */
  229. function _bind_textdomain_codeset($domain, $codeset) {
  230. global $text_domains;
  231. $text_domains[$domain]->codeset = $codeset;
  232. }
  233. /**
  234. * Sets the default domain.
  235. */
  236. function _textdomain($domain) {
  237. global $default_domain;
  238. $default_domain = $domain;
  239. }
  240. /**
  241. * Lookup a message in the current domain.
  242. */
  243. function _gettext($msgid) {
  244. $l10n = _get_reader();
  245. return _encode($l10n->translate($msgid));
  246. }
  247. /**
  248. * Alias for gettext.
  249. */
  250. function __($msgid) {
  251. return _gettext($msgid);
  252. }
  253. /**
  254. * Plural version of gettext.
  255. */
  256. function _ngettext($single, $plural, $number) {
  257. $l10n = _get_reader();
  258. return _encode($l10n->ngettext($single, $plural, $number));
  259. }
  260. /**
  261. * Override the current domain.
  262. */
  263. function _dgettext($domain, $msgid) {
  264. $l10n = _get_reader($domain);
  265. return _encode($l10n->translate($msgid));
  266. }
  267. /**
  268. * Plural version of dgettext.
  269. */
  270. function _dngettext($domain, $single, $plural, $number) {
  271. $l10n = _get_reader($domain);
  272. return _encode($l10n->ngettext($single, $plural, $number));
  273. }
  274. /**
  275. * Overrides the domain and category for a single lookup.
  276. */
  277. function _dcgettext($domain, $msgid, $category) {
  278. $l10n = _get_reader($domain, $category);
  279. return _encode($l10n->translate($msgid));
  280. }
  281. /**
  282. * Plural version of dcgettext.
  283. */
  284. function _dcngettext($domain, $single, $plural, $number, $category) {
  285. $l10n = _get_reader($domain, $category);
  286. return _encode($l10n->ngettext($single, $plural, $number));
  287. }
  288. /**
  289. * Context version of gettext.
  290. */
  291. function _pgettext($context, $msgid) {
  292. $l10n = _get_reader();
  293. return _encode($l10n->pgettext($context, $msgid));
  294. }
  295. /**
  296. * Override the current domain in a context gettext call.
  297. */
  298. function _dpgettext($domain, $context, $msgid) {
  299. $l10n = _get_reader($domain);
  300. return _encode($l10n->pgettext($context, $msgid));
  301. }
  302. /**
  303. * Overrides the domain and category for a single context-based lookup.
  304. */
  305. function _dcpgettext($domain, $context, $msgid, $category) {
  306. $l10n = _get_reader($domain, $category);
  307. return _encode($l10n->pgettext($context, $msgid));
  308. }
  309. /**
  310. * Context version of ngettext.
  311. */
  312. function _npgettext($context, $singular, $plural) {
  313. $l10n = _get_reader();
  314. return _encode($l10n->npgettext($context, $singular, $plural));
  315. }
  316. /**
  317. * Override the current domain in a context ngettext call.
  318. */
  319. function _dnpgettext($domain, $context, $singular, $plural) {
  320. $l10n = _get_reader($domain);
  321. return _encode($l10n->npgettext($context, $singular, $plural));
  322. }
  323. /**
  324. * Overrides the domain and category for a plural context-based lookup.
  325. */
  326. function _dcnpgettext($domain, $context, $singular, $plural, $category) {
  327. $l10n = _get_reader($domain, $category);
  328. return _encode($l10n->npgettext($context, $singular, $plural));
  329. }
  330. // Wrappers to use if the standard gettext functions are available,
  331. // but the current locale is not supported by the system.
  332. // Use the standard impl if the current locale is supported, use the
  333. // custom impl otherwise.
  334. function T_setlocale($category, $locale) {
  335. return _setlocale($category, $locale);
  336. }
  337. function T_bindtextdomain($domain, $path) {
  338. if (_check_locale_and_function()) return bindtextdomain($domain, $path);
  339. else return _bindtextdomain($domain, $path);
  340. }
  341. function T_bind_textdomain_codeset($domain, $codeset) {
  342. // bind_textdomain_codeset is available only in PHP 4.2.0+
  343. if (_check_locale_and_function('bind_textdomain_codeset'))
  344. return bind_textdomain_codeset($domain, $codeset);
  345. else return _bind_textdomain_codeset($domain, $codeset);
  346. }
  347. function T_textdomain($domain) {
  348. if (_check_locale_and_function()) return textdomain($domain);
  349. else return _textdomain($domain);
  350. }
  351. function T_gettext($msgid) {
  352. if (_check_locale_and_function()) return gettext($msgid);
  353. else return _gettext($msgid);
  354. }
  355. function T_($msgid) {
  356. if (_check_locale_and_function()) return _($msgid);
  357. return __($msgid);
  358. }
  359. function T_ngettext($single, $plural, $number) {
  360. if (_check_locale_and_function())
  361. return ngettext($single, $plural, $number);
  362. else return _ngettext($single, $plural, $number);
  363. }
  364. function T_dgettext($domain, $msgid) {
  365. if (_check_locale_and_function()) return dgettext($domain, $msgid);
  366. else return _dgettext($domain, $msgid);
  367. }
  368. function T_dngettext($domain, $single, $plural, $number) {
  369. if (_check_locale_and_function())
  370. return dngettext($domain, $single, $plural, $number);
  371. else return _dngettext($domain, $single, $plural, $number);
  372. }
  373. function T_dcgettext($domain, $msgid, $category) {
  374. if (_check_locale_and_function())
  375. return dcgettext($domain, $msgid, $category);
  376. else return _dcgettext($domain, $msgid, $category);
  377. }
  378. function T_dcngettext($domain, $single, $plural, $number, $category) {
  379. if (_check_locale_and_function())
  380. return dcngettext($domain, $single, $plural, $number, $category);
  381. else return _dcngettext($domain, $single, $plural, $number, $category);
  382. }
  383. function T_pgettext($context, $msgid) {
  384. if (_check_locale_and_function('pgettext'))
  385. return pgettext($context, $msgid);
  386. else
  387. return _pgettext($context, $msgid);
  388. }
  389. function T_dpgettext($domain, $context, $msgid) {
  390. if (_check_locale_and_function('dpgettext'))
  391. return dpgettext($domain, $context, $msgid);
  392. else
  393. return _dpgettext($domain, $context, $msgid);
  394. }
  395. function T_dcpgettext($domain, $context, $msgid, $category) {
  396. if (_check_locale_and_function('dcpgettext'))
  397. return dcpgettext($domain, $context, $msgid, $category);
  398. else
  399. return _dcpgettext($domain, $context, $msgid, $category);
  400. }
  401. function T_npgettext($context, $singular, $plural) {
  402. if (_check_locale_and_function('npgettext'))
  403. return npgettext($context, $single, $plural, $number);
  404. else
  405. return _npgettext($context, $single, $plural, $number);
  406. }
  407. function T_dnpgettext($domain, $context, $singular, $plural) {
  408. if (_check_locale_and_function('dnpgettext'))
  409. return dnpgettext($domain, $context, $single, $plural, $number);
  410. else
  411. return _dnpgettext($domain, $context, $single, $plural, $number);
  412. }
  413. function T_dcnpgettext($domain, $context, $singular, $plural, $category) {
  414. if (_check_locale_and_function('dcnpgettext'))
  415. return dcnpgettext($domain, $context, $single,
  416. $plural, $number, $category);
  417. else
  418. return _dcnpgettext($domain, $context, $single,
  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($single, $plural, $number) {
  439. return _ngettext($single, $plural, $number);
  440. }
  441. function dgettext($domain, $msgid) {
  442. return _dgettext($domain, $msgid);
  443. }
  444. function dngettext($domain, $single, $plural, $number) {
  445. return _dngettext($domain, $single, $plural, $number);
  446. }
  447. function dcgettext($domain, $msgid, $category) {
  448. return _dcgettext($domain, $msgid, $category);
  449. }
  450. function dcngettext($domain, $single, $plural, $number, $category) {
  451. return _dcngettext($domain, $single, $plural, $number, $category);
  452. }
  453. function pgettext($context, $msgid) {
  454. return _pgettext($context, $msgid);
  455. }
  456. function npgettext($context, $single, $plural, $number) {
  457. return _npgettext($context, $single, $plural, $number);
  458. }
  459. function dpgettext($domain, $context, $msgid) {
  460. return _dpgettext($domain, $context, $msgid);
  461. }
  462. function dnpgettext($domain, $context, $single, $plural, $number) {
  463. return _dnpgettext($domain, $context, $single, $plural, $number);
  464. }
  465. function dcpgettext($domain, $context, $msgid, $category) {
  466. return _dcpgettext($domain, $context, $msgid, $category);
  467. }
  468. function dcnpgettext($domain, $context, $single, $plural,
  469. $number, $category) {
  470. return _dcnpgettext($domain, $context, $single, $plural,
  471. $number, $category);
  472. }
  473. }
  474. ?>