]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/language.php
Localisation updates for !StatusNet from !translatewiki.net !sntrans
[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 if (!function_exists('gettext')) {
36     require_once("php-gettext/gettext.inc");
37 }
38
39
40 if (!function_exists('dpgettext')) {
41     /**
42      * Context-aware dgettext wrapper; use when messages in different contexts
43      * won't be distinguished from the English source but need different translations.
44      * The context string will appear as msgctxt in the .po files.
45      *
46      * Not currently exposed in PHP's gettext module; implemented to be compat
47      * with gettext.h's macros.
48      *
49      * @param string $domain domain identifier, or null for default domain
50      * @param string $context context identifier, should be some key like "menu|file"
51      * @param string $msgid English source text
52      * @return string original or translated message
53      */
54     function dpgettext($domain, $context, $msg)
55     {
56         $msgid = $context . "\004" . $msg;
57         $out = dcgettext($domain, $msgid, LC_MESSAGES);
58         if ($out == $msgid) {
59             return $msg;
60         } else {
61             return $out;
62         }
63     }
64 }
65
66 if (!function_exists('pgettext')) {
67     /**
68      * Context-aware gettext wrapper; use when messages in different contexts
69      * won't be distinguished from the English source but need different translations.
70      * The context string will appear as msgctxt in the .po files.
71      *
72      * Not currently exposed in PHP's gettext module; implemented to be compat
73      * with gettext.h's macros.
74      *
75      * @param string $context context identifier, should be some key like "menu|file"
76      * @param string $msgid English source text
77      * @return string original or translated message
78      */
79     function pgettext($context, $msg)
80     {
81         return dpgettext(textdomain(NULL), $context, $msg);
82     }
83 }
84
85 if (!function_exists('dnpgettext')) {
86     /**
87      * Context-aware dngettext wrapper; use when messages in different contexts
88      * won't be distinguished from the English source but need different translations.
89      * The context string will appear as msgctxt in the .po files.
90      *
91      * Not currently exposed in PHP's gettext module; implemented to be compat
92      * with gettext.h's macros.
93      *
94      * @param string $domain domain identifier, or null for default domain
95      * @param string $context context identifier, should be some key like "menu|file"
96      * @param string $msg singular English source text
97      * @param string $plural plural English source text
98      * @param int $n number of items to control plural selection
99      * @return string original or translated message
100      */
101     function dnpgettext($domain, $context, $msg, $plural, $n)
102     {
103         $msgid = $context . "\004" . $msg;
104         $out = dcngettext($domain, $msgid, $plural, $n, LC_MESSAGES);
105         if ($out == $msgid) {
106             return $msg;
107         } else {
108             return $out;
109         }
110     }
111 }
112
113 if (!function_exists('npgettext')) {
114     /**
115      * Context-aware ngettext wrapper; use when messages in different contexts
116      * won't be distinguished from the English source but need different translations.
117      * The context string will appear as msgctxt in the .po files.
118      *
119      * Not currently exposed in PHP's gettext module; implemented to be compat
120      * with gettext.h's macros.
121      *
122      * @param string $context context identifier, should be some key like "menu|file"
123      * @param string $msg singular English source text
124      * @param string $plural plural English source text
125      * @param int $n number of items to control plural selection
126      * @return string original or translated message
127      */
128     function npgettext($context, $msg, $plural, $n)
129     {
130         return dnpgettext(textdomain(NULL), $msgid, $plural, $n, LC_MESSAGES);
131     }
132 }
133
134 /**
135  * Shortcut for *gettext functions with smart domain detection.
136  *
137  * If calling from a plugin, this function checks which plugin was
138  * being called from and uses that as text domain, which will have
139  * been set up during plugin initialization.
140  *
141  * Also handles plurals and contexts depending on what parameters
142  * are passed to it:
143  *
144  *   gettext -> _m($msg)
145  *  ngettext -> _m($msg1, $msg2, $n)
146  *  pgettext -> _m($ctx, $msg)
147  * npgettext -> _m($ctx, $msg1, $msg2, $n)
148  *
149  * @fixme may not work properly in eval'd code
150  *
151  * @param string $msg
152  * @return string
153  */
154 function _m($msg/*, ...*/)
155 {
156     $domain = _mdomain(debug_backtrace());
157     $args = func_get_args();
158     switch(count($args)) {
159     case 1: return dgettext($domain, $msg);
160     case 2: return dpgettext($domain, $args[0], $args[1]);
161     case 3: return dngettext($domain, $args[0], $args[1], $args[2]);
162     case 4: return dnpgettext($domain, $args[0], $args[1], $args[2], $args[3]);
163     default: throw new Exception("Bad parameter count to _m()");
164     }
165 }
166
167 /**
168  * Looks for which plugin we've been called from to set the gettext domain.
169  *
170  * @param array $backtrace debug_backtrace() output
171  * @return string
172  * @private
173  * @fixme could explode if SN is under a 'plugins' folder or share name.
174  */
175 function _mdomain($backtrace)
176 {
177     /*
178       0 => 
179         array
180           'file' => string '/var/www/mublog/plugins/FeedSub/FeedSubPlugin.php' (length=49)
181           'line' => int 77
182           'function' => string '_m' (length=2)
183           'args' => 
184             array
185               0 => &string 'Feeds' (length=5)
186     */
187     static $cached;
188     $path = $backtrace[0]['file'];
189     if (!isset($cached[$path])) {
190         if (DIRECTORY_SEPARATOR !== '/') {
191             $path = strtr($path, DIRECTORY_SEPARATOR, '/');
192         }
193         $cut = strpos($path, '/plugins/') + 9;
194         $cut2 = strpos($path, '/', $cut);
195         if ($cut && $cut2) {
196             $cached[$path] = substr($path, $cut, $cut2 - $cut);
197         } else {
198             return null;
199         }
200     }
201     return $cached[$path];
202 }
203
204
205 /**
206  * Content negotiation for language codes
207  *
208  * @param string $httplang HTTP Accept-Language header
209  *
210  * @return string language code for best language match
211  */
212
213 function client_prefered_language($httplang)
214 {
215     $client_langs = array();
216
217     $all_languages = common_config('site', 'languages');
218
219     preg_match_all('"(((\S\S)-?(\S\S)?)(;q=([0-9.]+))?)\s*(,\s*|$)"',
220                    strtolower($httplang), $httplang);
221
222     for ($i = 0; $i < count($httplang); $i++) {
223         if (!empty($httplang[2][$i])) {
224             // if no q default to 1.0
225             $client_langs[$httplang[2][$i]] =
226               ($httplang[6][$i]? (float) $httplang[6][$i] : 1.0 - ($i*0.01));
227         }
228         if (!empty($httplang[3][$i]) && empty($client_langs[$httplang[3][$i]])) {
229             // if a catchall default 0.01 lower
230             $client_langs[$httplang[3][$i]] =
231               ($httplang[6][$i]? (float) $httplang[6][$i]-0.01 : 0.99);
232         }
233     }
234     // sort in decending q
235     arsort($client_langs);
236
237     foreach ($client_langs as $lang => $q) {
238         if (isset($all_languages[$lang])) {
239             return($all_languages[$lang]['lang']);
240         }
241     }
242     return false;
243 }
244
245 /**
246  * returns a simple code -> name mapping for languages
247  *
248  * @return array map of available languages by code to language name.
249  */
250
251 function get_nice_language_list()
252 {
253     $nice_lang = array();
254
255     $all_languages = common_config('site', 'languages');
256
257     foreach ($all_languages as $lang) {
258         $nice_lang = $nice_lang + array($lang['lang'] => $lang['name']);
259     }
260     return $nice_lang;
261 }
262
263 /**
264  * Get a list of all languages that are enabled in the default config
265  *
266  * This should ONLY be called when setting up the default config in common.php.
267  * Any other attempt to get a list of languages should instead call
268  * common_config('site','languages')
269  *
270  * @return array mapping of language codes to language info
271  */
272 function get_all_languages() {
273     return array(
274         'ar'      => array('q' => 0.8, 'lang' => 'ar', 'name' => 'Arabic', 'direction' => 'rtl'),
275         'arz'     => array('q' => 0.8, 'lang' => 'arz', 'name' => 'Egyptian Spoken Arabic', 'direction' => 'rtl'),
276         'bg'      => array('q' => 0.8, 'lang' => 'bg', 'name' => 'Bulgarian', 'direction' => 'ltr'),
277         'ca'      => array('q' => 0.5, 'lang' => 'ca', 'name' => 'Catalan', 'direction' => 'ltr'),
278         'cs'      => array('q' => 0.5, 'lang' => 'cs', 'name' => 'Czech', 'direction' => 'ltr'),
279         'de'      => array('q' => 0.8, 'lang' => 'de', 'name' => 'German', 'direction' => 'ltr'),
280         'el'      => array('q' => 0.1, 'lang' => 'el',    'name' => 'Greek', 'direction' => 'ltr'),
281         'en-us'   => array('q' => 1, 'lang' => 'en', 'name' => 'English (US)', 'direction' => 'ltr'),
282         'en-gb'   => array('q' => 1, 'lang' => 'en_GB', 'name' => 'English (British)', 'direction' => 'ltr'),
283         'en'      => array('q' => 1, 'lang' => 'en',    'name' => 'English (US)', 'direction' => 'ltr'),
284         'es'      => array('q' => 1, 'lang' => 'es',    'name' => 'Spanish', 'direction' => 'ltr'),
285         'fi'      => array('q' => 1, 'lang' => 'fi', 'name' => 'Finnish', 'direction' => 'ltr'),
286         'fr-fr'   => array('q' => 1, 'lang' => 'fr', 'name' => 'French', 'direction' => 'ltr'),
287         'ga'      => array('q' => 0.5, 'lang' => 'ga', 'name' => 'Galician', 'direction' => 'ltr'),
288         'he'      => array('q' => 0.5, 'lang' => 'he', 'name' => 'Hebrew', 'direction' => 'rtl'),
289         'hsb'     => array('q' => 0.8, 'lang' => 'hsb', 'name' => 'Upper Sorbian', 'direction' => 'ltr'),
290         'ia'      => array('q' => 0.8, 'lang' => 'ia', 'name' => 'Interlingua', 'direction' => 'ltr'),
291         'is'      => array('q' => 0.1, 'lang' => 'is', 'name' => 'Icelandic', 'direction' => 'ltr'),
292         'it'      => array('q' => 1, 'lang' => 'it', 'name' => 'Italian', 'direction' => 'ltr'),
293         'jp'      => array('q' => 0.5, 'lang' => 'ja', 'name' => 'Japanese', 'direction' => 'ltr'),
294         'ko'      => array('q' => 0.9, 'lang' => 'ko',    'name' => 'Korean', 'direction' => 'ltr'),
295         'mk'      => array('q' => 0.5, 'lang' => 'mk', 'name' => 'Macedonian', 'direction' => 'ltr'),
296         'nb'      => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
297         'no'      => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
298         'nn'      => array('q' => 1, 'lang' => 'nn', 'name' => 'Norwegian (Nynorsk)', 'direction' => 'ltr'),
299         'nl'      => array('q' => 0.5, 'lang' => 'nl', 'name' => 'Dutch', 'direction' => 'ltr'),
300         'pl'      => array('q' => 0.5, 'lang' => 'pl', 'name' => 'Polish', 'direction' => 'ltr'),
301         'pt'      => array('q' => 0.1, 'lang' => 'pt',    'name' => 'Portuguese', 'direction' => 'ltr'),
302         'pt-br'   => array('q' => 0.9, 'lang' => 'pt_BR', 'name' => 'Portuguese Brazil', 'direction' => 'ltr'),
303         'ru'      => array('q' => 0.9, 'lang' => 'ru', 'name' => 'Russian', 'direction' => 'ltr'),
304         'sv'      => array('q' => 0.8, 'lang' => 'sv', 'name' => 'Swedish', 'direction' => 'ltr'),
305         'te'      => array('q' => 0.3, 'lang' => 'te', 'name' => 'Telugu', 'direction' => 'ltr'),
306         'tr'      => array('q' => 0.5, 'lang' => 'tr', 'name' => 'Turkish', 'direction' => 'ltr'),
307         'uk'      => array('q' => 1, 'lang' => 'uk', 'name' => 'Ukrainian', 'direction' => 'ltr'),
308         'vi'      => array('q' => 0.8, 'lang' => 'vi', 'name' => 'Vietnamese', 'direction' => 'ltr'),
309         'zh-cn'   => array('q' => 0.9, 'lang' => 'zh_CN', 'name' => 'Chinese (Simplified)', 'direction' => 'ltr'),
310         'zh-hant' => array('q' => 0.2, 'lang' => 'zh_TW', 'name' => 'Chinese (Taiwanese)', 'direction' => 'ltr'),
311     );
312 }