]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/language.php
Introduced common_location_shared() to check if location sharing is always,
[quix0rs-gnu-social.git] / lib / language.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * utility functions for i18n
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category I18n
23  * @package  StatusNet
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/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 // Locale category constants are usually predefined, but may not be
36 // on some systems such as Win32.
37 $LC_CATEGORIES = array('LC_CTYPE',
38                        'LC_NUMERIC',
39                        'LC_TIME',
40                        'LC_COLLATE',
41                        'LC_MONETARY',
42                        'LC_MESSAGES',
43                        'LC_ALL');
44 foreach ($LC_CATEGORIES as $key => $name) {
45     if (!defined($name)) {
46         define($name, $key);
47     }
48 }
49
50 if (!function_exists('gettext')) {
51     require_once("php-gettext/gettext.inc");
52 }
53
54
55 if (!function_exists('dpgettext')) {
56     /**
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.
60      *
61      * Not currently exposed in PHP's gettext module; implemented to be compat
62      * with gettext.h's macros.
63      *
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
68      */
69     function dpgettext($domain, $context, $msg)
70     {
71         $msgid = $context . "\004" . $msg;
72         $out = dcgettext($domain, $msgid, LC_MESSAGES);
73         if ($out == $msgid) {
74             return $msg;
75         } else {
76             return $out;
77         }
78     }
79 }
80
81 if (!function_exists('pgettext')) {
82     /**
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.
86      *
87      * Not currently exposed in PHP's gettext module; implemented to be compat
88      * with gettext.h's macros.
89      *
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
93      */
94     function pgettext($context, $msg)
95     {
96         return dpgettext(textdomain(NULL), $context, $msg);
97     }
98 }
99
100 if (!function_exists('dnpgettext')) {
101     /**
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.
105      *
106      * Not currently exposed in PHP's gettext module; implemented to be compat
107      * with gettext.h's macros.
108      *
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
115      */
116     function dnpgettext($domain, $context, $msg, $plural, $n)
117     {
118         $msgid = $context . "\004" . $msg;
119         $out = dcngettext($domain, $msgid, $plural, $n, LC_MESSAGES);
120         if ($out == $msgid) {
121             return $msg;
122         } else {
123             return $out;
124         }
125     }
126 }
127
128 if (!function_exists('npgettext')) {
129     /**
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.
133      *
134      * Not currently exposed in PHP's gettext module; implemented to be compat
135      * with gettext.h's macros.
136      *
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
142      */
143     function npgettext($context, $msg, $plural, $n)
144     {
145         return dnpgettext(textdomain(NULL), $msgid, $plural, $n, LC_MESSAGES);
146     }
147 }
148
149 /**
150  * Shortcut for *gettext functions with smart domain detection.
151  *
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.
155  *
156  * Also handles plurals and contexts depending on what parameters
157  * are passed to it:
158  *
159  *   gettext -> _m($msg)
160  *  ngettext -> _m($msg1, $msg2, $n)
161  *  pgettext -> _m($ctx, $msg)
162  * npgettext -> _m($ctx, $msg1, $msg2, $n)
163  *
164  * @fixme may not work properly in eval'd code
165  *
166  * @param string $msg
167  * @return string
168  */
169 function _m($msg/*, ...*/)
170 {
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()");
179     }
180 }
181
182 /**
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'.
185  *
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.
188  *
189  * @param array $backtrace debug_backtrace() output
190  * @return string
191  * @private
192  * @fixme could explode if SN is under a 'plugins' folder or share name.
193  */
194 function _mdomain($backtrace)
195 {
196     /*
197       0 =>
198         array
199           'file' => string '/var/www/mublog/plugins/FeedSub/FeedSubPlugin.php' (length=49)
200           'line' => int 77
201           'function' => string '_m' (length=2)
202           'args' =>
203             array
204               0 => &string 'Feeds' (length=5)
205     */
206     static $cached;
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, '/');
212         }
213         $plug = strpos($path, '/plugins/');
214         if ($plug === false) {
215             // We're not in a plugin; return default domain.
216             $final = 'statusnet';
217         } else {
218             $cut = $plug + 9;
219             $cut2 = strpos($path, '/', $cut);
220             if ($cut2) {
221                 $final = substr($path, $cut, $cut2 - $cut);
222             } else {
223                 // We might be running directly from the plugins dir?
224                 // If so, there's no place to store locale info.
225                 $final = 'statusnet';
226             }
227         }
228         $cached[$path] = $final;
229     }
230     return $cached[$path];
231 }
232
233
234 /**
235  * Content negotiation for language codes
236  *
237  * @param string $httplang HTTP Accept-Language header
238  *
239  * @return string language code for best language match
240  */
241
242 function client_prefered_language($httplang)
243 {
244     $client_langs = array();
245
246     $all_languages = common_config('site', 'languages');
247
248     preg_match_all('"(((\S\S)-?(\S\S)?)(;q=([0-9.]+))?)\s*(,\s*|$)"',
249                    strtolower($httplang), $httplang);
250
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));
256         }
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);
261         }
262     }
263     // sort in decending q
264     arsort($client_langs);
265
266     foreach ($client_langs as $lang => $q) {
267         if (isset($all_languages[$lang])) {
268             return($all_languages[$lang]['lang']);
269         }
270     }
271     return false;
272 }
273
274 /**
275  * returns a simple code -> name mapping for languages
276  *
277  * @return array map of available languages by code to language name.
278  */
279
280 function get_nice_language_list()
281 {
282     $nice_lang = array();
283
284     $all_languages = common_config('site', 'languages');
285
286     foreach ($all_languages as $lang) {
287         $nice_lang = $nice_lang + array($lang['lang'] => $lang['name']);
288     }
289     return $nice_lang;
290 }
291
292 /*
293  * Check whether a language is right-to-left
294  *
295  * @param string $lang language code of the language to check
296  *
297  * @return boolean true if language is rtl
298  */
299
300 function is_rtl($lang)
301 {
302     $all_languages = common_config('site', 'languages');
303     $lang = $all_languages[$lang];
304     return ($lang['direction'] == 'rtl');
305 }
306
307 /**
308  * Get a list of all languages that are enabled in the default config
309  *
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')
313  *
314  * @return array mapping of language codes to language info
315  */
316 function get_all_languages() {
317     return array(
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         'ast'      => array('q' => 1, 'lang' => 'ast', 'name' => 'Asturian', 'direction' => 'ltr'),        
321         'eu'      => array('q' => 1, 'lang' => 'eu',    'name' => 'Basque', 'direction' => 'ltr'),
322         'be-tarask' => array('q' => 0.5, 'lang' => 'be-tarask', 'name' => 'Belarusian (Taraškievica orthography)', 'direction' => 'ltr'),
323         'br'      => array('q' => 0.8, 'lang' => 'br', 'name' => 'Breton', 'direction' => 'ltr'),
324         'bg'      => array('q' => 0.8, 'lang' => 'bg', 'name' => 'Bulgarian', 'direction' => 'ltr'),
325         'my'      => array('q' => 1, 'lang' => 'my', 'name' => 'Burmese', 'direction' => 'ltr'),        
326         'ca'      => array('q' => 0.5, 'lang' => 'ca', 'name' => 'Catalan', 'direction' => 'ltr'),
327         'zh-cn'   => array('q' => 0.9, 'lang' => 'zh_CN', 'name' => 'Chinese (Simplified)', 'direction' => 'ltr'),
328         'zh-hant' => array('q' => 0.2, 'lang' => 'zh_TW', 'name' => 'Chinese (Taiwanese)', 'direction' => 'ltr'),
329         'ksh'      => array('q' => 1, 'lang' => 'ksh', 'name' => 'Colognian', 'direction' => 'ltr'),
330         'cs'      => array('q' => 0.5, 'lang' => 'cs', 'name' => 'Czech', 'direction' => 'ltr'),        
331         'da'      => array('q' => 0.8, 'lang' => 'da', 'name' => 'Danish', 'direction' => 'ltr'),
332         'nl'      => array('q' => 0.5, 'lang' => 'nl', 'name' => 'Dutch', 'direction' => 'ltr'),        
333         'arz'     => array('q' => 0.8, 'lang' => 'arz', 'name' => 'Egyptian Spoken Arabic', 'direction' => 'rtl'),
334         'en'      => array('q' => 1, 'lang' => 'en',    'name' => 'English', 'direction' => 'ltr'),
335         'en-us'   => array('q' => 1, 'lang' => 'en', 'name' => 'English (US)', 'direction' => 'ltr'),
336         'en-gb'   => array('q' => 1, 'lang' => 'en_GB', 'name' => 'English (British)', 'direction' => 'ltr'),
337         'eo'      => array('q' => 0.8, 'lang' => 'eo',    'name' => 'Esperanto', 'direction' => 'ltr'),        
338         'fi'      => array('q' => 1, 'lang' => 'fi', 'name' => 'Finnish', 'direction' => 'ltr'),
339         'fr'      => array('q' => 1, 'lang' => 'fr', 'name' => 'French', 'direction' => 'ltr'),
340         'fr-fr'   => array('q' => 1, 'lang' => 'fr', 'name' => 'French (France)', 'direction' => 'ltr'),
341         'fur'     => array('q' => 0.8, 'lang' => 'fur', 'name' => 'Friulian', 'direction' => 'ltr'),                       
342         'gl'      => array('q' => 0.8, 'lang' => 'gl', 'name' => 'Galician', 'direction' => 'ltr'),
343         'ka'      => array('q' => 0.8, 'lang' => 'ka',    'name' => 'Georgian', 'direction' => 'ltr'),
344         'de'      => array('q' => 0.8, 'lang' => 'de', 'name' => 'German', 'direction' => 'ltr'),
345         'el'      => array('q' => 0.1, 'lang' => 'el',    'name' => 'Greek', 'direction' => 'ltr'),        
346         'he'      => array('q' => 0.5, 'lang' => 'he', 'name' => 'Hebrew', 'direction' => 'rtl'),
347         'hu'      => array('q' => 0.8, 'lang' => 'hu', 'name' => 'Hungarian', 'direction' => 'ltr'),        
348         'is'      => array('q' => 0.1, 'lang' => 'is', 'name' => 'Icelandic', 'direction' => 'ltr'),
349         'id'      => array('q' => 1, 'lang' => 'id', 'name' => 'Indonesian', 'direction' => 'ltr'),
350         'ia'      => array('q' => 0.8, 'lang' => 'ia', 'name' => 'Interlingua', 'direction' => 'ltr'),
351         'ga'      => array('q' => 0.5, 'lang' => 'ga', 'name' => 'Irish', 'direction' => 'ltr'),
352         'it'      => array('q' => 1, 'lang' => 'it', 'name' => 'Italian', 'direction' => 'ltr'),        
353         'jp'      => array('q' => 0.5, 'lang' => 'ja', 'name' => 'Japanese', 'direction' => 'ltr'),        
354         'ko'      => array('q' => 0.9, 'lang' => 'ko',    'name' => 'Korean', 'direction' => 'ltr'),        
355         'lv'      => array('q' => 1, 'lang' => 'lv', 'name' => 'Latvian', 'direction' => 'ltr'),
356         'lt'      => array('q' => 1, 'lang' => 'lt', 'name' => 'Lithuanian', 'direction' => 'ltr'),
357         'lb'      => array('q' => 1, 'lang' => 'lb', 'name' => 'Luxembourgish', 'direction' => 'ltr'),
358         'mk'      => array('q' => 0.5, 'lang' => 'mk', 'name' => 'Macedonian', 'direction' => 'ltr'),
359         'mg'      => array('q' => 1, 'lang' => 'mg', 'name' => 'Malagasy', 'direction' => 'ltr'),
360         'ms'      => array('q' => 1, 'lang' => 'ms', 'name' => 'Malay', 'direction' => 'ltr'),
361         'ml'      => array('q' => 0.5, 'lang' => 'ml', 'name' => 'Malayalam', 'direction' => 'ltr'),
362         'ne'      => array('q' => 1, 'lang' => 'ne', 'name' => 'Nepali', 'direction' => 'ltr'),
363         'nb'      => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
364         'no'      => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
365         'nn'      => array('q' => 1, 'lang' => 'nn', 'name' => 'Norwegian (Nynorsk)', 'direction' => 'ltr'),
366         'fa'      => array('q' => 1, 'lang' => 'fa', 'name' => 'Persian', 'direction' => 'rtl'),
367         'pl'      => array('q' => 0.5, 'lang' => 'pl', 'name' => 'Polish', 'direction' => 'ltr'),
368         'pt'      => array('q' => 0.1, 'lang' => 'pt',    'name' => 'Portuguese', 'direction' => 'ltr'),
369         'pt-br'   => array('q' => 0.9, 'lang' => 'pt_BR', 'name' => 'Portuguese Brazil', 'direction' => 'ltr'),
370         'ru'      => array('q' => 0.9, 'lang' => 'ru', 'name' => 'Russian', 'direction' => 'ltr'),
371         'sr-ec'      => array('q' => 1, 'lang' => 'sr-ec', 'name' => 'Serbian', 'direction' => 'ltr'),
372         'es'      => array('q' => 1, 'lang' => 'es',    'name' => 'Spanish', 'direction' => 'ltr'),
373         'sv'      => array('q' => 0.8, 'lang' => 'sv', 'name' => 'Swedish', 'direction' => 'ltr'),
374         'tl'      => array('q' => 0.8, 'lang' => 'tl', 'name' => 'Tagalog', 'direction' => 'ltr'),
375         'ta'      => array('q' => 1, 'lang' => 'ta', 'name' => 'Tamil', 'direction' => 'ltr'),
376         'te'      => array('q' => 0.3, 'lang' => 'te', 'name' => 'Telugu', 'direction' => 'ltr'),
377         'tr'      => array('q' => 0.5, 'lang' => 'tr', 'name' => 'Turkish', 'direction' => 'ltr'),
378         'uk'      => array('q' => 1, 'lang' => 'uk', 'name' => 'Ukrainian', 'direction' => 'ltr'),
379         'hsb'     => array('q' => 0.8, 'lang' => 'hsb', 'name' => 'Upper Sorbian', 'direction' => 'ltr'),
380         'ur_PK'      => array('q' => 1, 'lang' => 'ur', 'name' => 'Urdu (Pakistan)', 'direction' => 'rtl'),
381         'vi'      => array('q' => 0.8, 'lang' => 'vi', 'name' => 'Vietnamese', 'direction' => 'ltr'),    
382     );
383 }