]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/language.php
define LACONICA and accept LACONICA for backwards compatibility
[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 /**
36  * Content negotiation for language codes
37  *
38  * @param string $httplang HTTP Accept-Language header
39  *
40  * @return string language code for best language match
41  */
42
43 function client_prefered_language($httplang)
44 {
45     $client_langs = array();
46
47     $all_languages = common_config('site', 'languages');
48
49     preg_match_all('"(((\S\S)-?(\S\S)?)(;q=([0-9.]+))?)\s*(,\s*|$)"',
50                    strtolower($httplang), $httplang);
51
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 - ($i*0.01));
57         }
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);
62         }
63     }
64     // sort in decending q
65     arsort($client_langs);
66
67     foreach ($client_langs as $lang => $q) {
68         if (isset($all_languages[$lang])) {
69             return($all_languages[$lang]['lang']);
70         }
71     }
72     return false;
73 }
74
75 /**
76  * returns a simple code -> name mapping for languages
77  *
78  * @return array map of available languages by code to language name.
79  */
80
81 function get_nice_language_list()
82 {
83     $nice_lang = array();
84
85     $all_languages = common_config('site', 'languages');
86
87     foreach ($all_languages as $lang) {
88         $nice_lang = $nice_lang + array($lang['lang'] => $lang['name']);
89     }
90     return $nice_lang;
91 }
92
93 /**
94  * Get a list of all languages that are enabled in the default config
95  *
96  * This should ONLY be called when setting up the default config in common.php.
97  * Any other attempt to get a list of languages should instead call
98  * common_config('site','languages')
99  *
100  * @return array mapping of language codes to language info
101  */
102 function get_all_languages() {
103         return array(
104                 'bg'      => array('q' => 0.8, 'lang' => 'bg_BG', 'name' => 'Bulgarian', 'direction' => 'ltr'),
105                 'ca'      => array('q' => 0.5, 'lang' => 'ca_ES', 'name' => 'Catalan', 'direction' => 'ltr'),
106                 'cs'      => array('q' => 0.5, 'lang' => 'cs_CZ', 'name' => 'Czech', 'direction' => 'ltr'),
107                 'de'      => array('q' => 0.8, 'lang' => 'de_DE', 'name' => 'German', 'direction' => 'ltr'),
108                 'el'      => array('q' => 0.1, 'lang' => 'el',    'name' => 'Greek', 'direction' => 'ltr'),
109                 'en-us'   => array('q' => 1, 'lang' => 'en_US', 'name' => 'English (US)', 'direction' => 'ltr'),
110                 'en-gb'   => array('q' => 1, 'lang' => 'en_GB', 'name' => 'English (British)', 'direction' => 'ltr'),
111                 'en'      => array('q' => 1, 'lang' => 'en_US',    'name' => 'English (US)', 'direction' => 'ltr'),
112                 'es'      => array('q' => 1, 'lang' => 'es',    'name' => 'Spanish', 'direction' => 'ltr'),
113                 'fi'      => array('q' => 1, 'lang' => 'fi', 'name' => 'Finnish', 'direction' => 'ltr'),
114                 'fr-fr'   => array('q' => 1, 'lang' => 'fr_FR', 'name' => 'French', 'direction' => 'ltr'),
115                 'he'      => array('q' => 0.5, 'lang' => 'he_IL', 'name' => 'Hebrew', 'direction' => 'rtl'),
116                 'it'      => array('q' => 1, 'lang' => 'it_IT', 'name' => 'Italian', 'direction' => 'ltr'),
117                 'jp'      => array('q' => 0.5, 'lang' => 'ja_JP', 'name' => 'Japanese', 'direction' => 'ltr'),
118                 'ko'      => array('q' => 0.9, 'lang' => 'ko_KR',    'name' => 'Korean', 'direction' => 'ltr'),
119                 'mk'      => array('q' => 0.5, 'lang' => 'mk_MK', 'name' => 'Macedonian', 'direction' => 'ltr'),
120                 'nb'      => array('q' => 0.1, 'lang' => 'nb_NO', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
121                 'no'      => array('q' => 0.1, 'lang' => 'nb_NO', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
122                 'nn'      => array('q' => 1, 'lang' => 'nn_NO', 'name' => 'Norwegian (Nynorsk)', 'direction' => 'ltr'),
123                 'nl'      => array('q' => 0.5, 'lang' => 'nl_NL', 'name' => 'Dutch', 'direction' => 'ltr'),
124                 'pl'      => array('q' => 0.5, 'lang' => 'pl_PL', 'name' => 'Polish', 'direction' => 'ltr'),
125                 'pt'      => array('q' => 0.1, 'lang' => 'pt',    'name' => 'Portuguese', 'direction' => 'ltr'),
126                 'pt-br'   => array('q' => 0.9, 'lang' => 'pt_BR', 'name' => 'Portuguese Brazil', 'direction' => 'ltr'),
127                 'ru'      => array('q' => 0.9, 'lang' => 'ru_RU', 'name' => 'Russian', 'direction' => 'ltr'),
128                 'sv'      => array('q' => 0.8, 'lang' => 'sv_SE', 'name' => 'Swedish', 'direction' => 'ltr'),
129                 'te'      => array('q' => 0.3, 'lang' => 'te_IN', 'name' => 'Telugu', 'direction' => 'ltr'),
130                 'tr'      => array('q' => 0.5, 'lang' => 'tr_TR', 'name' => 'Turkish', 'direction' => 'ltr'),
131                 'uk'      => array('q' => 1, 'lang' => 'uk_UA', 'name' => 'Ukrainian', 'direction' => 'ltr'),
132                 'vi'      => array('q' => 0.8, 'lang' => 'vi_VN', 'name' => 'Vietnamese', 'direction' => 'ltr'),
133                 'zh-cn'   => array('q' => 0.9, 'lang' => 'zh_CN', 'name' => 'Chinese (Simplified)', 'direction' => 'ltr'),
134                 'zh-hant' => array('q' => 0.2, 'lang' => 'zh_TW', 'name' => 'Chinese (Taiwanese)', 'direction' => 'ltr'),
135         );
136 }