]> git.mxchange.org Git - friendica.git/blob - include/pgettext.php
ed924fa337209bc51dcfb654bb6ed29bb37e78cf
[friendica.git] / include / pgettext.php
1 <?php
2
3 require_once("include/dba.php");
4
5 /**
6  * translation support
7  */
8
9
10 /**
11  *
12  * Get the language setting directly from system variables, bypassing get_config()
13  * as database may not yet be configured.
14  *
15  * If possible, we use the value from the browser.
16  *
17  */
18
19
20 if(! function_exists('get_browser_language')) {
21 function get_browser_language() {
22
23         if (x($_SERVER,'HTTP_ACCEPT_LANGUAGE')) {
24                 // break up string into pieces (languages and q factors)
25                 preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
26                         $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
27
28                 if (count($lang_parse[1])) {
29                         for ($i=0; $i<count($lang_parse[1]); $i++) {
30                                 if ( strlen($lang_parse[1][$i])>2 ) {
31                                         if (! in_array(substr($lang_parse[1][$i], 0, 2), $lang_parse[1] ) ) {
32                                                 array_push($lang_parse[1], substr($lang_parse[1][$i], 0, 2));
33                                                 if (floatval($lang_parse[4][$i])>0)
34                                                         array_push($lang_parse[4], strval(floatval($lang_parse[4][$i])-0.0001));
35                                                 else
36                                                         array_push($lang_parse[4], '0.9999');
37                                         }
38                                 }
39                         }
40                         // create a list like "en" => 0.8
41                         $langs = array_combine($lang_parse[1], $lang_parse[4]);
42
43                         // set default to 1 for any without q factor
44                         foreach ($langs as $lang => $val) {
45                                 if ($val === '') $langs[$lang] = 1;
46                                 if ($val === '0') $langs[$lang] = 1;
47                         }
48
49                         // sort list based on value
50                         arsort($langs, SORT_NUMERIC);
51                         print_r($langs);
52                 }
53         }
54
55         if(isset($langs) && count($langs)) {
56                 foreach ($langs as $lang => $v) {
57                         if(file_exists("view/lang/$lang") && is_dir("view/lang/$lang")) {
58                                 $preferred = $lang;
59                                 break;
60                         }
61                 }
62         }
63
64         if(isset($preferred))
65                 return $preferred;
66
67     $a = get_app();
68         return ((isset($a->config['system']['language'])) ? $a->config['system']['language'] : 'en');
69 }}
70
71
72 function push_lang($language) {
73         global $lang, $a;
74
75         $a->langsave = $lang;
76
77         if($language === $lang)
78                 return;
79
80         if(isset($a->strings) && count($a->strings)) {
81                 $a->stringsave = $a->strings;
82         }
83         $a->strings = array();
84         load_translation_table($language);
85         $lang = $language;
86 }
87
88 function pop_lang() {
89         global $lang, $a;
90
91         if($lang === $a->langsave)
92                 return;
93
94         if(isset($a->stringsave))
95                 $a->strings = $a->stringsave;
96         else
97                 $a->strings = array();
98
99         $lang = $a->langsave;
100 }
101
102
103 // l
104
105 if(! function_exists('load_translation_table')) {
106 /**
107  * load string translation table for alternate language
108  *
109  * first plugin strings are loaded, then globals
110  *
111  * @param string $lang language code to load
112  */
113 function load_translation_table($lang) {
114         global $a;
115
116         $a->strings = array();
117         // load enabled plugins strings
118         $plugins = q("SELECT name FROM addon WHERE installed=1;");
119         if ($plugins!==false) {
120                 foreach($plugins as $p) {
121                         $name = $p['name'];
122                         if(file_exists("addon/$name/lang/$lang/strings.php")) {
123                                 include("addon/$name/lang/$lang/strings.php");
124                         }
125                 }
126         }
127
128         if(file_exists("view/lang/$lang/strings.php")) {
129                 include("view/lang/$lang/strings.php");
130         }
131
132 }}
133
134 // translate string if translation exists
135
136 if(! function_exists('t')) {
137 function t($s) {
138
139         $a = get_app();
140
141         if(x($a->strings,$s)) {
142                 $t = $a->strings[$s];
143                 return is_array($t)?$t[0]:$t;
144         }
145         return $s;
146 }}
147
148 if(! function_exists('tt')){
149 function tt($singular, $plural, $count){
150         global $lang;
151         $a = get_app();
152
153         if(x($a->strings,$singular)) {
154                 $t = $a->strings[$singular];
155                 $f = 'string_plural_select_' . str_replace('-','_',$lang);
156                 if(! function_exists($f))
157                         $f = 'string_plural_select_default';
158                 $k = $f($count);
159                 return is_array($t)?$t[$k]:$t;
160         }
161
162         if ($count!=1){
163                 return $plural;
164         } else {
165                 return $singular;
166         }
167 }}
168
169 // provide a fallback which will not collide with
170 // a function defined in any language file
171
172 if(! function_exists('string_plural_select_default')) {
173 function string_plural_select_default($n) {
174         return ($n != 1);
175 }}
176
177
178 /**
179  * Return installed languages as associative array
180  * [
181  *              lang => lang,
182  *              ...
183  * ]
184  */
185 function get_avaiable_languages() {
186         $lang_choices = array();
187         $langs = glob('view/lang/*/strings.php'); /**/
188
189         if(is_array($langs) && count($langs)) {
190                 if(! in_array('view/lang/en/strings.php',$langs))
191                         $langs[] = 'view/lang/en/';
192                 asort($langs);
193                 foreach($langs as $l) {
194                         $t = explode("/",$l);
195                         $lang_choices[$t[1]] = $t[1];
196                 }
197         }
198         return $lang_choices;
199 }