3 * GNU social - a federating social network
5 * utility functions for i18n
7 * LICENCE: This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * @author Matthew Gregg <matthew.gregg@gmail.com>
23 * @author Ciaran Gultnieks <ciaran@ciarang.com>
24 * @author Evan Prodromou <evan@status.net>
25 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
26 * @link http://status.net/
29 if (!defined('STATUSNET') && !defined('LACONICA')) {
33 // Locale category constants are usually predefined, but may not be
34 // on some systems such as Win32.
35 $LC_CATEGORIES = ['LC_CTYPE',
42 foreach ($LC_CATEGORIES as $key => $name) {
43 if (!defined($name)) {
48 if (!function_exists('dpgettext')) {
50 * Context-aware dgettext wrapper; use when messages in different contexts
51 * won't be distinguished from the English source but need different translations.
52 * The context string will appear as msgctxt in the .po files.
54 * Not currently exposed in PHP's gettext module; implemented to be compat
55 * with gettext.h's macros.
57 * @param string $domain domain identifier
58 * @param string $context context identifier, should be some key like "menu|file"
59 * @param string $msg English source text
60 * @return string original or translated message
62 function dpgettext($domain, $context, $msg)
64 $msgid = $context . "\004" . $msg;
65 $out = dcgettext($domain, $msgid, LC_MESSAGES);
74 if (!function_exists('pgettext')) {
76 * Context-aware gettext wrapper; use when messages in different contexts
77 * won't be distinguished from the English source but need different translations.
78 * The context string will appear as msgctxt in the .po files.
80 * Not currently exposed in PHP's gettext module; implemented to be compat
81 * with gettext.h's macros.
83 * @param string $context context identifier, should be some key like "menu|file"
84 * @param string $msgid English source text
85 * @return string original or translated message
87 function pgettext($context, $msgid)
89 return dpgettext(textdomain(NULL), $context, $msgid);
93 if (!function_exists('dnpgettext')) {
95 * Context-aware dngettext wrapper; use when messages in different contexts
96 * won't be distinguished from the English source but need different translations.
97 * The context string will appear as msgctxt in the .po files.
99 * Not currently exposed in PHP's gettext module; implemented to be compat
100 * with gettext.h's macros.
102 * @param string $domain domain identifier
103 * @param string $context context identifier, should be some key like "menu|file"
104 * @param string $msg singular English source text
105 * @param string $plural plural English source text
106 * @param int $n number of items to control plural selection
107 * @return string original or translated message
109 function dnpgettext($domain, $context, $msg, $plural, $n)
111 $msgid = $context . "\004" . $msg;
112 $out = dcngettext($domain, $msgid, $plural, $n, LC_MESSAGES);
113 if ($out == $msgid) {
121 if (!function_exists('npgettext')) {
123 * Context-aware ngettext wrapper; use when messages in different contexts
124 * won't be distinguished from the English source but need different translations.
125 * The context string will appear as msgctxt in the .po files.
127 * Not currently exposed in PHP's gettext module; implemented to be compat
128 * with gettext.h's macros.
130 * @param string $context context identifier, should be some key like "menu|file"
131 * @param string $msgid singular English source text
132 * @param string $plural plural English source text
133 * @param int $n number of items to control plural selection
134 * @return string original or translated message
136 function npgettext($context, $msgid, $plural, $n)
138 return dnpgettext(textdomain(NULL), $msgid, $plural, $n, LC_MESSAGES);
143 * Shortcut for *gettext functions with smart domain detection.
145 * If calling from a plugin, this function checks which plugin was
146 * being called from and uses that as text domain, which will have
147 * been set up during plugin initialization.
149 * Also handles plurals and contexts depending on what parameters
152 * gettext -> _m($msg)
153 * ngettext -> _m($msg1, $msg2, $n)
154 * pgettext -> _m($ctx, $msg)
155 * npgettext -> _m($ctx, $msg1, $msg2, $n)
157 * @fixme may not work properly in eval'd code
163 function _m($msg/*, ...*/)
165 $domain = _mdomain(debug_backtrace());
166 $args = func_get_args();
167 switch (count($args)) {
169 return dgettext($domain, $msg);
171 return dpgettext($domain, $args[0], $args[1]);
173 return dngettext($domain, $args[0], $args[1], $args[2]);
175 return dnpgettext($domain, $args[0], $args[1], $args[2], $args[3]);
177 throw new Exception("Bad parameter count to _m()");
182 * Looks for which plugin we've been called from to set the gettext domain;
183 * if not in a plugin subdirectory, we'll use the default 'statusnet'.
185 * Note: we can't return null for default domain since most of the PHP gettext
186 * wrapper functions turn null into "" before passing to the backend library.
188 * @param array $backtrace debug_backtrace() output
191 * @fixme could explode if SN is under a 'plugins' folder or share name.
193 function _mdomain($backtrace)
198 'file' => string '/var/www/mublog/plugins/FeedSub/FeedSubPlugin.php' (length=49)
200 'function' => string '_m' (length=2)
203 0 => &string 'Feeds' (length=5)
206 $path = $backtrace[0]['file'];
207 if (!isset($cached[$path])) {
208 if (DIRECTORY_SEPARATOR !== '/') {
209 $path = strtr($path, DIRECTORY_SEPARATOR, '/');
211 $plug = strpos($path, '/plugins/');
212 if ($plug === false) {
213 // We're not in a plugin; return default domain.
214 $final = 'statusnet';
217 $cut2 = strpos($path, '/', $cut);
219 $final = substr($path, $cut, $cut2 - $cut);
221 // We might be running directly from the plugins dir?
222 // If so, there's no place to store locale info.
223 $final = 'statusnet';
226 $cached[$path] = $final;
228 return $cached[$path];
233 * Content negotiation for language codes
235 * @param $http_accept_lang_header string HTTP Accept-Language header
236 * @return string language code for best language match, false otherwise
239 function client_preferred_language($http_accept_lang_header)
243 $all_languages = common_config('site', 'languages');
245 preg_match_all('"(((\S\S)-?(\S\S)?)(;q=([0-9.]+))?)\s*(,\s*|$)"',
246 strtolower($http_accept_lang_header), $http_langs);
248 for ($i = 0; $i < count($http_langs); ++$i) {
249 if (!empty($http_langs[2][$i])) {
250 // if no q default to 1.0
251 $client_langs[$http_langs[2][$i]] =
252 ($http_langs[6][$i] ? (float)$http_langs[6][$i] : 1.0 - ($i * 0.01));
254 if (!empty($http_langs[3][$i]) && empty($client_langs[$http_langs[3][$i]])) {
255 // if a catchall default 0.01 lower
256 $client_langs[$http_langs[3][$i]] =
257 ($http_langs[6][$i] ? (float)$http_langs[6][$i] - 0.01 : 0.99);
260 // sort in descending q
261 arsort($client_langs);
263 foreach ($client_langs as $lang => $q) {
264 if (isset($all_languages[$lang])) {
265 return ($all_languages[$lang]['lang']);
272 * returns a simple code -> name mapping for languages
274 * @return array map of available languages by code to language name.
277 function get_nice_language_list()
281 $all_languages = common_config('site', 'languages');
283 foreach ($all_languages as $lang) {
284 $nice_lang = $nice_lang + array($lang['lang'] => $lang['name']);
290 * Check whether a language is right-to-left
292 * @param string $lang language code of the language to check
294 * @return boolean true if language is rtl
297 function is_rtl($lang_value)
299 foreach (common_config('site', 'languages') as $code => $info) {
300 if ($lang_value == $info['lang']) {
301 return $info['direction'] == 'rtl';
307 * Get a list of all languages that are enabled in the default config
309 * This should ONLY be called when setting up the default config in common.php.
310 * Any other attempt to get a list of languages should instead call
311 * common_config('site','languages')
313 * @return array mapping of language codes to language info
315 function get_all_languages()
318 'af' => ['q' => 0.8, 'lang' => 'af', 'name' => 'Afrikaans', 'direction' => 'ltr'],
319 'ar' => ['q' => 0.8, 'lang' => 'ar', 'name' => 'Arabic', 'direction' => 'rtl'],
320 'ast' => ['q' => 1, 'lang' => 'ast', 'name' => 'Asturian', 'direction' => 'ltr'],
321 'eu' => ['q' => 1, 'lang' => 'eu', 'name' => 'Basque', 'direction' => 'ltr'],
322 'be-tarask' => ['q' => 0.5, 'lang' => 'be-tarask', 'name' => 'Belarusian (Taraškievica orthography)', 'direction' => 'ltr'],
323 'br' => ['q' => 0.8, 'lang' => 'br', 'name' => 'Breton', 'direction' => 'ltr'],
324 'bg' => ['q' => 0.8, 'lang' => 'bg', 'name' => 'Bulgarian', 'direction' => 'ltr'],
325 'my' => ['q' => 1, 'lang' => 'my', 'name' => 'Burmese', 'direction' => 'ltr'],
326 'ca' => ['q' => 0.5, 'lang' => 'ca', 'name' => 'Catalan', 'direction' => 'ltr'],
327 'zh-cn' => ['q' => 0.9, 'lang' => 'zh_CN', 'name' => 'Chinese (Simplified)', 'direction' => 'ltr'],
328 'zh-hant' => ['q' => 0.2, 'lang' => 'zh_TW', 'name' => 'Chinese (Taiwanese)', 'direction' => 'ltr'],
329 'ksh' => ['q' => 1, 'lang' => 'ksh', 'name' => 'Colognian', 'direction' => 'ltr'],
330 'cs' => ['q' => 0.5, 'lang' => 'cs', 'name' => 'Czech', 'direction' => 'ltr'],
331 'da' => ['q' => 0.8, 'lang' => 'da', 'name' => 'Danish', 'direction' => 'ltr'],
332 'nl' => ['q' => 0.5, 'lang' => 'nl', 'name' => 'Dutch', 'direction' => 'ltr'],
333 'arz' => ['q' => 0.8, 'lang' => 'arz', 'name' => 'Egyptian Spoken Arabic', 'direction' => 'rtl'],
334 'en' => ['q' => 1, 'lang' => 'en', 'name' => 'English', 'direction' => 'ltr'],
335 'en-us' => ['q' => 1, 'lang' => 'en', 'name' => 'English (US)', 'direction' => 'ltr'],
336 'en-gb' => ['q' => 1, 'lang' => 'en_GB', 'name' => 'English (UK)', 'direction' => 'ltr'],
337 'eo' => ['q' => 0.8, 'lang' => 'eo', 'name' => 'Esperanto', 'direction' => 'ltr'],
338 'fi' => ['q' => 1, 'lang' => 'fi', 'name' => 'Finnish', 'direction' => 'ltr'],
339 'fr' => ['q' => 1, 'lang' => 'fr', 'name' => 'French', 'direction' => 'ltr'],
340 'fr-fr' => ['q' => 1, 'lang' => 'fr', 'name' => 'French (France)', 'direction' => 'ltr'],
341 'fur' => ['q' => 0.8, 'lang' => 'fur', 'name' => 'Friulian', 'direction' => 'ltr'],
342 'gl' => ['q' => 0.8, 'lang' => 'gl', 'name' => 'Galician', 'direction' => 'ltr'],
343 'ka' => ['q' => 0.8, 'lang' => 'ka', 'name' => 'Georgian', 'direction' => 'ltr'],
344 'de' => ['q' => 0.8, 'lang' => 'de', 'name' => 'German', 'direction' => 'ltr'],
345 'el' => ['q' => 0.1, 'lang' => 'el', 'name' => 'Greek', 'direction' => 'ltr'],
346 'he' => ['q' => 0.5, 'lang' => 'he', 'name' => 'Hebrew', 'direction' => 'rtl'],
347 'hu' => ['q' => 0.8, 'lang' => 'hu', 'name' => 'Hungarian', 'direction' => 'ltr'],
348 'is' => ['q' => 0.1, 'lang' => 'is', 'name' => 'Icelandic', 'direction' => 'ltr'],
349 'id' => ['q' => 1, 'lang' => 'id', 'name' => 'Indonesian', 'direction' => 'ltr'],
350 'ia' => ['q' => 0.8, 'lang' => 'ia', 'name' => 'Interlingua', 'direction' => 'ltr'],
351 'ga' => ['q' => 0.5, 'lang' => 'ga', 'name' => 'Irish', 'direction' => 'ltr'],
352 'it' => ['q' => 1, 'lang' => 'it', 'name' => 'Italian', 'direction' => 'ltr'],
353 'ja' => ['q' => 0.5, 'lang' => 'ja', 'name' => 'Japanese', 'direction' => 'ltr'],
354 'ko' => ['q' => 0.9, 'lang' => 'ko', 'name' => 'Korean', 'direction' => 'ltr'],
355 'lv' => ['q' => 1, 'lang' => 'lv', 'name' => 'Latvian', 'direction' => 'ltr'],
356 'lt' => ['q' => 1, 'lang' => 'lt', 'name' => 'Lithuanian', 'direction' => 'ltr'],
357 'lb' => ['q' => 1, 'lang' => 'lb', 'name' => 'Luxembourgish', 'direction' => 'ltr'],
358 'mk' => ['q' => 0.5, 'lang' => 'mk', 'name' => 'Macedonian', 'direction' => 'ltr'],
359 'mg' => ['q' => 1, 'lang' => 'mg', 'name' => 'Malagasy', 'direction' => 'ltr'],
360 'ms' => ['q' => 1, 'lang' => 'ms', 'name' => 'Malay', 'direction' => 'ltr'],
361 'ml' => ['q' => 0.5, 'lang' => 'ml', 'name' => 'Malayalam', 'direction' => 'ltr'],
362 'ne' => ['q' => 1, 'lang' => 'ne', 'name' => 'Nepali', 'direction' => 'ltr'],
363 'nb' => ['q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'],
364 'no' => ['q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'],
365 'nn' => ['q' => 1, 'lang' => 'nn', 'name' => 'Norwegian (Nynorsk)', 'direction' => 'ltr'],
366 'fa' => ['q' => 1, 'lang' => 'fa', 'name' => 'Persian', 'direction' => 'rtl'],
367 'pl' => ['q' => 0.5, 'lang' => 'pl', 'name' => 'Polish', 'direction' => 'ltr'],
368 'pt' => ['q' => 1, 'lang' => 'pt', 'name' => 'Portuguese', 'direction' => 'ltr'],
369 'pt-br' => ['q' => 0.9, 'lang' => 'pt_BR', 'name' => 'Brazilian Portuguese', 'direction' => 'ltr'],
370 'ru' => ['q' => 0.9, 'lang' => 'ru', 'name' => 'Russian', 'direction' => 'ltr'],
371 'sr-ec' => ['q' => 1, 'lang' => 'sr-ec', 'name' => 'Serbian', 'direction' => 'ltr'],
372 'es' => ['q' => 1, 'lang' => 'es', 'name' => 'Spanish', 'direction' => 'ltr'],
373 'sv' => ['q' => 0.8, 'lang' => 'sv', 'name' => 'Swedish', 'direction' => 'ltr'],
374 'tl' => ['q' => 0.8, 'lang' => 'tl', 'name' => 'Tagalog', 'direction' => 'ltr'],
375 'ta' => ['q' => 1, 'lang' => 'ta', 'name' => 'Tamil', 'direction' => 'ltr'],
376 'te' => ['q' => 0.3, 'lang' => 'te', 'name' => 'Telugu', 'direction' => 'ltr'],
377 'tr' => ['q' => 0.5, 'lang' => 'tr', 'name' => 'Turkish', 'direction' => 'ltr'],
378 'uk' => ['q' => 1, 'lang' => 'uk', 'name' => 'Ukrainian', 'direction' => 'ltr'],
379 'hsb' => ['q' => 0.8, 'lang' => 'hsb', 'name' => 'Upper Sorbian', 'direction' => 'ltr'],
380 'ur' => ['q' => 1, 'lang' => 'ur_PK', 'name' => 'Urdu (Pakistan)', 'direction' => 'rtl'],
381 'vi' => ['q' => 0.8, 'lang' => 'vi', 'name' => 'Vietnamese', 'direction' => 'ltr'],