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