]> git.mxchange.org Git - friendica.git/blob - include/pgettext.php
pgettext needs dba.php for q()
[friendica.git] / include / pgettext.php
1 <?php
2
3 require_once("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                 // create a list like "en" => 0.8
30                 $langs = array_combine($lang_parse[1], $lang_parse[4]);
31         
32                 // set default to 1 for any without q factor
33                 foreach ($langs as $lang => $val) {
34                 if ($val === '') $langs[$lang] = 1;
35                 }
36
37                 // sort list based on value     
38                 arsort($langs, SORT_NUMERIC);
39         }
40         }
41
42         if(isset($langs) && count($langs)) {
43                 foreach ($langs as $lang => $v) {
44                         if(file_exists("view/$lang") && is_dir("view/$lang")) {
45                                 $preferred = $lang;
46                                 break;
47                         }
48                 }
49         }
50
51         if(isset($preferred))
52                 return $preferred;
53
54     $a = get_app();
55         return ((isset($a->config['system']['language'])) ? $a->config['system']['language'] : 'en');
56 }}
57
58
59 function push_lang($language) {
60         global $lang, $a;
61
62         $a->langsave = $lang;
63
64         if($language === $lang)
65                 return;
66
67         if(isset($a->strings) && count($a->strings)) {
68                 $a->stringsave = $a->strings;
69         }
70         $a->strings = array();
71         load_translation_table($language);
72         $lang = $language;
73 }
74
75 function pop_lang() {
76         global $lang, $a;
77
78         if($lang === $a->langsave)
79                 return;
80
81         if(isset($a->stringsave))
82                 $a->strings = $a->stringsave;
83         else
84                 $a->strings = array();
85
86         $lang = $a->langsave;
87 }
88
89
90 // load string translation table for alternate language
91
92 if(! function_exists('load_translation_table')) {
93 function load_translation_table($lang) {
94         global $a;
95          
96         $a->strings = array();
97         if(file_exists("view/$lang/strings.php")) {
98                 include("view/$lang/strings.php");
99         }
100         // load enabled plugins strings
101         $plugins = q("SELECT name FROM addon WHERE installed=1;");
102         if ($plugins!==false) {
103                 foreach($plugins as $p) {
104                         $name = $p['name'];
105                         if(file_exists("addon/$name/lang/$lang/strings.php")) {
106                                 include("addon/$name/lang/$lang/strings.php");
107                         }
108                 }
109         }
110 }}
111
112 // translate string if translation exists
113
114 if(! function_exists('t')) {
115 function t($s) {
116
117         $a = get_app();
118
119         if(x($a->strings,$s)) {
120                 $t = $a->strings[$s];
121                 return is_array($t)?$t[0]:$t;
122         }
123         return $s;
124 }}
125
126 if(! function_exists('tt')){
127 function tt($singular, $plural, $count){
128         global $lang;
129         $a = get_app();
130
131         if(x($a->strings,$singular)) {
132                 $t = $a->strings[$singular];
133                 $f = 'string_plural_select_' . str_replace('-','_',$lang);
134                 if(! function_exists($f))
135                         $f = 'string_plural_select_default';
136                 $k = $f($count);
137                 return is_array($t)?$t[$k]:$t;
138         }
139         
140         if ($count!=1){
141                 return $plural;
142         } else {
143                 return $singular;
144         }
145 }}
146
147 // provide a fallback which will not collide with 
148 // a function defined in any language file 
149
150 if(! function_exists('string_plural_select_default')) {
151 function string_plural_select_default($n) {
152         return ($n != 1);
153 }}
154