3 * StatusNet, the distributed open-source microblogging tool
5 * utility functions for i18n
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program 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 Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Matthew Gregg <matthew.gregg@gmail.com>
25 * @author Ciaran Gultnieks <ciaran@ciarang.com>
26 * @author Evan Prodromou <evan@status.net>
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 // Locale category constants are usually predefined, but may not be
36 // on some systems such as Win32.
37 $LC_CATEGORIES = array('LC_CTYPE',
44 foreach ($LC_CATEGORIES as $key => $name) {
45 if (!defined($name)) {
50 if (!function_exists('gettext')) {
51 require_once("php-gettext/gettext.inc");
55 if (!function_exists('dpgettext')) {
57 * Context-aware dgettext wrapper; use when messages in different contexts
58 * won't be distinguished from the English source but need different translations.
59 * The context string will appear as msgctxt in the .po files.
61 * Not currently exposed in PHP's gettext module; implemented to be compat
62 * with gettext.h's macros.
64 * @param string $domain domain identifier
65 * @param string $context context identifier, should be some key like "menu|file"
66 * @param string $msgid English source text
67 * @return string original or translated message
69 function dpgettext($domain, $context, $msg)
71 $msgid = $context . "\004" . $msg;
72 $out = dcgettext($domain, $msgid, LC_MESSAGES);
81 if (!function_exists('pgettext')) {
83 * Context-aware gettext wrapper; use when messages in different contexts
84 * won't be distinguished from the English source but need different translations.
85 * The context string will appear as msgctxt in the .po files.
87 * Not currently exposed in PHP's gettext module; implemented to be compat
88 * with gettext.h's macros.
90 * @param string $context context identifier, should be some key like "menu|file"
91 * @param string $msgid English source text
92 * @return string original or translated message
94 function pgettext($context, $msg)
96 return dpgettext(textdomain(NULL), $context, $msg);
100 if (!function_exists('dnpgettext')) {
102 * Context-aware dngettext wrapper; use when messages in different contexts
103 * won't be distinguished from the English source but need different translations.
104 * The context string will appear as msgctxt in the .po files.
106 * Not currently exposed in PHP's gettext module; implemented to be compat
107 * with gettext.h's macros.
109 * @param string $domain domain identifier
110 * @param string $context context identifier, should be some key like "menu|file"
111 * @param string $msg singular English source text
112 * @param string $plural plural English source text
113 * @param int $n number of items to control plural selection
114 * @return string original or translated message
116 function dnpgettext($domain, $context, $msg, $plural, $n)
118 $msgid = $context . "\004" . $msg;
119 $out = dcngettext($domain, $msgid, $plural, $n, LC_MESSAGES);
120 if ($out == $msgid) {
128 if (!function_exists('npgettext')) {
130 * Context-aware ngettext wrapper; use when messages in different contexts
131 * won't be distinguished from the English source but need different translations.
132 * The context string will appear as msgctxt in the .po files.
134 * Not currently exposed in PHP's gettext module; implemented to be compat
135 * with gettext.h's macros.
137 * @param string $context context identifier, should be some key like "menu|file"
138 * @param string $msg singular English source text
139 * @param string $plural plural English source text
140 * @param int $n number of items to control plural selection
141 * @return string original or translated message
143 function npgettext($context, $msg, $plural, $n)
145 return dnpgettext(textdomain(NULL), $msgid, $plural, $n, LC_MESSAGES);
150 * Shortcut for *gettext functions with smart domain detection.
152 * If calling from a plugin, this function checks which plugin was
153 * being called from and uses that as text domain, which will have
154 * been set up during plugin initialization.
156 * Also handles plurals and contexts depending on what parameters
159 * gettext -> _m($msg)
160 * ngettext -> _m($msg1, $msg2, $n)
161 * pgettext -> _m($ctx, $msg)
162 * npgettext -> _m($ctx, $msg1, $msg2, $n)
164 * @fixme may not work properly in eval'd code
169 function _m($msg/*, ...*/)
171 $domain = _mdomain(debug_backtrace());
172 $args = func_get_args();
173 switch(count($args)) {
174 case 1: return dgettext($domain, $msg);
175 case 2: return dpgettext($domain, $args[0], $args[1]);
176 case 3: return dngettext($domain, $args[0], $args[1], $args[2]);
177 case 4: return dnpgettext($domain, $args[0], $args[1], $args[2], $args[3]);
178 default: throw new Exception("Bad parameter count to _m()");
183 * Looks for which plugin we've been called from to set the gettext domain;
184 * if not in a plugin subdirectory, we'll use the default 'statusnet'.
186 * Note: we can't return null for default domain since most of the PHP gettext
187 * wrapper functions turn null into "" before passing to the backend library.
189 * @param array $backtrace debug_backtrace() output
192 * @fixme could explode if SN is under a 'plugins' folder or share name.
194 function _mdomain($backtrace)
199 'file' => string '/var/www/mublog/plugins/FeedSub/FeedSubPlugin.php' (length=49)
201 'function' => string '_m' (length=2)
204 0 => &string 'Feeds' (length=5)
207 $path = $backtrace[0]['file'];
208 if (!isset($cached[$path])) {
209 $final = 'statusnet'; // assume default domain
210 if (DIRECTORY_SEPARATOR !== '/') {
211 $path = strtr($path, DIRECTORY_SEPARATOR, '/');
213 $plug = strpos($path, '/plugins/');
214 if ($plug === false) {
215 // We're not in a plugin; return default domain.
216 $final = 'statusnet';
219 $cut2 = strpos($path, '/', $cut);
221 $final = substr($path, $cut, $cut2 - $cut);
223 // We might be running directly from the plugins dir?
224 // If so, there's no place to store locale info.
225 $final = 'statusnet';
228 $cached[$path] = $final;
230 return $cached[$path];
235 * Content negotiation for language codes
237 * @param string $httplang HTTP Accept-Language header
239 * @return string language code for best language match
242 function client_prefered_language($httplang)
244 $client_langs = array();
246 $all_languages = common_config('site', 'languages');
248 preg_match_all('"(((\S\S)-?(\S\S)?)(;q=([0-9.]+))?)\s*(,\s*|$)"',
249 strtolower($httplang), $httplang);
251 for ($i = 0; $i < count($httplang); $i++) {
252 if (!empty($httplang[2][$i])) {
253 // if no q default to 1.0
254 $client_langs[$httplang[2][$i]] =
255 ($httplang[6][$i]? (float) $httplang[6][$i] : 1.0 - ($i*0.01));
257 if (!empty($httplang[3][$i]) && empty($client_langs[$httplang[3][$i]])) {
258 // if a catchall default 0.01 lower
259 $client_langs[$httplang[3][$i]] =
260 ($httplang[6][$i]? (float) $httplang[6][$i]-0.01 : 0.99);
263 // sort in decending q
264 arsort($client_langs);
266 foreach ($client_langs as $lang => $q) {
267 if (isset($all_languages[$lang])) {
268 return($all_languages[$lang]['lang']);
275 * returns a simple code -> name mapping for languages
277 * @return array map of available languages by code to language name.
280 function get_nice_language_list()
282 $nice_lang = array();
284 $all_languages = common_config('site', 'languages');
286 foreach ($all_languages as $lang) {
287 $nice_lang = $nice_lang + array($lang['lang'] => $lang['name']);
293 * Check whether a language is right-to-left
295 * @param string $lang language code of the language to check
297 * @return boolean true if language is rtl
300 function is_rtl($lang)
302 $all_languages = common_config('site', 'languages');
303 $lang = $all_languages[$lang];
304 return ($lang['direction'] == 'rtl');
308 * Get a list of all languages that are enabled in the default config
310 * This should ONLY be called when setting up the default config in common.php.
311 * Any other attempt to get a list of languages should instead call
312 * common_config('site','languages')
314 * @return array mapping of language codes to language info
316 function get_all_languages() {
318 'af' => array('q' => 0.8, 'lang' => 'af', 'name' => 'Afrikaans', 'direction' => 'ltr'),
319 'ar' => array('q' => 0.8, 'lang' => 'ar', 'name' => 'Arabic', 'direction' => 'rtl'),
320 'arz' => array('q' => 0.8, 'lang' => 'arz', 'name' => 'Egyptian Spoken Arabic', 'direction' => 'rtl'),
321 'bg' => array('q' => 0.8, 'lang' => 'bg', 'name' => 'Bulgarian', 'direction' => 'ltr'),
322 'br' => array('q' => 0.8, 'lang' => 'br', 'name' => 'Breton', 'direction' => 'ltr'),
323 'ca' => array('q' => 0.5, 'lang' => 'ca', 'name' => 'Catalan', 'direction' => 'ltr'),
324 'cs' => array('q' => 0.5, 'lang' => 'cs', 'name' => 'Czech', 'direction' => 'ltr'),
325 'da' => array('q' => 0.8, 'lang' => 'da', 'name' => 'Danish', 'direction' => 'ltr'),
326 'de' => array('q' => 0.8, 'lang' => 'de', 'name' => 'German', 'direction' => 'ltr'),
327 'el' => array('q' => 0.1, 'lang' => 'el', 'name' => 'Greek', 'direction' => 'ltr'),
328 'eo' => array('q' => 0.8, 'lang' => 'eo', 'name' => 'Esperanto', 'direction' => 'ltr'),
329 'en-us' => array('q' => 1, 'lang' => 'en', 'name' => 'English (US)', 'direction' => 'ltr'),
330 'en-gb' => array('q' => 1, 'lang' => 'en_GB', 'name' => 'English (British)', 'direction' => 'ltr'),
331 'en' => array('q' => 1, 'lang' => 'en', 'name' => 'English (US)', 'direction' => 'ltr'),
332 'es' => array('q' => 1, 'lang' => 'es', 'name' => 'Spanish', 'direction' => 'ltr'),
333 'fa' => array('q' => 1, 'lang' => 'fa', 'name' => 'Persian', 'direction' => 'rtl'),
334 'fi' => array('q' => 1, 'lang' => 'fi', 'name' => 'Finnish', 'direction' => 'ltr'),
335 'fr-fr' => array('q' => 1, 'lang' => 'fr', 'name' => 'French', 'direction' => 'ltr'),
336 'fur' => array('q' => 0.8, 'lang' => 'fur', 'name' => 'Friulian', 'direction' => 'ltr'),
337 'ga' => array('q' => 0.5, 'lang' => 'ga', 'name' => 'Irish', 'direction' => 'ltr'),
338 'gl' => array('q' => 0.8, 'lang' => 'gl', 'name' => 'Galician', 'direction' => 'ltr'),
339 'he' => array('q' => 0.5, 'lang' => 'he', 'name' => 'Hebrew', 'direction' => 'rtl'),
340 'hsb' => array('q' => 0.8, 'lang' => 'hsb', 'name' => 'Upper Sorbian', 'direction' => 'ltr'),
341 'hu' => array('q' => 0.8, 'lang' => 'hu', 'name' => 'Hungarian', 'direction' => 'ltr'),
342 'ia' => array('q' => 0.8, 'lang' => 'ia', 'name' => 'Interlingua', 'direction' => 'ltr'),
343 'is' => array('q' => 0.1, 'lang' => 'is', 'name' => 'Icelandic', 'direction' => 'ltr'),
344 'it' => array('q' => 1, 'lang' => 'it', 'name' => 'Italian', 'direction' => 'ltr'),
345 'jp' => array('q' => 0.5, 'lang' => 'ja', 'name' => 'Japanese', 'direction' => 'ltr'),
346 'ka' => array('q' => 0.8, 'lang' => 'ka', 'name' => 'Georgian', 'direction' => 'ltr'),
347 'ko' => array('q' => 0.9, 'lang' => 'ko', 'name' => 'Korean', 'direction' => 'ltr'),
348 'mk' => array('q' => 0.5, 'lang' => 'mk', 'name' => 'Macedonian', 'direction' => 'ltr'),
349 'nb' => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
350 'no' => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
351 'nn' => array('q' => 1, 'lang' => 'nn', 'name' => 'Norwegian (Nynorsk)', 'direction' => 'ltr'),
352 'nl' => array('q' => 0.5, 'lang' => 'nl', 'name' => 'Dutch', 'direction' => 'ltr'),
353 'pl' => array('q' => 0.5, 'lang' => 'pl', 'name' => 'Polish', 'direction' => 'ltr'),
354 'pt' => array('q' => 0.1, 'lang' => 'pt', 'name' => 'Portuguese', 'direction' => 'ltr'),
355 'pt-br' => array('q' => 0.9, 'lang' => 'pt_BR', 'name' => 'Portuguese Brazil', 'direction' => 'ltr'),
356 'ru' => array('q' => 0.9, 'lang' => 'ru', 'name' => 'Russian', 'direction' => 'ltr'),
357 'sv' => array('q' => 0.8, 'lang' => 'sv', 'name' => 'Swedish', 'direction' => 'ltr'),
358 'te' => array('q' => 0.3, 'lang' => 'te', 'name' => 'Telugu', 'direction' => 'ltr'),
359 'tl' => array('q' => 0.8, 'lang' => 'tl', 'name' => 'Tagalog', 'direction' => 'ltr'),
360 'tr' => array('q' => 0.5, 'lang' => 'tr', 'name' => 'Turkish', 'direction' => 'ltr'),
361 'uk' => array('q' => 1, 'lang' => 'uk', 'name' => 'Ukrainian', 'direction' => 'ltr'),
362 'vi' => array('q' => 0.8, 'lang' => 'vi', 'name' => 'Vietnamese', 'direction' => 'ltr'),
363 'zh-cn' => array('q' => 0.9, 'lang' => 'zh_CN', 'name' => 'Chinese (Simplified)', 'direction' => 'ltr'),
364 'zh-hant' => array('q' => 0.2, 'lang' => 'zh_TW', 'name' => 'Chinese (Taiwanese)', 'direction' => 'ltr'),