]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/language.php
[DATABASE][User_openid_prefs] Fix wrong type for modified column
[quix0rs-gnu-social.git] / lib / language.php
1 <?php
2 /**
3  * GNU social - a federating social network
4  *
5  * utility functions for i18n
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  * @category I18n
21  * @package  GNU social
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/
27  */
28
29 if (!defined('STATUSNET') && !defined('LACONICA')) {
30     exit(1);
31 }
32
33 // Locale category constants are usually predefined, but may not be
34 // on some systems such as Win32.
35 $LC_CATEGORIES = ['LC_CTYPE',
36                   'LC_NUMERIC',
37                   'LC_TIME',
38                   'LC_COLLATE',
39                   'LC_MONETARY',
40                   'LC_MESSAGES',
41                   'LC_ALL'];
42 foreach ($LC_CATEGORIES as $key => $name) {
43     if (!defined($name)) {
44         define($name, $key);
45     }
46 }
47
48 if (!function_exists('dpgettext')) {
49     /**
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.
53      *
54      * Not currently exposed in PHP's gettext module; implemented to be compat
55      * with gettext.h's macros.
56      *
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
61      */
62     function dpgettext($domain, $context, $msg)
63     {
64         $msgid = $context . "\004" . $msg;
65         $out = dcgettext($domain, $msgid, LC_MESSAGES);
66         if ($out == $msgid) {
67             return $msg;
68         } else {
69             return $out;
70         }
71     }
72 }
73
74 if (!function_exists('pgettext')) {
75     /**
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.
79      *
80      * Not currently exposed in PHP's gettext module; implemented to be compat
81      * with gettext.h's macros.
82      *
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
86      */
87     function pgettext($context, $msgid)
88     {
89         return dpgettext(textdomain(NULL), $context, $msgid);
90     }
91 }
92
93 if (!function_exists('dnpgettext')) {
94     /**
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.
98      *
99      * Not currently exposed in PHP's gettext module; implemented to be compat
100      * with gettext.h's macros.
101      *
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
108      */
109     function dnpgettext($domain, $context, $msg, $plural, $n)
110     {
111         $msgid = $context . "\004" . $msg;
112         $out = dcngettext($domain, $msgid, $plural, $n, LC_MESSAGES);
113         if ($out == $msgid) {
114             return $msg;
115         } else {
116             return $out;
117         }
118     }
119 }
120
121 if (!function_exists('npgettext')) {
122     /**
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.
126      *
127      * Not currently exposed in PHP's gettext module; implemented to be compat
128      * with gettext.h's macros.
129      *
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
135      */
136     function npgettext($context, $msgid, $plural, $n)
137     {
138         return dnpgettext(textdomain(NULL), $msgid, $plural, $n, LC_MESSAGES);
139     }
140 }
141
142 /**
143  * Shortcut for *gettext functions with smart domain detection.
144  *
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.
148  *
149  * Also handles plurals and contexts depending on what parameters
150  * are passed to it:
151  *
152  *   gettext -> _m($msg)
153  *  ngettext -> _m($msg1, $msg2, $n)
154  *  pgettext -> _m($ctx, $msg)
155  * npgettext -> _m($ctx, $msg1, $msg2, $n)
156  *
157  * @fixme may not work properly in eval'd code
158  *
159  * @param string $msg
160  * @return string
161  * @throws Exception
162  */
163 function _m($msg/*, ...*/)
164 {
165     $domain = _mdomain(debug_backtrace());
166     $args = func_get_args();
167     switch (count($args)) {
168         case 1:
169             return dgettext($domain, $msg);
170         case 2:
171             return dpgettext($domain, $args[0], $args[1]);
172         case 3:
173             return dngettext($domain, $args[0], $args[1], $args[2]);
174         case 4:
175             return dnpgettext($domain, $args[0], $args[1], $args[2], $args[3]);
176         default:
177             throw new Exception("Bad parameter count to _m()");
178     }
179 }
180
181 /**
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'.
184  *
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.
187  *
188  * @param array $backtrace debug_backtrace() output
189  * @return string
190  * @private
191  * @fixme could explode if SN is under a 'plugins' folder or share name.
192  */
193 function _mdomain($backtrace)
194 {
195     /*
196       0 =>
197         array
198           'file' => string '/var/www/mublog/plugins/FeedSub/FeedSubPlugin.php' (length=49)
199           'line' => int 77
200           'function' => string '_m' (length=2)
201           'args' =>
202             array
203               0 => &string 'Feeds' (length=5)
204     */
205     static $cached;
206     $path = $backtrace[0]['file'];
207     if (!isset($cached[$path])) {
208         if (DIRECTORY_SEPARATOR !== '/') {
209             $path = strtr($path, DIRECTORY_SEPARATOR, '/');
210         }
211         $plug = strpos($path, '/plugins/');
212         if ($plug === false) {
213             // We're not in a plugin; return default domain.
214             $final = 'statusnet';
215         } else {
216             $cut = $plug + 9;
217             $cut2 = strpos($path, '/', $cut);
218             if ($cut2) {
219                 $final = substr($path, $cut, $cut2 - $cut);
220             } else {
221                 // We might be running directly from the plugins dir?
222                 // If so, there's no place to store locale info.
223                 $final = 'statusnet';
224             }
225         }
226         $cached[$path] = $final;
227     }
228     return $cached[$path];
229 }
230
231
232 /**
233  * Content negotiation for language codes
234  *
235  * @param $http_accept_lang_header string HTTP Accept-Language header
236  * @return string language code for best language match, false otherwise
237  */
238
239 function client_preferred_language($http_accept_lang_header)
240 {
241     $client_langs = [];
242
243     $all_languages = common_config('site', 'languages');
244
245     preg_match_all('"(((\S\S)-?(\S\S)?)(;q=([0-9.]+))?)\s*(,\s*|$)"',
246         strtolower($http_accept_lang_header), $http_langs);
247
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));
253         }
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);
258         }
259     }
260     // sort in descending q
261     arsort($client_langs);
262
263     foreach ($client_langs as $lang => $q) {
264         if (isset($all_languages[$lang])) {
265             return ($all_languages[$lang]['lang']);
266         }
267     }
268     return false;
269 }
270
271 /**
272  * returns a simple code -> name mapping for languages
273  *
274  * @return array map of available languages by code to language name.
275  */
276
277 function get_nice_language_list()
278 {
279     $nice_lang = [];
280
281     $all_languages = common_config('site', 'languages');
282
283     foreach ($all_languages as $lang) {
284         $nice_lang = $nice_lang + array($lang['lang'] => $lang['name']);
285     }
286     return $nice_lang;
287 }
288
289 /*
290  * Check whether a language is right-to-left
291  *
292  * @param string $lang language code of the language to check
293  *
294  * @return boolean true if language is rtl
295  */
296
297 function is_rtl($lang_value)
298 {
299         foreach (common_config('site', 'languages') as $code => $info) {
300                 if ($lang_value == $info['lang']) {
301                         return $info['direction'] == 'rtl';
302                 }
303         }
304 }
305
306 /**
307  * Get a list of all languages that are enabled in the default config
308  *
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')
312  *
313  * @return array mapping of language codes to language info
314  */
315 function get_all_languages()
316 {
317     return [
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'],
382     ];
383 }