]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/language.php
Fix for _m() usage with context in StatusNet main code.
[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, or null for default domain
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, or null for default domain
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  *
185  * @param array $backtrace debug_backtrace() output
186  * @return string
187  * @private
188  * @fixme could explode if SN is under a 'plugins' folder or share name.
189  */
190 function _mdomain($backtrace)
191 {
192     /*
193       0 => 
194         array
195           'file' => string '/var/www/mublog/plugins/FeedSub/FeedSubPlugin.php' (length=49)
196           'line' => int 77
197           'function' => string '_m' (length=2)
198           'args' => 
199             array
200               0 => &string 'Feeds' (length=5)
201     */
202     static $cached;
203     $path = $backtrace[0]['file'];
204     if (!isset($cached[$path])) {
205         $final = 'statusnet'; // assume default domain
206         if (DIRECTORY_SEPARATOR !== '/') {
207             $path = strtr($path, DIRECTORY_SEPARATOR, '/');
208         }
209         $cut = strpos($path, '/plugins/');
210         if ($cut) {
211             $cut += strlen('/plugins/');
212             $cut2 = strpos($path, '/', $cut);
213             if ($cut && $cut2) {
214                 $final = substr($path, $cut, $cut2 - $cut);
215             }
216         }
217         $cached[$path] = $final;
218     }
219     return $cached[$path];
220 }
221
222
223 /**
224  * Content negotiation for language codes
225  *
226  * @param string $httplang HTTP Accept-Language header
227  *
228  * @return string language code for best language match
229  */
230
231 function client_prefered_language($httplang)
232 {
233     $client_langs = array();
234
235     $all_languages = common_config('site', 'languages');
236
237     preg_match_all('"(((\S\S)-?(\S\S)?)(;q=([0-9.]+))?)\s*(,\s*|$)"',
238                    strtolower($httplang), $httplang);
239
240     for ($i = 0; $i < count($httplang); $i++) {
241         if (!empty($httplang[2][$i])) {
242             // if no q default to 1.0
243             $client_langs[$httplang[2][$i]] =
244               ($httplang[6][$i]? (float) $httplang[6][$i] : 1.0 - ($i*0.01));
245         }
246         if (!empty($httplang[3][$i]) && empty($client_langs[$httplang[3][$i]])) {
247             // if a catchall default 0.01 lower
248             $client_langs[$httplang[3][$i]] =
249               ($httplang[6][$i]? (float) $httplang[6][$i]-0.01 : 0.99);
250         }
251     }
252     // sort in decending q
253     arsort($client_langs);
254
255     foreach ($client_langs as $lang => $q) {
256         if (isset($all_languages[$lang])) {
257             return($all_languages[$lang]['lang']);
258         }
259     }
260     return false;
261 }
262
263 /**
264  * returns a simple code -> name mapping for languages
265  *
266  * @return array map of available languages by code to language name.
267  */
268
269 function get_nice_language_list()
270 {
271     $nice_lang = array();
272
273     $all_languages = common_config('site', 'languages');
274
275     foreach ($all_languages as $lang) {
276         $nice_lang = $nice_lang + array($lang['lang'] => $lang['name']);
277     }
278     return $nice_lang;
279 }
280
281 /**
282  * Get a list of all languages that are enabled in the default config
283  *
284  * This should ONLY be called when setting up the default config in common.php.
285  * Any other attempt to get a list of languages should instead call
286  * common_config('site','languages')
287  *
288  * @return array mapping of language codes to language info
289  */
290 function get_all_languages() {
291     return array(
292         'ar'      => array('q' => 0.8, 'lang' => 'ar', 'name' => 'Arabic', 'direction' => 'rtl'),
293         'arz'     => array('q' => 0.8, 'lang' => 'arz', 'name' => 'Egyptian Spoken Arabic', 'direction' => 'rtl'),
294         'bg'      => array('q' => 0.8, 'lang' => 'bg', 'name' => 'Bulgarian', 'direction' => 'ltr'),
295         'br'      => array('q' => 0.8, 'lang' => 'br', 'name' => 'Breton', 'direction' => 'ltr'),
296         'ca'      => array('q' => 0.5, 'lang' => 'ca', 'name' => 'Catalan', 'direction' => 'ltr'),
297         'cs'      => array('q' => 0.5, 'lang' => 'cs', 'name' => 'Czech', 'direction' => 'ltr'),
298         'de'      => array('q' => 0.8, 'lang' => 'de', 'name' => 'German', 'direction' => 'ltr'),
299         'el'      => array('q' => 0.1, 'lang' => 'el',    'name' => 'Greek', 'direction' => 'ltr'),
300         'en-us'   => array('q' => 1, 'lang' => 'en', 'name' => 'English (US)', 'direction' => 'ltr'),
301         'en-gb'   => array('q' => 1, 'lang' => 'en_GB', 'name' => 'English (British)', 'direction' => 'ltr'),
302         'en'      => array('q' => 1, 'lang' => 'en',    'name' => 'English (US)', 'direction' => 'ltr'),
303         'es'      => array('q' => 1, 'lang' => 'es',    'name' => 'Spanish', 'direction' => 'ltr'),
304         'fi'      => array('q' => 1, 'lang' => 'fi', 'name' => 'Finnish', 'direction' => 'ltr'),
305         'fa'      => array('q' => 1, 'lang' => 'fa', 'name' => 'Persian', 'direction' => 'rtl'),
306         'fr-fr'   => array('q' => 1, 'lang' => 'fr', 'name' => 'French', 'direction' => 'ltr'),
307         'ga'      => array('q' => 0.5, 'lang' => 'ga', 'name' => 'Galician', 'direction' => 'ltr'),
308         'he'      => array('q' => 0.5, 'lang' => 'he', 'name' => 'Hebrew', 'direction' => 'rtl'),
309         'hsb'     => array('q' => 0.8, 'lang' => 'hsb', 'name' => 'Upper Sorbian', 'direction' => 'ltr'),
310         'ia'      => array('q' => 0.8, 'lang' => 'ia', 'name' => 'Interlingua', 'direction' => 'ltr'),
311         'is'      => array('q' => 0.1, 'lang' => 'is', 'name' => 'Icelandic', 'direction' => 'ltr'),
312         'it'      => array('q' => 1, 'lang' => 'it', 'name' => 'Italian', 'direction' => 'ltr'),
313         'jp'      => array('q' => 0.5, 'lang' => 'ja', 'name' => 'Japanese', 'direction' => 'ltr'),
314         'ko'      => array('q' => 0.9, 'lang' => 'ko',    'name' => 'Korean', 'direction' => 'ltr'),
315         'mk'      => array('q' => 0.5, 'lang' => 'mk', 'name' => 'Macedonian', 'direction' => 'ltr'),
316         'nb'      => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
317         'no'      => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
318         'nn'      => array('q' => 1, 'lang' => 'nn', 'name' => 'Norwegian (Nynorsk)', 'direction' => 'ltr'),
319         'nl'      => array('q' => 0.5, 'lang' => 'nl', 'name' => 'Dutch', 'direction' => 'ltr'),
320         'pl'      => array('q' => 0.5, 'lang' => 'pl', 'name' => 'Polish', 'direction' => 'ltr'),
321         'pt'      => array('q' => 0.1, 'lang' => 'pt',    'name' => 'Portuguese', 'direction' => 'ltr'),
322         'pt-br'   => array('q' => 0.9, 'lang' => 'pt_BR', 'name' => 'Portuguese Brazil', 'direction' => 'ltr'),
323         'ru'      => array('q' => 0.9, 'lang' => 'ru', 'name' => 'Russian', 'direction' => 'ltr'),
324         'sv'      => array('q' => 0.8, 'lang' => 'sv', 'name' => 'Swedish', 'direction' => 'ltr'),
325         'te'      => array('q' => 0.3, 'lang' => 'te', 'name' => 'Telugu', 'direction' => 'ltr'),
326         'tr'      => array('q' => 0.5, 'lang' => 'tr', 'name' => 'Turkish', 'direction' => 'ltr'),
327         'uk'      => array('q' => 1, 'lang' => 'uk', 'name' => 'Ukrainian', 'direction' => 'ltr'),
328         'vi'      => array('q' => 0.8, 'lang' => 'vi', 'name' => 'Vietnamese', 'direction' => 'ltr'),
329         'zh-cn'   => array('q' => 0.9, 'lang' => 'zh_CN', 'name' => 'Chinese (Simplified)', 'direction' => 'ltr'),
330         'zh-hant' => array('q' => 0.2, 'lang' => 'zh_TW', 'name' => 'Chinese (Taiwanese)', 'direction' => 'ltr'),
331     );
332 }