]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/language.php
Twitter-compatible API: better error handling for replier_by_reply()
[quix0rs-gnu-social.git] / lib / language.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22
23
24 function client_prefered_language($httplang) {
25         $client_langs = array();
26         $all_languages = get_all_languages();
27
28         preg_match_all('"(((\S\S)-?(\S\S)?)(;q=([0-9.]+))?)\s*(,\s*|$)"',strtolower($httplang),$httplang);
29         for ($i = 0; $i < count($httplang); $i++) {
30              if(!empty($httplang[2][$i])) {
31                     #if no q default to 1.0
32                     $client_langs[$httplang[2][$i]] = ($httplang[6][$i]? (float) $httplang[6][$i] : 1.0);
33                 }
34                 if(!empty($httplang[3][$i]) && empty($client_langs[$httplang[3][$i]])) {
35                     #if a catchall default 0.01 lower
36                     $client_langs[$httplang[3][$i]] = ($httplang[6][$i]? (float) $httplang[6][$i]-0.01 : 0.99);
37                 }
38             }
39             #sort in decending q
40             arsort($client_langs);
41
42             foreach ($client_langs as $lang => $q) {
43                 if (isset($all_languages[$lang])) {
44                     return($all_languages[$lang]['lang']);
45                 }
46             }
47             return FALSE;
48 }
49
50 function get_nice_language_list() {
51         $nice_lang = array();
52         $all_languages = get_all_languages();
53         foreach ($all_languages as $lang) {
54                 $nice_lang = $nice_lang + array($lang['lang'] => $lang['name']);
55         }
56         return $nice_lang;
57 }
58
59 function get_all_languages() {
60         $all_languages = array('en-us' => array('q' => 1, 'lang' => 'en_US', 'name' => 'English (US)', 'direction' => 'ltr'),
61                             'en-nz' => array('q' => 1, 'lang' => 'en_NZ', 'name' => 'English (NZ)', 'direction' => 'ltr'),
62                             'en' => array('q' => 1, 'lang' => 'en', 'name' => 'English', 'direction' => 'ltr'),
63                             'fr-fr' => array('q' => 1, 'lang' => 'fr_FR', 'name' => 'French', 'direction' => 'ltr'),
64                         );
65         return $all_languages;
66 }