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