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