3 * StatusNet, the distributed open-source microblogging tool
5 * utility functions for i18n
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.
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.
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/>.
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/
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 if (!function_exists('gettext')) {
36 require_once("php-gettext/gettext.inc");
40 if (!function_exists('dpgettext')) {
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.
46 * Not currently exposed in PHP's gettext module; implemented to be compat
47 * with gettext.h's macros.
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
54 function dpgettext($domain, $context, $msg)
56 $msgid = $context . "\004" . $msg;
57 $out = dcgettext($domain, $msgid, LC_MESSAGES);
66 if (!function_exists('pgettext')) {
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.
72 * Not currently exposed in PHP's gettext module; implemented to be compat
73 * with gettext.h's macros.
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
79 function pgettext($context, $msg)
81 return dpgettext(textdomain(NULL), $context, $msg);
85 if (!function_exists('dnpgettext')) {
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.
91 * Not currently exposed in PHP's gettext module; implemented to be compat
92 * with gettext.h's macros.
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
101 function dnpgettext($domain, $context, $msg, $plural, $n)
103 $msgid = $context . "\004" . $msg;
104 $out = dcngettext($domain, $msgid, $plural, $n, LC_MESSAGES);
105 if ($out == $msgid) {
113 if (!function_exists('npgettext')) {
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.
119 * Not currently exposed in PHP's gettext module; implemented to be compat
120 * with gettext.h's macros.
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
128 function npgettext($context, $msg, $plural, $n)
130 return dnpgettext(textdomain(NULL), $msgid, $plural, $n, LC_MESSAGES);
135 * Shortcut for *gettext functions with smart domain detection.
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.
141 * Also handles plurals and contexts depending on what parameters
144 * gettext -> _m($msg)
145 * ngettext -> _m($msg1, $msg2, $n)
146 * pgettext -> _m($ctx, $msg)
147 * npgettext -> _m($ctx, $msg1, $msg2, $n)
149 * @fixme may not work properly in eval'd code
154 function _m($msg/*, ...*/)
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()");
168 * Looks for which plugin we've been called from to set the gettext domain.
170 * @param array $backtrace debug_backtrace() output
173 * @fixme could explode if SN is under a 'plugins' folder or share name.
175 function _mdomain($backtrace)
180 'file' => string '/var/www/mublog/plugins/FeedSub/FeedSubPlugin.php' (length=49)
182 'function' => string '_m' (length=2)
185 0 => &string 'Feeds' (length=5)
188 $path = $backtrace[0]['file'];
189 if (!isset($cached[$path])) {
190 if (DIRECTORY_SEPARATOR !== '/') {
191 $path = strtr($path, DIRECTORY_SEPARATOR, '/');
193 $cut = strpos($path, '/plugins/') + 9;
194 $cut2 = strpos($path, '/', $cut);
196 $cached[$path] = substr($path, $cut, $cut2 - $cut);
201 return $cached[$path];
206 * Content negotiation for language codes
208 * @param string $httplang HTTP Accept-Language header
210 * @return string language code for best language match
213 function client_prefered_language($httplang)
215 $client_langs = array();
217 $all_languages = common_config('site', 'languages');
219 preg_match_all('"(((\S\S)-?(\S\S)?)(;q=([0-9.]+))?)\s*(,\s*|$)"',
220 strtolower($httplang), $httplang);
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));
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);
234 // sort in decending q
235 arsort($client_langs);
237 foreach ($client_langs as $lang => $q) {
238 if (isset($all_languages[$lang])) {
239 return($all_languages[$lang]['lang']);
246 * returns a simple code -> name mapping for languages
248 * @return array map of available languages by code to language name.
251 function get_nice_language_list()
253 $nice_lang = array();
255 $all_languages = common_config('site', 'languages');
257 foreach ($all_languages as $lang) {
258 $nice_lang = $nice_lang + array($lang['lang'] => $lang['name']);
264 * Get a list of all languages that are enabled in the default config
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')
270 * @return array mapping of language codes to language info
272 function get_all_languages() {
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 'is' => array('q' => 0.1, 'lang' => 'is', 'name' => 'Icelandic', 'direction' => 'ltr'),
291 'it' => array('q' => 1, 'lang' => 'it', 'name' => 'Italian', 'direction' => 'ltr'),
292 'jp' => array('q' => 0.5, 'lang' => 'ja', 'name' => 'Japanese', 'direction' => 'ltr'),
293 'ko' => array('q' => 0.9, 'lang' => 'ko', 'name' => 'Korean', 'direction' => 'ltr'),
294 'mk' => array('q' => 0.5, 'lang' => 'mk', 'name' => 'Macedonian', 'direction' => 'ltr'),
295 'nb' => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
296 'no' => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
297 'nn' => array('q' => 1, 'lang' => 'nn', 'name' => 'Norwegian (Nynorsk)', 'direction' => 'ltr'),
298 'nl' => array('q' => 0.5, 'lang' => 'nl', 'name' => 'Dutch', 'direction' => 'ltr'),
299 'pl' => array('q' => 0.5, 'lang' => 'pl', 'name' => 'Polish', 'direction' => 'ltr'),
300 'pt' => array('q' => 0.1, 'lang' => 'pt', 'name' => 'Portuguese', 'direction' => 'ltr'),
301 'pt-br' => array('q' => 0.9, 'lang' => 'pt_BR', 'name' => 'Portuguese Brazil', 'direction' => 'ltr'),
302 'ru' => array('q' => 0.9, 'lang' => 'ru', 'name' => 'Russian', 'direction' => 'ltr'),
303 'sv' => array('q' => 0.8, 'lang' => 'sv', 'name' => 'Swedish', 'direction' => 'ltr'),
304 'te' => array('q' => 0.3, 'lang' => 'te', 'name' => 'Telugu', 'direction' => 'ltr'),
305 'tr' => array('q' => 0.5, 'lang' => 'tr', 'name' => 'Turkish', 'direction' => 'ltr'),
306 'uk' => array('q' => 1, 'lang' => 'uk', 'name' => 'Ukrainian', 'direction' => 'ltr'),
307 'vi' => array('q' => 0.8, 'lang' => 'vi', 'name' => 'Vietnamese', 'direction' => 'ltr'),
308 'zh-cn' => array('q' => 0.9, 'lang' => 'zh_CN', 'name' => 'Chinese (Simplified)', 'direction' => 'ltr'),
309 'zh-hant' => array('q' => 0.2, 'lang' => 'zh_TW', 'name' => 'Chinese (Taiwanese)', 'direction' => 'ltr'),