]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/php-gettext/gettext.inc
Added type-hints for StartInitializeRouter hooks.
[quix0rs-gnu-social.git] / extlib / php-gettext / gettext.inc
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
6    Drop in replacement for native gettext.
7
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 /*
26 LC_CTYPE        0
27 LC_NUMERIC      1
28 LC_TIME         2
29 LC_COLLATE      3
30 LC_MONETARY     4
31 LC_MESSAGES     5
32 LC_ALL          6
33 */
34
35 // LC_MESSAGES is not available if php-gettext is not loaded
36 // while the other constants are already available from session extension.
37 if (!defined('LC_MESSAGES')) {
38   define('LC_MESSAGES', 5);
39 }
40
41 require('streams.php');
42 require('gettext.php');
43
44
45 // Variables
46
47 global $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
54 /* Class to hold a single domain included in $text_domains. */
55 class domain {
56   var $l10n;
57   var $path;
58   var $codeset;
59 }
60
61 // Utility functions
62
63 /**
64  * Return a list of locales to try for any POSIX-style locale specification.
65  */
66 function 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
113 /**
114  * Utility function to get a StreamReader for the given text domain.
115  */
116 function _get_reader($domain=null, $category=5, $enable_cache=true) {
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;
144 }
145
146 /**
147  * Returns whether we are using our emulated gettext API or PHP built-in one.
148  */
149 function locale_emulation() {
150     global $EMULATEGETTEXT;
151     return $EMULATEGETTEXT;
152 }
153
154 /**
155  * Checks if the current locale is supported on this system.
156  */
157 function _check_locale_and_function($function=false) {
158     global $EMULATEGETTEXT;
159     if ($function and !function_exists($function))
160         return false;
161     return !$EMULATEGETTEXT;
162 }
163
164 /**
165  * Get the codeset for the given domain.
166  */
167 function _get_codeset($domain=null) {
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');
171 }
172
173 /**
174  * Convert the given string to the encoding set by bind_textdomain_codeset.
175  */
176 function _encode($text) {
177   $target_encoding = _get_codeset();
178   if (function_exists("mb_detect_encoding")) {
179     $source_encoding = mb_detect_encoding($text);
180     if ($source_encoding != $target_encoding)
181       $text = mb_convert_encoding($text, $target_encoding, $source_encoding);
182   }
183   return $text;
184 }
185
186
187 // Custom implementation of the standard gettext related functions
188
189 /**
190  * Returns passed in $locale, or environment variable $LANG if $locale == ''.
191  */
192 function _get_default_locale($locale) {
193   if ($locale == '') // emulate variable support
194     return getenv('LANG');
195   else
196     return $locale;
197 }
198
199 /**
200  * Sets a requested locale, if needed emulates it.
201  */
202 function _setlocale($category, $locale) {
203     global $CURRENTLOCALE, $EMULATEGETTEXT;
204     if ($locale === 0) { // use === to differentiate between string "0"
205         if ($CURRENTLOCALE != '')
206             return $CURRENTLOCALE;
207         else
208             // obey LANG variable, maybe extend to support all of LC_* vars
209             // even if we tried to read locale without setting it first
210             return _setlocale($category, $CURRENTLOCALE);
211     } else {
212         if (function_exists('setlocale')) {
213           $ret = setlocale($category, $locale);
214           if (($locale == '' and !$ret) or // failed setting it by env
215               ($locale != '' and $ret != $locale)) { // failed setting it
216             // Failed setting it according to environment.
217             $CURRENTLOCALE = _get_default_locale($locale);
218             $EMULATEGETTEXT = 1;
219           } else {
220             $CURRENTLOCALE = $ret;
221             $EMULATEGETTEXT = 0;
222           }
223         } else {
224           // No function setlocale(), emulate it all.
225           $CURRENTLOCALE = _get_default_locale($locale);
226           $EMULATEGETTEXT = 1;
227         }
228         // Allow locale to be changed on the go for one translation domain.
229         global $text_domains, $default_domain;
230         if (array_key_exists($default_domain, $text_domains)) {
231             unset($text_domains[$default_domain]->l10n);
232         }
233         return $CURRENTLOCALE;
234     }
235 }
236
237 /**
238  * Sets the path for a domain.
239  */
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] != '/')
245         $path .= '\\';
246     } else {
247       if ($path[strlen($path)-1] != '/')
248         $path .= '/';
249     }
250     if (!array_key_exists($domain, $text_domains)) {
251       // Initialize an empty domain object.
252       $text_domains[$domain] = new domain();
253     }
254     $text_domains[$domain]->path = $path;
255 }
256
257 /**
258  * Specify the character encoding in which the messages from the DOMAIN message catalog will be returned.
259  */
260 function _bind_textdomain_codeset($domain, $codeset) {
261     global $text_domains;
262     $text_domains[$domain]->codeset = $codeset;
263 }
264
265 /**
266  * Sets the default domain.
267  */
268 function _textdomain($domain) {
269     global $default_domain;
270     $default_domain = $domain;
271 }
272
273 /**
274  * Lookup a message in the current domain.
275  */
276 function _gettext($msgid) {
277     $l10n = _get_reader();
278     return _encode($l10n->translate($msgid));
279 }
280
281 /**
282  * Alias for gettext.
283  */
284 function __($msgid) {
285     return _gettext($msgid);
286 }
287
288 /**
289  * Plural version of gettext.
290  */
291 function _ngettext($singular, $plural, $number) {
292     $l10n = _get_reader();
293     return _encode($l10n->ngettext($singular, $plural, $number));
294 }
295
296 /**
297  * Override the current domain.
298  */
299 function _dgettext($domain, $msgid) {
300     $l10n = _get_reader($domain);
301     return _encode($l10n->translate($msgid));
302 }
303
304 /**
305  * Plural version of dgettext.
306  */
307 function _dngettext($domain, $singular, $plural, $number) {
308     $l10n = _get_reader($domain);
309     return _encode($l10n->ngettext($singular, $plural, $number));
310 }
311
312 /**
313  * Overrides the domain and category for a single lookup.
314  */
315 function _dcgettext($domain, $msgid, $category) {
316     $l10n = _get_reader($domain, $category);
317     return _encode($l10n->translate($msgid));
318 }
319 /**
320  * Plural version of dcgettext.
321  */
322 function _dcngettext($domain, $singular, $plural, $number, $category) {
323     $l10n = _get_reader($domain, $category);
324     return _encode($l10n->ngettext($singular, $plural, $number));
325 }
326
327 /**
328  * Context version of gettext.
329  */
330 function _pgettext($context, $msgid) {
331     $l10n = _get_reader();
332     return _encode($l10n->pgettext($context, $msgid));
333 }
334
335 /**
336  * Override the current domain in a context gettext call.
337  */
338 function _dpgettext($domain, $context, $msgid) {
339     $l10n = _get_reader($domain);
340     return _encode($l10n->pgettext($context, $msgid));
341 }
342
343 /**
344  * Overrides the domain and category for a single context-based lookup.
345  */
346 function _dcpgettext($domain, $context, $msgid, $category) {
347     $l10n = _get_reader($domain, $category);
348     return _encode($l10n->pgettext($context, $msgid));
349 }
350
351 /**
352  * Context version of ngettext.
353  */
354 function _npgettext($context, $singular, $plural) {
355     $l10n = _get_reader();
356     return _encode($l10n->npgettext($context, $singular, $plural));
357 }
358
359 /**
360  * Override the current domain in a context ngettext call.
361  */
362 function _dnpgettext($domain, $context, $singular, $plural) {
363     $l10n = _get_reader($domain);
364     return _encode($l10n->npgettext($context, $singular, $plural));
365 }
366
367 /**
368  * Overrides the domain and category for a plural context-based lookup.
369  */
370 function _dcnpgettext($domain, $context, $singular, $plural, $category) {
371     $l10n = _get_reader($domain, $category);
372     return _encode($l10n->npgettext($context, $singular, $plural));
373 }
374
375
376
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.
381
382 function T_setlocale($category, $locale) {
383     return _setlocale($category, $locale);
384 }
385
386 function T_bindtextdomain($domain, $path) {
387     if (_check_locale_and_function()) return bindtextdomain($domain, $path);
388     else return _bindtextdomain($domain, $path);
389 }
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);
395 }
396 function T_textdomain($domain) {
397     if (_check_locale_and_function()) return textdomain($domain);
398     else return _textdomain($domain);
399 }
400 function T_gettext($msgid) {
401     if (_check_locale_and_function()) return gettext($msgid);
402     else return _gettext($msgid);
403 }
404 function T_($msgid) {
405     if (_check_locale_and_function()) return _($msgid);
406     return __($msgid);
407 }
408 function T_ngettext($singular, $plural, $number) {
409     if (_check_locale_and_function())
410         return ngettext($singular, $plural, $number);
411     else return _ngettext($singular, $plural, $number);
412 }
413 function T_dgettext($domain, $msgid) {
414     if (_check_locale_and_function()) return dgettext($domain, $msgid);
415     else return _dgettext($domain, $msgid);
416 }
417 function T_dngettext($domain, $singular, $plural, $number) {
418     if (_check_locale_and_function())
419         return dngettext($domain, $singular, $plural, $number);
420     else return _dngettext($domain, $singular, $plural, $number);
421 }
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);
426 }
427 function T_dcngettext($domain, $singular, $plural, $number, $category) {
428     if (_check_locale_and_function())
429       return dcngettext($domain, $singular, $plural, $number, $category);
430     else return _dcngettext($domain, $singular, $plural, $number, $category);
431 }
432
433 function T_pgettext($context, $msgid) {
434   if (_check_locale_and_function('pgettext'))
435       return pgettext($context, $msgid);
436   else
437       return _pgettext($context, $msgid);
438 }
439
440 function T_dpgettext($domain, $context, $msgid) {
441   if (_check_locale_and_function('dpgettext'))
442       return dpgettext($domain, $context, $msgid);
443   else
444       return _dpgettext($domain, $context, $msgid);
445 }
446
447 function T_dcpgettext($domain, $context, $msgid, $category) {
448   if (_check_locale_and_function('dcpgettext'))
449       return dcpgettext($domain, $context, $msgid, $category);
450   else
451       return _dcpgettext($domain, $context, $msgid, $category);
452 }
453
454 function T_npgettext($context, $singular, $plural, $number) {
455     if (_check_locale_and_function('npgettext'))
456         return npgettext($context, $singular, $plural, $number);
457     else
458         return _npgettext($context, $singular, $plural, $number);
459 }
460
461 function T_dnpgettext($domain, $context, $singular, $plural, $number) {
462   if (_check_locale_and_function('dnpgettext'))
463       return dnpgettext($domain, $context, $singular, $plural, $number);
464   else
465       return _dnpgettext($domain, $context, $singular, $plural, $number);
466 }
467
468 function T_dcnpgettext($domain, $context, $singular, $plural,
469                        $number, $category) {
470     if (_check_locale_and_function('dcnpgettext'))
471         return dcnpgettext($domain, $context, $singular,
472                            $plural, $number, $category);
473     else
474         return _dcnpgettext($domain, $context, $singular,
475                             $plural, $number, $category);
476 }
477
478
479
480 // Wrappers used as a drop in replacement for the standard gettext functions
481
482 if (!function_exists('gettext')) {
483     function bindtextdomain($domain, $path) {
484         return _bindtextdomain($domain, $path);
485     }
486     function bind_textdomain_codeset($domain, $codeset) {
487         return _bind_textdomain_codeset($domain, $codeset);
488     }
489     function textdomain($domain) {
490         return _textdomain($domain);
491     }
492     function gettext($msgid) {
493         return _gettext($msgid);
494     }
495     function _($msgid) {
496         return __($msgid);
497     }
498     function ngettext($singular, $plural, $number) {
499         return _ngettext($singular, $plural, $number);
500     }
501     function dgettext($domain, $msgid) {
502         return _dgettext($domain, $msgid);
503     }
504     function dngettext($domain, $singular, $plural, $number) {
505         return _dngettext($domain, $singular, $plural, $number);
506     }
507     function dcgettext($domain, $msgid, $category) {
508         return _dcgettext($domain, $msgid, $category);
509     }
510     function dcngettext($domain, $singular, $plural, $number, $category) {
511         return _dcngettext($domain, $singular, $plural, $number, $category);
512     }
513     function pgettext($context, $msgid) {
514         return _pgettext($context, $msgid);
515     }
516     function npgettext($context, $singular, $plural, $number) {
517         return _npgettext($context, $singular, $plural, $number);
518     }
519     function dpgettext($domain, $context, $msgid) {
520         return _dpgettext($domain, $context, $msgid);
521     }
522     function dnpgettext($domain, $context, $singular, $plural, $number) {
523         return _dnpgettext($domain, $context, $singular, $plural, $number);
524     }
525     function dcpgettext($domain, $context, $msgid, $category) {
526         return _dcpgettext($domain, $context, $msgid, $category);
527     }
528     function dcnpgettext($domain, $context, $singular, $plural,
529                          $number, $category) {
530       return _dcnpgettext($domain, $context, $singular, $plural,
531                           $number, $category);
532     }
533 }
534
535 ?>