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

/inc/thirdparty/gettext.inc

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