3 * Laconica, 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@controlyourself.ca>
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://laconi.ca/
31 if (!defined('LACONICA')) {
36 * Content negotiation for language codes
38 * @param string $httplang HTTP Accept-Language header
40 * @return string language code for best language match
43 function client_prefered_language($httplang)
45 $client_langs = array();
47 $all_languages = common_config('site', 'languages');
49 preg_match_all('"(((\S\S)-?(\S\S)?)(;q=([0-9.]+))?)\s*(,\s*|$)"',
50 strtolower($httplang), $httplang);
52 for ($i = 0; $i < count($httplang); $i++) {
53 if (!empty($httplang[2][$i])) {
54 // if no q default to 1.0
55 $client_langs[$httplang[2][$i]] =
56 ($httplang[6][$i]? (float) $httplang[6][$i] : 1.0);
58 if (!empty($httplang[3][$i]) && empty($client_langs[$httplang[3][$i]])) {
59 // if a catchall default 0.01 lower
60 $client_langs[$httplang[3][$i]] =
61 ($httplang[6][$i]? (float) $httplang[6][$i]-0.01 : 0.99);
64 // sort in decending q
65 arsort($client_langs);
67 foreach ($client_langs as $lang => $q) {
68 if (isset($all_languages[$lang])) {
69 return($all_languages[$lang]['lang']);
76 * returns a simple code -> name mapping for languages
78 * @return array map of available languages by code to language name.
81 function get_nice_language_list()
85 $all_languages = common_config('site', 'languages');
87 foreach ($all_languages as $lang) {
88 $nice_lang = $nice_lang + array($lang['lang'] => $lang['name']);
94 * Get a list of all languages that are enabled in the default config
96 * This should ONLY be called when setting up the default config in common.php.
97 * Any other attempt to get a list of lanugages should instead call
98 * common_config('site','languages')
100 * @return array mapping of language codes to language info
103 function get_all_languages()
106 array('en-us' => array('q' => 1, 'lang' => 'en_US',
107 'name' => 'English (US)', 'direction' => 'ltr'),
108 'en-nz' => array('q' => 1, 'lang' => 'en_NZ',
109 'name' => 'English (NZ)', 'direction' => 'ltr'),
110 'en-gb' => array('q' => 1, 'lang' => 'en_GB',
111 'name' => 'English (British)', 'direction' => 'ltr'),
112 'en' => array('q' => 1, 'lang' => 'en',
113 'name' => 'English', 'direction' => 'ltr'),
114 'da' => array('q' => 0.1, 'lang' => 'da_DK',
115 'name' => 'Danish', 'direction' => 'ltr'),
116 'nl' => array('q' => 1, 'lang' => 'nl_NL',
117 'name' => 'Dutch', 'direction' => 'ltr'),
118 'eo' => array('q' => 0.1, 'lang' => 'eo',
119 'name' => 'Esperanto', 'direction' => 'ltr'),
120 'fr-fr' => array('q' => 0.9, 'lang' => 'fr_FR',
121 'name' => 'French', 'direction' => 'ltr'),
122 'de' => array('q' => 1, 'lang' => 'de_DE',
123 'name' => 'German', 'direction' => 'ltr'),
124 'it' => array('q' => 1, 'lang' => 'it_IT',
125 'name' => 'Italian', 'direction' => 'ltr'),
126 'ko' => array('q' => 0.1, 'lang' => 'ko',
127 'name' => 'Korean', 'direction' => 'ltr'),
128 'nb' => array('q' => 1, 'lang' => 'nb_NO',
129 'name' => 'Norwegian (bokmal)', 'direction' => 'ltr'),
130 'pt' => array('q' => 0.2, 'lang' => 'pt',
131 'name' => 'Portuguese', 'direction' => 'ltr'),
132 'pt-br' => array('q' => 1, 'lang' => 'pt_BR',
133 'name' => 'Portuguese Brazil', 'direction' => 'ltr'),
134 'es' => array('q' => 1, 'lang' => 'es',
135 'name' => 'Spanish', 'direction' => 'ltr'),
136 'tr' => array('q' => 1, 'lang' => 'tr_TR',
137 'name' => 'Turkish', 'direction' => 'ltr'),
138 'uk' => array('q' => 1, 'lang' => 'uk_UA',
139 'name' => 'Ukrainian', 'direction' => 'ltr'),
140 'pl' => array('q' => 1, 'lang' => 'pl_PL',
141 'name' => 'Polish', 'direction' => 'ltr'),
142 'mk' => array('q' => 1, 'lang' => 'mk_MK',
143 'name' => 'Macedonian', 'direction' => 'ltr'),
144 'jp' => array('q' => 0.1, 'lang' => 'ja_JP',
145 'name' => 'Japanese', 'direction' => 'ltr'),
146 'cs' => array('q' => 1, 'lang' => 'cs_CZ',
147 'name' => 'Czech', 'direction' => 'ltr'),
148 'ca' => array('q' => 1, 'lang' => 'ca_ES',
149 'name' => 'Catalan', 'direction' => 'ltr'),