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");
39 if (!function_exists('pgettext')) {
41 * Context-aware gettext wrapper; use when messages in different contexts
42 * won't be distinguished from the English source but need different translations.
43 * The context string will appear as msgctxt in the .po files.
45 * Not currently exposed in PHP's gettext module; implemented to be compat
46 * with gettext.h's macros.
48 * @param string $context context identifier, should be some key like "menu|file"
49 * @param string $msgid English source text
50 * @return string original or translated message
52 function pgettext($context, $msg)
54 $msgid = $context . "\004" . $msg;
55 $out = dcgettext(textdomain(NULL), $msgid, LC_MESSAGES);
64 if (!function_exists('npgettext')) {
66 * Context-aware ngettext wrapper; use when messages in different contexts
67 * won't be distinguished from the English source but need different translations.
68 * The context string will appear as msgctxt in the .po files.
70 * Not currently exposed in PHP's gettext module; implemented to be compat
71 * with gettext.h's macros.
73 * @param string $context context identifier, should be some key like "menu|file"
74 * @param string $msg singular English source text
75 * @param string $plural plural English source text
76 * @param int $n number of items to control plural selection
77 * @return string original or translated message
79 function npgettext($context, $msg, $plural, $n)
81 $msgid = $context . "\004" . $msg;
82 $out = dcngettext(textdomain(NULL), $msgid, $plural, $n, LC_MESSAGES);
93 * Content negotiation for language codes
95 * @param string $httplang HTTP Accept-Language header
97 * @return string language code for best language match
100 function client_prefered_language($httplang)
102 $client_langs = array();
104 $all_languages = common_config('site', 'languages');
106 preg_match_all('"(((\S\S)-?(\S\S)?)(;q=([0-9.]+))?)\s*(,\s*|$)"',
107 strtolower($httplang), $httplang);
109 for ($i = 0; $i < count($httplang); $i++) {
110 if (!empty($httplang[2][$i])) {
111 // if no q default to 1.0
112 $client_langs[$httplang[2][$i]] =
113 ($httplang[6][$i]? (float) $httplang[6][$i] : 1.0 - ($i*0.01));
115 if (!empty($httplang[3][$i]) && empty($client_langs[$httplang[3][$i]])) {
116 // if a catchall default 0.01 lower
117 $client_langs[$httplang[3][$i]] =
118 ($httplang[6][$i]? (float) $httplang[6][$i]-0.01 : 0.99);
121 // sort in decending q
122 arsort($client_langs);
124 foreach ($client_langs as $lang => $q) {
125 if (isset($all_languages[$lang])) {
126 return($all_languages[$lang]['lang']);
133 * returns a simple code -> name mapping for languages
135 * @return array map of available languages by code to language name.
138 function get_nice_language_list()
140 $nice_lang = array();
142 $all_languages = common_config('site', 'languages');
144 foreach ($all_languages as $lang) {
145 $nice_lang = $nice_lang + array($lang['lang'] => $lang['name']);
151 * Get a list of all languages that are enabled in the default config
153 * This should ONLY be called when setting up the default config in common.php.
154 * Any other attempt to get a list of languages should instead call
155 * common_config('site','languages')
157 * @return array mapping of language codes to language info
159 function get_all_languages() {
161 'ar' => array('q' => 0.8, 'lang' => 'ar', 'name' => 'Arabic', 'direction' => 'rtl'),
162 'bg' => array('q' => 0.8, 'lang' => 'bg', 'name' => 'Bulgarian', 'direction' => 'ltr'),
163 'ca' => array('q' => 0.5, 'lang' => 'ca', 'name' => 'Catalan', 'direction' => 'ltr'),
164 'cs' => array('q' => 0.5, 'lang' => 'cs', 'name' => 'Czech', 'direction' => 'ltr'),
165 'de' => array('q' => 0.8, 'lang' => 'de', 'name' => 'German', 'direction' => 'ltr'),
166 'el' => array('q' => 0.1, 'lang' => 'el', 'name' => 'Greek', 'direction' => 'ltr'),
167 'en-us' => array('q' => 1, 'lang' => 'en', 'name' => 'English (US)', 'direction' => 'ltr'),
168 'en-gb' => array('q' => 1, 'lang' => 'en_GB', 'name' => 'English (British)', 'direction' => 'ltr'),
169 'en' => array('q' => 1, 'lang' => 'en', 'name' => 'English (US)', 'direction' => 'ltr'),
170 'es' => array('q' => 1, 'lang' => 'es', 'name' => 'Spanish', 'direction' => 'ltr'),
171 'fi' => array('q' => 1, 'lang' => 'fi', 'name' => 'Finnish', 'direction' => 'ltr'),
172 'fr-fr' => array('q' => 1, 'lang' => 'fr', 'name' => 'French', 'direction' => 'ltr'),
173 'ga' => array('q' => 0.5, 'lang' => 'ga', 'name' => 'Galician', 'direction' => 'ltr'),
174 'he' => array('q' => 0.5, 'lang' => 'he', 'name' => 'Hebrew', 'direction' => 'rtl'),
175 'is' => array('q' => 0.1, 'lang' => 'is', 'name' => 'Icelandic', 'direction' => 'ltr'),
176 'it' => array('q' => 1, 'lang' => 'it', 'name' => 'Italian', 'direction' => 'ltr'),
177 'jp' => array('q' => 0.5, 'lang' => 'ja', 'name' => 'Japanese', 'direction' => 'ltr'),
178 'ko' => array('q' => 0.9, 'lang' => 'ko', 'name' => 'Korean', 'direction' => 'ltr'),
179 'mk' => array('q' => 0.5, 'lang' => 'mk', 'name' => 'Macedonian', 'direction' => 'ltr'),
180 'nb' => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
181 'no' => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
182 'nn' => array('q' => 1, 'lang' => 'nn', 'name' => 'Norwegian (Nynorsk)', 'direction' => 'ltr'),
183 'nl' => array('q' => 0.5, 'lang' => 'nl', 'name' => 'Dutch', 'direction' => 'ltr'),
184 'pl' => array('q' => 0.5, 'lang' => 'pl', 'name' => 'Polish', 'direction' => 'ltr'),
185 'pt' => array('q' => 0.1, 'lang' => 'pt', 'name' => 'Portuguese', 'direction' => 'ltr'),
186 'pt-br' => array('q' => 0.9, 'lang' => 'pt_BR', 'name' => 'Portuguese Brazil', 'direction' => 'ltr'),
187 'ru' => array('q' => 0.9, 'lang' => 'ru', 'name' => 'Russian', 'direction' => 'ltr'),
188 'sv' => array('q' => 0.8, 'lang' => 'sv', 'name' => 'Swedish', 'direction' => 'ltr'),
189 'te' => array('q' => 0.3, 'lang' => 'te', 'name' => 'Telugu', 'direction' => 'ltr'),
190 'tr' => array('q' => 0.5, 'lang' => 'tr', 'name' => 'Turkish', 'direction' => 'ltr'),
191 'uk' => array('q' => 1, 'lang' => 'uk', 'name' => 'Ukrainian', 'direction' => 'ltr'),
192 'vi' => array('q' => 0.8, 'lang' => 'vi', 'name' => 'Vietnamese', 'direction' => 'ltr'),
193 'zh-cn' => array('q' => 0.9, 'lang' => 'zh_CN', 'name' => 'Chinese (Simplified)', 'direction' => 'ltr'),
194 'zh-hant' => array('q' => 0.2, 'lang' => 'zh_TW', 'name' => 'Chinese (Taiwanese)', 'direction' => 'ltr'),