]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/language.php
Fix a couple of errors in PublicGroupNav
[quix0rs-gnu-social.git] / lib / language.php
1 <?php
2 /**
3  * Laconica, 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  Laconica
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/
29  */
30
31 if (!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);
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 lanugages should instead call
98  * common_config('site','languages')
99  *
100  * @return array mapping of language codes to language info
101  */
102
103 function get_all_languages()
104 {
105     return
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'),
150             );
151 }
152