3 Copyright (c) 2005 Steven Armstrong <sa at c-area dot ch>
4 Copyright (c) 2009 Danilo Segan <danilo@kvota.net>
6 Drop in replacement for native gettext.
8 This file is part of PHP-gettext.
10 PHP-gettext is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 PHP-gettext is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with PHP-gettext; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 // LC_MESSAGES is not available if php-gettext is not loaded
37 // while the other constants are already available from session extension.
38 if (!defined('LC_MESSAGES')) {
39 define('LC_MESSAGES', 5);
42 require('streams.php');
43 require('gettext.php');
48 global $text_domains, $default_domain, $LC_CATEGORIES, $EMULATEGETTEXT, $CURRENTLOCALE;
49 $text_domains = array();
50 $default_domain = 'messages';
51 $LC_CATEGORIES = array('LC_CTYPE', 'LC_NUMERIC', 'LC_TIME', 'LC_COLLATE', 'LC_MONETARY', 'LC_MESSAGES', 'LC_ALL');
55 /* Class to hold a single domain included in $text_domains. */
65 * Return a list of locales to try for any POSIX-style locale specification.
67 function get_list_of_locales($locale) {
68 /* Figure out all possible locale names and start with the most
69 * specific ones. I.e. for sr_CS.UTF-8@latin, look through all of
70 * sr_CS.UTF-8@latin, sr_CS@latin, sr@latin, sr_CS.UTF-8, sr_CS, sr.
72 $locale_names = array();
78 if (preg_match("/^(?P<lang>[a-z]{2,3})" // language code
79 ."(?:_(?P<country>[A-Z]{2}))?" // country code
80 ."(?:\.(?P<charset>[-A-Za-z0-9_]+))?" // charset
81 ."(?:@(?P<modifier>[-A-Za-z0-9_]+))?$/", // @ modifier
84 if (isset($matches["lang"])) $lang = $matches["lang"];
85 if (isset($matches["country"])) $country = $matches["country"];
86 if (isset($matches["charset"])) $charset = $matches["charset"];
87 if (isset($matches["modifier"])) $modifier = $matches["modifier"];
92 array_push($locale_names, "${lang}_$country.$charset@$modifier");
93 array_push($locale_names, "${lang}_$country@$modifier");
95 array_push($locale_names, "${lang}.$charset@$modifier");
96 array_push($locale_names, "$lang@$modifier");
100 array_push($locale_names, "${lang}_$country.$charset");
101 array_push($locale_names, "${lang}_$country");
103 array_push($locale_names, "${lang}.$charset");
104 array_push($locale_names, $lang);
107 // If the locale name doesn't match POSIX style, just include it as-is.
108 if (!in_array($locale, $locale_names))
109 array_push($locale_names, $locale);
111 return $locale_names;
115 * Utility function to get a StreamReader for the given text domain.
117 function _get_reader($domain=null, $category=5, $enable_cache=true) {
118 global $text_domains, $default_domain, $LC_CATEGORIES;
119 if (!isset($domain)) $domain = $default_domain;
120 if (!isset($text_domains[$domain]->l10n)) {
121 // get the current locale
122 $locale = _setlocale(LC_MESSAGES, 0);
123 $bound_path = isset($text_domains[$domain]->path) ?
124 $text_domains[$domain]->path : './';
125 $subpath = $LC_CATEGORIES[$category] ."/$domain.mo";
127 $locale_names = get_list_of_locales($locale);
129 foreach ($locale_names as $locale) {
130 $full_path = $bound_path . $locale . "/" . $subpath;
131 if (file_exists($full_path)) {
132 $input = new FileReader($full_path);
137 if (!array_key_exists($domain, $text_domains)) {
138 // Initialize an empty domain object.
139 $text_domains[$domain] = new domain();
141 $text_domains[$domain]->l10n = new gettext_reader($input,
144 return $text_domains[$domain]->l10n;
148 * Returns whether we are using our emulated gettext API or PHP built-in one.
150 function locale_emulation() {
151 global $EMULATEGETTEXT;
152 return $EMULATEGETTEXT;
156 * Checks if the current locale is supported on this system.
158 function _check_locale_and_function($function=false) {
159 global $EMULATEGETTEXT;
160 if ($function and !function_exists($function))
162 return !$EMULATEGETTEXT;
166 * Get the codeset for the given domain.
168 function _get_codeset($domain=null) {
169 global $text_domains, $default_domain, $LC_CATEGORIES;
170 if (!isset($domain)) $domain = $default_domain;
171 return (isset($text_domains[$domain]->codeset))? $text_domains[$domain]->codeset : ini_get('mbstring.internal_encoding');
175 * Convert the given string to the encoding set by bind_textdomain_codeset.
177 function _encode($text) {
178 $source_encoding = mb_detect_encoding($text);
179 $target_encoding = _get_codeset();
180 if ($source_encoding != $target_encoding) {
181 return mb_convert_encoding($text, $target_encoding, $source_encoding);
189 // Custom implementation of the standard gettext related functions
192 * Returns passed in $locale, or environment variable $LANG if $locale == ''.
194 function _get_default_locale($locale) {
195 if ($locale == '') // emulate variable support
196 return getenv('LANG');
202 * Sets a requested locale, if needed emulates it.
204 function _setlocale($category, $locale) {
205 global $CURRENTLOCALE, $EMULATEGETTEXT;
206 if ($locale === 0) { // use === to differentiate between string "0"
207 if ($CURRENTLOCALE != '')
208 return $CURRENTLOCALE;
210 // obey LANG variable, maybe extend to support all of LC_* vars
211 // even if we tried to read locale without setting it first
212 return _setlocale($category, $CURRENTLOCALE);
214 if (function_exists('setlocale')) {
215 $ret = setlocale($category, $locale);
216 if (($locale == '' and !$ret) or // failed setting it by env
217 ($locale != '' and $ret != $locale)) { // failed setting it
218 // Failed setting it according to environment.
219 $CURRENTLOCALE = _get_default_locale($locale);
222 $CURRENTLOCALE = $ret;
226 // No function setlocale(), emulate it all.
227 $CURRENTLOCALE = _get_default_locale($locale);
230 // Allow locale to be changed on the go for one translation domain.
231 global $text_domains, $default_domain;
232 unset($text_domains[$default_domain]->l10n);
233 return $CURRENTLOCALE;
238 * Sets the path for a domain.
240 function _bindtextdomain($domain, $path) {
241 global $text_domains;
242 // ensure $path ends with a slash ('/' should work for both, but lets still play nice)
243 if (substr(php_uname(), 0, 7) == "Windows") {
244 if ($path[strlen($path)-1] != '\\' and $path[strlen($path)-1] != '/')
247 if ($path[strlen($path)-1] != '/')
250 if (!array_key_exists($domain, $text_domains)) {
251 // Initialize an empty domain object.
252 $text_domains[$domain] = new domain();
254 $text_domains[$domain]->path = $path;
258 * Specify the character encoding in which the messages from the DOMAIN message catalog will be returned.
260 function _bind_textdomain_codeset($domain, $codeset) {
261 global $text_domains;
262 $text_domains[$domain]->codeset = $codeset;
266 * Sets the default domain.
268 function _textdomain($domain) {
269 global $default_domain;
270 $default_domain = $domain;
274 * Lookup a message in the current domain.
276 function _gettext($msgid) {
277 $l10n = _get_reader();
278 return _encode($l10n->translate($msgid));
284 function __($msgid) {
285 return _gettext($msgid);
289 * Plural version of gettext.
291 function _ngettext($single, $plural, $number) {
292 $l10n = _get_reader();
293 return _encode($l10n->ngettext($single, $plural, $number));
297 * Override the current domain.
299 function _dgettext($domain, $msgid) {
300 $l10n = _get_reader($domain);
301 return _encode($l10n->translate($msgid));
305 * Plural version of dgettext.
307 function _dngettext($domain, $single, $plural, $number) {
308 $l10n = _get_reader($domain);
309 return _encode($l10n->ngettext($single, $plural, $number));
313 * Overrides the domain and category for a single lookup.
315 function _dcgettext($domain, $msgid, $category) {
316 $l10n = _get_reader($domain, $category);
317 return _encode($l10n->translate($msgid));
320 * Plural version of dcgettext.
322 function _dcngettext($domain, $single, $plural, $number, $category) {
323 $l10n = _get_reader($domain, $category);
324 return _encode($l10n->ngettext($single, $plural, $number));
328 * Context version of gettext.
330 function _pgettext($context, $msgid) {
331 $l10n = _get_reader();
332 return _encode($l10n->pgettext($context, $msgid));
336 * Override the current domain in a context gettext call.
338 function _dpgettext($domain, $context, $msgid) {
339 $l10n = _get_reader($domain);
340 return _encode($l10n->pgettext($context, $msgid));
344 * Overrides the domain and category for a single context-based lookup.
346 function _dcpgettext($domain, $context, $msgid, $category) {
347 $l10n = _get_reader($domain, $category);
348 return _encode($l10n->pgettext($context, $msgid));
352 * Context version of ngettext.
354 function _npgettext($context, $singular, $plural) {
355 $l10n = _get_reader();
356 return _encode($l10n->npgettext($context, $singular, $plural));
360 * Override the current domain in a context ngettext call.
362 function _dnpgettext($domain, $context, $singular, $plural) {
363 $l10n = _get_reader($domain);
364 return _encode($l10n->npgettext($context, $singular, $plural));
368 * Overrides the domain and category for a plural context-based lookup.
370 function _dcnpgettext($domain, $context, $singular, $plural, $category) {
371 $l10n = _get_reader($domain, $category);
372 return _encode($l10n->npgettext($context, $singular, $plural));
377 // Wrappers to use if the standard gettext functions are available,
378 // but the current locale is not supported by the system.
379 // Use the standard impl if the current locale is supported, use the
380 // custom impl otherwise.
382 function T_setlocale($category, $locale) {
383 return _setlocale($category, $locale);
386 function T_bindtextdomain($domain, $path) {
387 if (_check_locale_and_function()) return bindtextdomain($domain, $path);
388 else return _bindtextdomain($domain, $path);
390 function T_bind_textdomain_codeset($domain, $codeset) {
391 // bind_textdomain_codeset is available only in PHP 4.2.0+
392 if (_check_locale_and_function('bind_textdomain_codeset'))
393 return bind_textdomain_codeset($domain, $codeset);
394 else return _bind_textdomain_codeset($domain, $codeset);
396 function T_textdomain($domain) {
397 if (_check_locale_and_function()) return textdomain($domain);
398 else return _textdomain($domain);
400 function T_gettext($msgid) {
401 if (_check_locale_and_function()) return gettext($msgid);
402 else return _gettext($msgid);
404 function T_($msgid) {
405 if (_check_locale_and_function()) return _($msgid);
408 function T_ngettext($single, $plural, $number) {
409 if (_check_locale_and_function())
410 return ngettext($single, $plural, $number);
411 else return _ngettext($single, $plural, $number);
413 function T_dgettext($domain, $msgid) {
414 if (_check_locale_and_function()) return dgettext($domain, $msgid);
415 else return _dgettext($domain, $msgid);
417 function T_dngettext($domain, $single, $plural, $number) {
418 if (_check_locale_and_function())
419 return dngettext($domain, $single, $plural, $number);
420 else return _dngettext($domain, $single, $plural, $number);
422 function T_dcgettext($domain, $msgid, $category) {
423 if (_check_locale_and_function())
424 return dcgettext($domain, $msgid, $category);
425 else return _dcgettext($domain, $msgid, $category);
427 function T_dcngettext($domain, $single, $plural, $number, $category) {
428 if (_check_locale_and_function())
429 return dcngettext($domain, $single, $plural, $number, $category);
430 else return _dcngettext($domain, $single, $plural, $number, $category);
433 function T_pgettext($context, $msgid) {
434 if (_check_locale_and_function('pgettext'))
435 return pgettext($context, $msgid);
437 return _pgettext($context, $msgid);
440 function T_dpgettext($domain, $context, $msgid) {
441 if (_check_locale_and_function('dpgettext'))
442 return dpgettext($domain, $context, $msgid);
444 return _dpgettext($domain, $context, $msgid);
447 function T_dcpgettext($domain, $context, $msgid, $category) {
448 if (_check_locale_and_function('dcpgettext'))
449 return dcpgettext($domain, $context, $msgid, $category);
451 return _dcpgettext($domain, $context, $msgid, $category);
454 function T_npgettext($context, $singular, $plural) {
455 if (_check_locale_and_function('npgettext'))
456 return npgettext($context, $single, $plural, $number);
458 return _npgettext($context, $single, $plural, $number);
461 function T_dnpgettext($domain, $context, $singular, $plural) {
462 if (_check_locale_and_function('dnpgettext'))
463 return dnpgettext($domain, $context, $single, $plural, $number);
465 return _dnpgettext($domain, $context, $single, $plural, $number);
468 function T_dcnpgettext($domain, $context, $singular, $plural, $category) {
469 if (_check_locale_and_function('dcnpgettext'))
470 return dcnpgettext($domain, $context, $single,
471 $plural, $number, $category);
473 return _dcnpgettext($domain, $context, $single,
474 $plural, $number, $category);
479 // Wrappers used as a drop in replacement for the standard gettext functions
481 if (!function_exists('gettext')) {
482 function bindtextdomain($domain, $path) {
483 return _bindtextdomain($domain, $path);
485 function bind_textdomain_codeset($domain, $codeset) {
486 return _bind_textdomain_codeset($domain, $codeset);
488 function textdomain($domain) {
489 return _textdomain($domain);
491 function gettext($msgid) {
492 return _gettext($msgid);
497 function ngettext($single, $plural, $number) {
498 return _ngettext($single, $plural, $number);
500 function dgettext($domain, $msgid) {
501 return _dgettext($domain, $msgid);
503 function dngettext($domain, $single, $plural, $number) {
504 return _dngettext($domain, $single, $plural, $number);
506 function dcgettext($domain, $msgid, $category) {
507 return _dcgettext($domain, $msgid, $category);
509 function dcngettext($domain, $single, $plural, $number, $category) {
510 return _dcngettext($domain, $single, $plural, $number, $category);
512 function pgettext($context, $msgid) {
513 return _pgettext($context, $msgid);
515 function npgettext($context, $single, $plural, $number) {
516 return _npgettext($context, $single, $plural, $number);
518 function dpgettext($domain, $context, $msgid) {
519 return _dpgettext($domain, $context, $msgid);
521 function dnpgettext($domain, $context, $single, $plural, $number) {
522 return _dnpgettext($domain, $context, $single, $plural, $number);
524 function dcpgettext($domain, $context, $msgid, $category) {
525 return _dcpgettext($domain, $context, $msgid, $category);
527 function dcnpgettext($domain, $context, $single, $plural,
528 $number, $category) {
529 return _dcnpgettext($domain, $context, $single, $plural,