3 Copyright (c) 2005 Steven Armstrong <sa at c-area dot ch>
5 Drop in replacement for native gettext.
7 This file is part of PHP-gettext.
9 PHP-gettext is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 PHP-gettext is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with PHP-gettext; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 require('streams.php');
35 require('gettext.php');
40 global $text_domains, $default_domain, $LC_CATEGORIES, $EMULATEGETTEXT, $CURRENTLOCALE;
41 $text_domains = array();
42 $default_domain = 'messages';
43 $LC_CATEGORIES = array('LC_CTYPE', 'LC_NUMERIC', 'LC_TIME', 'LC_COLLATE', 'LC_MONETARY', 'LC_MESSAGES', 'LC_ALL');
51 * Utility function to get a StreamReader for the given text domain.
53 function _get_reader($domain=null, $category=5, $enable_cache=true) {
54 global $text_domains, $default_domain, $LC_CATEGORIES;
55 if (!isset($domain)) $domain = $default_domain;
56 if (!isset($text_domains[$domain]->l10n)) {
57 // get the current locale
58 $locale = _setlocale(LC_MESSAGES, 0);
59 $p = isset($text_domains[$domain]->path) ? $text_domains[$domain]->path : './';
60 $path = $p . "$locale/". $LC_CATEGORIES[$category] ."/$domain.mo";
61 if (file_exists($path)) {
62 $input = new FileReader($path);
67 $text_domains[$domain]->l10n = new gettext_reader($input, $enable_cache);
69 return $text_domains[$domain]->l10n;
73 * Returns whether we are using our emulated gettext API or PHP built-in one.
75 function locale_emulation() {
76 global $EMULATEGETTEXT;
77 return $EMULATEGETTEXT;
81 * Checks if the current locale is supported on this system.
83 function _check_locale() {
84 global $EMULATEGETTEXT;
85 return !$EMULATEGETTEXT;
89 * Get the codeset for the given domain.
91 function _get_codeset($domain=null) {
92 global $text_domains, $default_domain, $LC_CATEGORIES;
93 if (!isset($domain)) $domain = $default_domain;
94 return (isset($text_domains[$domain]->codeset))? $text_domains[$domain]->codeset : ini_get('mbstring.internal_encoding');
98 * Convert the given string to the encoding set by bind_textdomain_codeset.
100 function _encode($text) {
101 $source_encoding = mb_detect_encoding($text);
102 $target_encoding = _get_codeset();
103 if ($source_encoding != $target_encoding) {
104 return mb_convert_encoding($text, $target_encoding, $source_encoding);
114 // Custom implementation of the standard gettext related functions
117 * Sets a requested locale, if needed emulates it.
119 function _setlocale($category, $locale) {
120 global $CURRENTLOCALE, $EMULATEGETTEXT;
121 if ($locale === 0) { // use === to differentiate between string "0"
122 if ($CURRENTLOCALE != '')
123 return $CURRENTLOCALE;
125 // obey LANG variable, maybe extend to support all of LC_* vars
126 // even if we tried to read locale without setting it first
127 return _setlocale($category, $CURRENTLOCALE);
130 if (function_exists('setlocale')) // I don't know if this ever happens ;)
131 $ret = setlocale($category, $locale);
132 if (($ret and $locale == '') or ($ret == $locale)) {
134 $CURRENTLOCALE = $ret;
136 if ($locale == '') // emulate variable support
137 $CURRENTLOCALE = getenv('LANG');
139 $CURRENTLOCALE = $locale;
142 return $CURRENTLOCALE;
147 * Sets the path for a domain.
149 function _bindtextdomain($domain, $path) {
150 global $text_domains;
151 // ensure $path ends with a slash
152 if ($path[strlen($path) - 1] != '/') $path .= '/';
153 elseif ($path[strlen($path) - 1] != '\\') $path .= '\\';
154 $text_domains[$domain]->path = $path;
158 * Specify the character encoding in which the messages from the DOMAIN message catalog will be returned.
160 function _bind_textdomain_codeset($domain, $codeset) {
161 global $text_domains;
162 $text_domains[$domain]->codeset = $codeset;
166 * Sets the default domain.
168 function _textdomain($domain) {
169 global $default_domain;
170 $default_domain = $domain;
174 * Lookup a message in the current domain.
176 function _gettext($msgid) {
177 $l10n = _get_reader();
178 //return $l10n->translate($msgid);
179 return _encode($l10n->translate($msgid));
184 function __($msgid) {
185 return _gettext($msgid);
188 * Plural version of gettext.
190 function _ngettext($single, $plural, $number) {
191 $l10n = _get_reader();
192 //return $l10n->ngettext($single, $plural, $number);
193 return _encode($l10n->ngettext($single, $plural, $number));
197 * Override the current domain.
199 function _dgettext($domain, $msgid) {
200 $l10n = _get_reader($domain);
201 //return $l10n->translate($msgid);
202 return _encode($l10n->translate($msgid));
205 * Plural version of dgettext.
207 function _dngettext($domain, $single, $plural, $number) {
208 $l10n = _get_reader($domain);
209 //return $l10n->ngettext($single, $plural, $number);
210 return _encode($l10n->ngettext($single, $plural, $number));
214 * Overrides the domain and category for a single lookup.
216 function _dcgettext($domain, $msgid, $category) {
217 $l10n = _get_reader($domain, $category);
218 //return $l10n->translate($msgid);
219 return _encode($l10n->translate($msgid));
222 * Plural version of dcgettext.
224 function _dcngettext($domain, $single, $plural, $number, $category) {
225 $l10n = _get_reader($domain, $category);
226 //return $l10n->ngettext($single, $plural, $number);
227 return _encode($l10n->ngettext($single, $plural, $number));
232 // Wrappers to use if the standard gettext functions are available, but the current locale is not supported by the system.
233 // Use the standard impl if the current locale is supported, use the custom impl otherwise.
235 function T_setlocale($category, $locale) {
236 return _setlocale($category, $locale);
239 function T_bindtextdomain($domain, $path) {
240 if (_check_locale()) return bindtextdomain($domain, $path);
241 else return _bindtextdomain($domain, $path);
243 function T_bind_textdomain_codeset($domain, $codeset) {
244 // bind_textdomain_codeset is available only in PHP 4.2.0+
245 if (_check_locale() and function_exists('bind_textdomain_codeset')) return bind_textdomain_codeset($domain, $codeset);
246 else return _bind_textdomain_codeset($domain, $codeset);
248 function T_textdomain($domain) {
249 if (_check_locale()) return textdomain($domain);
250 else return _textdomain($domain);
252 function T_gettext($msgid) {
253 if (_check_locale()) return gettext($msgid);
254 else return _gettext($msgid);
256 function T_($msgid) {
257 if (_check_locale()) return _($msgid);
260 function T_ngettext($single, $plural, $number) {
261 if (_check_locale()) return ngettext($single, $plural, $number);
262 else return _ngettext($single, $plural, $number);
264 function T_dgettext($domain, $msgid) {
265 if (_check_locale()) return dgettext($domain, $msgid);
266 else return _dgettext($domain, $msgid);
268 function T_dngettext($domain, $single, $plural, $number) {
269 if (_check_locale()) return dngettext($domain, $single, $plural, $number);
270 else return _dngettext($domain, $single, $plural, $number);
272 function T_dcgettext($domain, $msgid, $category) {
273 if (_check_locale()) return dcgettext($domain, $msgid, $category);
274 else return _dcgettext($domain, $msgid, $category);
276 function T_dcngettext($domain, $single, $plural, $number, $category) {
277 if (_check_locale()) return dcngettext($domain, $single, $plural, $number, $category);
278 else return _dcngettext($domain, $single, $plural, $number, $category);
283 // Wrappers used as a drop in replacement for the standard gettext functions
285 if (!function_exists('gettext')) {
286 function bindtextdomain($domain, $path) {
287 return _bindtextdomain($domain, $path);
289 function bind_textdomain_codeset($domain, $codeset) {
290 return _bind_textdomain_codeset($domain, $codeset);
292 function textdomain($domain) {
293 return _textdomain($domain);
295 function gettext($msgid) {
296 return _gettext($msgid);
301 function ngettext($single, $plural, $number) {
302 return _ngettext($single, $plural, $number);
304 function dgettext($domain, $msgid) {
305 return _dgettext($domain, $msgid);
307 function dngettext($domain, $single, $plural, $number) {
308 return _dngettext($domain, $single, $plural, $number);
310 function dcgettext($domain, $msgid, $category) {
311 return _dcgettext($domain, $msgid, $category);
313 function dcngettext($domain, $single, $plural, $number, $category) {
314 return _dcngettext($domain, $single, $plural, $number, $category);