2 // This file is part of GNU social - https://www.gnu.org/software/social
4 // GNU social is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // GNU social is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
18 * Utility functions for i18n
22 * @author Matthew Gregg <matthew.gregg@gmail.com>
23 * @author Ciaran Gultnieks <ciaran@ciarang.com>
24 * @author Evan Prodromou <evan@status.net>
25 * @author Diogo Cordeiro <diogo@fc.up.pt>
26 * @copyright 2010-2019 Free Software Foundation, Inc http://www.fsf.org
27 * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
30 defined('GNUSOCIAL') || die();
32 // Locale category constants are usually predefined, but may not be
33 // on some systems such as Win32.
34 $LC_CATEGORIES = ['LC_CTYPE',
41 foreach ($LC_CATEGORIES as $key => $name) {
42 if (!defined($name)) {
47 if (!function_exists('dpgettext')) {
49 * Context-aware dgettext wrapper; use when messages in different contexts
50 * won't be distinguished from the English source but need different translations.
51 * The context string will appear as msgctxt in the .po files.
53 * Not currently exposed in PHP's gettext module; implemented to be compat
54 * with gettext.h's macros.
56 * @param string $domain domain identifier
57 * @param string $context context identifier, should be some key like "menu|file"
58 * @param string $msg English source text
59 * @return string original or translated message
61 function dpgettext($domain, $context, $msg)
63 $msgid = $context . "\004" . $msg;
64 $out = dcgettext($domain, $msgid, LC_MESSAGES);
73 if (!function_exists('pgettext')) {
75 * Context-aware gettext wrapper; use when messages in different contexts
76 * won't be distinguished from the English source but need different translations.
77 * The context string will appear as msgctxt in the .po files.
79 * Not currently exposed in PHP's gettext module; implemented to be compat
80 * with gettext.h's macros.
82 * @param string $context context identifier, should be some key like "menu|file"
83 * @param string $msgid English source text
84 * @return string original or translated message
86 function pgettext($context, $msgid)
88 return dpgettext(textdomain(NULL), $context, $msgid);
92 if (!function_exists('dnpgettext')) {
94 * Context-aware dngettext wrapper; use when messages in different contexts
95 * won't be distinguished from the English source but need different translations.
96 * The context string will appear as msgctxt in the .po files.
98 * Not currently exposed in PHP's gettext module; implemented to be compat
99 * with gettext.h's macros.
101 * @param string $domain domain identifier
102 * @param string $context context identifier, should be some key like "menu|file"
103 * @param string $msg singular English source text
104 * @param string $plural plural English source text
105 * @param int $n number of items to control plural selection
106 * @return string original or translated message
108 function dnpgettext($domain, $context, $msg, $plural, $n)
110 $msgid = $context . "\004" . $msg;
111 $out = dcngettext($domain, $msgid, $plural, $n, LC_MESSAGES);
112 if ($out == $msgid) {
120 if (!function_exists('npgettext')) {
122 * Context-aware ngettext wrapper; use when messages in different contexts
123 * won't be distinguished from the English source but need different translations.
124 * The context string will appear as msgctxt in the .po files.
126 * Not currently exposed in PHP's gettext module; implemented to be compat
127 * with gettext.h's macros.
129 * @param string $context context identifier, should be some key like "menu|file"
130 * @param string $msgid singular English source text
131 * @param string $plural plural English source text
132 * @param int $n number of items to control plural selection
133 * @return string original or translated message
135 function npgettext($context, $msgid, $plural, $n)
137 return dnpgettext(textdomain(NULL), $msgid, $plural, $n, LC_MESSAGES);
142 * Shortcut for *gettext functions with smart domain detection.
144 * If calling from a plugin, this function checks which plugin was
145 * being called from and uses that as text domain, which will have
146 * been set up during plugin initialization.
148 * Also handles plurals and contexts depending on what parameters
151 * gettext -> _m($msg)
152 * ngettext -> _m($msg1, $msg2, $n)
153 * pgettext -> _m($ctx, $msg)
154 * npgettext -> _m($ctx, $msg1, $msg2, $n)
156 * @fixme may not work properly in eval'd code
162 function _m($msg/*, ...*/)
164 $domain = _mdomain(debug_backtrace());
165 $args = func_get_args();
166 switch (count($args)) {
168 return dgettext($domain, $msg);
170 return dpgettext($domain, $args[0], $args[1]);
172 return dngettext($domain, $args[0], $args[1], $args[2]);
174 return dnpgettext($domain, $args[0], $args[1], $args[2], $args[3]);
176 throw new Exception("Bad parameter count to _m()");
181 * Looks for which plugin we've been called from to set the gettext domain;
182 * if not in a plugin subdirectory, we'll use the default 'statusnet'.
184 * Note: we can't return null for default domain since most of the PHP gettext
185 * wrapper functions turn null into "" before passing to the backend library.
187 * @param array $backtrace debug_backtrace() output
190 * @fixme could explode if SN is under a 'plugins' folder or share name.
192 function _mdomain($backtrace)
197 'file' => string '/var/www/mublog/plugins/FeedSub/FeedSubPlugin.php' (length=49)
199 'function' => string '_m' (length=2)
202 0 => &string 'Feeds' (length=5)
205 $path = $backtrace[0]['file'];
206 if (!isset($cached[$path])) {
207 if (DIRECTORY_SEPARATOR !== '/') {
208 $path = strtr($path, DIRECTORY_SEPARATOR, '/');
210 $plug = strpos($path, '/plugins/');
211 if ($plug === false) {
212 // We're not in a plugin; return default domain.
213 $final = 'statusnet';
216 $cut2 = strpos($path, '/', $cut);
218 $final = substr($path, $cut, $cut2 - $cut);
220 // We might be running directly from the plugins dir?
221 // If so, there's no place to store locale info.
222 $final = 'statusnet';
225 $cached[$path] = $final;
227 return $cached[$path];
232 * Content negotiation for language codes
234 * @param $http_accept_lang_header string HTTP Accept-Language header
235 * @return string language code for best language match, false otherwise
238 function client_preferred_language($http_accept_lang_header)
242 $all_languages = common_config('site', 'languages');
244 preg_match_all('"(((\S\S)-?(\S\S)?)(;q=([0-9.]+))?)\s*(,\s*|$)"',
245 strtolower($http_accept_lang_header), $http_langs);
247 for ($i = 0; $i < count($http_langs); ++$i) {
248 if (!empty($http_langs[2][$i])) {
249 // if no q default to 1.0
250 $client_langs[$http_langs[2][$i]] =
251 ($http_langs[6][$i] ? (float)$http_langs[6][$i] : 1.0 - ($i * 0.01));
253 if (!empty($http_langs[3][$i]) && empty($client_langs[$http_langs[3][$i]])) {
254 // if a catchall default 0.01 lower
255 $client_langs[$http_langs[3][$i]] =
256 ($http_langs[6][$i] ? (float)$http_langs[6][$i] - 0.01 : 0.99);
259 // sort in descending q
260 arsort($client_langs);
262 foreach ($client_langs as $lang => $q) {
263 if (isset($all_languages[$lang])) {
264 return ($all_languages[$lang]['lang']);
271 * returns a simple code -> name mapping for languages
273 * @return array map of available languages by code to language name.
276 function get_nice_language_list()
280 $all_languages = common_config('site', 'languages');
282 foreach ($all_languages as $lang) {
283 $nice_lang = $nice_lang + array($lang['lang'] => $lang['name']);
289 * Check whether a language is right-to-left
291 * @param string $lang language code of the language to check
293 * @return boolean true if language is rtl
296 function is_rtl($lang_value)
298 foreach (common_config('site', 'languages') as $code => $info) {
299 if ($lang_value == $info['lang']) {
300 return $info['direction'] == 'rtl';
306 * Get a list of all languages that are enabled in the default config
308 * This should ONLY be called when setting up the default config in common.php.
309 * Any other attempt to get a list of languages should instead call
310 * common_config('site','languages')
312 * @return array mapping of language codes to language info
314 function get_all_languages()
317 'af' => ['q' => 0.8, 'lang' => 'af', 'name' => 'Afrikaans', 'direction' => 'ltr'],
318 'ar' => ['q' => 0.8, 'lang' => 'ar', 'name' => 'Arabic', 'direction' => 'rtl'],
319 'ast' => ['q' => 1, 'lang' => 'ast', 'name' => 'Asturian', 'direction' => 'ltr'],
320 'eu' => ['q' => 1, 'lang' => 'eu', 'name' => 'Basque', 'direction' => 'ltr'],
321 'be-tarask' => ['q' => 0.5, 'lang' => 'be-tarask', 'name' => 'Belarusian (Taraškievica orthography)', 'direction' => 'ltr'],
322 'br' => ['q' => 0.8, 'lang' => 'br', 'name' => 'Breton', 'direction' => 'ltr'],
323 'bg' => ['q' => 0.8, 'lang' => 'bg', 'name' => 'Bulgarian', 'direction' => 'ltr'],
324 'my' => ['q' => 1, 'lang' => 'my', 'name' => 'Burmese', 'direction' => 'ltr'],
325 'ca' => ['q' => 0.5, 'lang' => 'ca', 'name' => 'Catalan', 'direction' => 'ltr'],
326 'zh-cn' => ['q' => 0.9, 'lang' => 'zh_CN', 'name' => 'Chinese (Simplified)', 'direction' => 'ltr'],
327 'zh-hant' => ['q' => 0.2, 'lang' => 'zh_TW', 'name' => 'Chinese (Taiwanese)', 'direction' => 'ltr'],
328 'ksh' => ['q' => 1, 'lang' => 'ksh', 'name' => 'Colognian', 'direction' => 'ltr'],
329 'cs' => ['q' => 0.5, 'lang' => 'cs', 'name' => 'Czech', 'direction' => 'ltr'],
330 'da' => ['q' => 0.8, 'lang' => 'da', 'name' => 'Danish', 'direction' => 'ltr'],
331 'nl' => ['q' => 0.5, 'lang' => 'nl', 'name' => 'Dutch', 'direction' => 'ltr'],
332 'arz' => ['q' => 0.8, 'lang' => 'arz', 'name' => 'Egyptian Spoken Arabic', 'direction' => 'rtl'],
333 'en' => ['q' => 1, 'lang' => 'en', 'name' => 'English', 'direction' => 'ltr'],
334 'en-us' => ['q' => 1, 'lang' => 'en', 'name' => 'English (US)', 'direction' => 'ltr'],
335 'en-gb' => ['q' => 1, 'lang' => 'en_GB', 'name' => 'English (UK)', 'direction' => 'ltr'],
336 'eo' => ['q' => 0.8, 'lang' => 'eo', 'name' => 'Esperanto', 'direction' => 'ltr'],
337 'fi' => ['q' => 1, 'lang' => 'fi', 'name' => 'Finnish', 'direction' => 'ltr'],
338 'fr' => ['q' => 1, 'lang' => 'fr', 'name' => 'French', 'direction' => 'ltr'],
339 'fr-fr' => ['q' => 1, 'lang' => 'fr', 'name' => 'French (France)', 'direction' => 'ltr'],
340 'fur' => ['q' => 0.8, 'lang' => 'fur', 'name' => 'Friulian', 'direction' => 'ltr'],
341 'gl' => ['q' => 0.8, 'lang' => 'gl', 'name' => 'Galician', 'direction' => 'ltr'],
342 'ka' => ['q' => 0.8, 'lang' => 'ka', 'name' => 'Georgian', 'direction' => 'ltr'],
343 'de' => ['q' => 0.8, 'lang' => 'de', 'name' => 'German', 'direction' => 'ltr'],
344 'el' => ['q' => 0.1, 'lang' => 'el', 'name' => 'Greek', 'direction' => 'ltr'],
345 'he' => ['q' => 0.5, 'lang' => 'he', 'name' => 'Hebrew', 'direction' => 'rtl'],
346 'hu' => ['q' => 0.8, 'lang' => 'hu', 'name' => 'Hungarian', 'direction' => 'ltr'],
347 'is' => ['q' => 0.1, 'lang' => 'is', 'name' => 'Icelandic', 'direction' => 'ltr'],
348 'id' => ['q' => 1, 'lang' => 'id', 'name' => 'Indonesian', 'direction' => 'ltr'],
349 'ia' => ['q' => 0.8, 'lang' => 'ia', 'name' => 'Interlingua', 'direction' => 'ltr'],
350 'ga' => ['q' => 0.5, 'lang' => 'ga', 'name' => 'Irish', 'direction' => 'ltr'],
351 'it' => ['q' => 1, 'lang' => 'it', 'name' => 'Italian', 'direction' => 'ltr'],
352 'ja' => ['q' => 0.5, 'lang' => 'ja', 'name' => 'Japanese', 'direction' => 'ltr'],
353 'ko' => ['q' => 0.9, 'lang' => 'ko', 'name' => 'Korean', 'direction' => 'ltr'],
354 'lv' => ['q' => 1, 'lang' => 'lv', 'name' => 'Latvian', 'direction' => 'ltr'],
355 'lt' => ['q' => 1, 'lang' => 'lt', 'name' => 'Lithuanian', 'direction' => 'ltr'],
356 'lb' => ['q' => 1, 'lang' => 'lb', 'name' => 'Luxembourgish', 'direction' => 'ltr'],
357 'mk' => ['q' => 0.5, 'lang' => 'mk', 'name' => 'Macedonian', 'direction' => 'ltr'],
358 'mg' => ['q' => 1, 'lang' => 'mg', 'name' => 'Malagasy', 'direction' => 'ltr'],
359 'ms' => ['q' => 1, 'lang' => 'ms', 'name' => 'Malay', 'direction' => 'ltr'],
360 'ml' => ['q' => 0.5, 'lang' => 'ml', 'name' => 'Malayalam', 'direction' => 'ltr'],
361 'ne' => ['q' => 1, 'lang' => 'ne', 'name' => 'Nepali', 'direction' => 'ltr'],
362 'nb' => ['q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'],
363 'no' => ['q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'],
364 'nn' => ['q' => 1, 'lang' => 'nn', 'name' => 'Norwegian (Nynorsk)', 'direction' => 'ltr'],
365 'fa' => ['q' => 1, 'lang' => 'fa', 'name' => 'Persian', 'direction' => 'rtl'],
366 'pl' => ['q' => 0.5, 'lang' => 'pl', 'name' => 'Polish', 'direction' => 'ltr'],
367 'pt' => ['q' => 1, 'lang' => 'pt', 'name' => 'Portuguese', 'direction' => 'ltr'],
368 'pt-br' => ['q' => 0.9, 'lang' => 'pt_BR', 'name' => 'Brazilian Portuguese', 'direction' => 'ltr'],
369 'ru' => ['q' => 0.9, 'lang' => 'ru', 'name' => 'Russian', 'direction' => 'ltr'],
370 'sr-ec' => ['q' => 1, 'lang' => 'sr-ec', 'name' => 'Serbian', 'direction' => 'ltr'],
371 'es' => ['q' => 1, 'lang' => 'es', 'name' => 'Spanish', 'direction' => 'ltr'],
372 'sv' => ['q' => 0.8, 'lang' => 'sv', 'name' => 'Swedish', 'direction' => 'ltr'],
373 'tl' => ['q' => 0.8, 'lang' => 'tl', 'name' => 'Tagalog', 'direction' => 'ltr'],
374 'ta' => ['q' => 1, 'lang' => 'ta', 'name' => 'Tamil', 'direction' => 'ltr'],
375 'te' => ['q' => 0.3, 'lang' => 'te', 'name' => 'Telugu', 'direction' => 'ltr'],
376 'tr' => ['q' => 0.5, 'lang' => 'tr', 'name' => 'Turkish', 'direction' => 'ltr'],
377 'uk' => ['q' => 1, 'lang' => 'uk', 'name' => 'Ukrainian', 'direction' => 'ltr'],
378 'hsb' => ['q' => 0.8, 'lang' => 'hsb', 'name' => 'Upper Sorbian', 'direction' => 'ltr'],
379 'ur' => ['q' => 1, 'lang' => 'ur_PK', 'name' => 'Urdu (Pakistan)', 'direction' => 'rtl'],
380 'vi' => ['q' => 0.8, 'lang' => 'vi', 'name' => 'Vietnamese', 'direction' => 'ltr'],