]> git.mxchange.org Git - friendica.git/blob - include/pgettext.php
Merge branch 'master' of https://github.com/friendica/friendica
[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_language')) {
19 function get_language() {
20
21         if (isset($_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         return ((isset($a->config['system']['language'])) ? $a->config['system']['language'] : 'en');
53 }}
54
55
56 function push_lang($language) {
57         global $lang, $a;
58
59         $a->langsave = $lang;
60
61         if($language === $lang)
62                 return;
63
64         if(isset($a->strings) && count($a->strings)) {
65                 $a->stringsave = $a->strings;
66         }
67         $a->strings = array();
68         load_translation_table($language);
69         $lang = $language;
70 }
71
72 function pop_lang() {
73         global $lang, $a;
74
75         if($lang === $a->langsave)
76                 return;
77
78         if(isset($a->stringsave))
79                 $a->strings = $a->stringsave;
80         else
81                 $a->strings = array();
82
83         $lang = $a->langsave;
84 }
85
86
87 // load string translation table for alternate language
88
89 if(! function_exists('load_translation_table')) {
90 function load_translation_table($lang) {
91         global $a;
92
93         if(file_exists("view/$lang/strings.php")) {
94                 include("view/$lang/strings.php");
95         }
96         else
97                 $a->strings = array();
98 }}
99
100 // translate string if translation exists
101
102 if(! function_exists('t')) {
103 function t($s) {
104
105         $a = get_app();
106
107         if(x($a->strings,$s)) {
108                 $t = $a->strings[$s];
109                 return is_array($t)?$t[0]:$t;
110         }
111         return $s;
112 }}
113
114 if(! function_exists('tt')){
115 function tt($singular, $plural, $count){
116         global $lang;
117         $a = get_app();
118
119         if(x($a->strings,$singular)) {
120                 $t = $a->strings[$singular];
121                 $f = 'string_plural_select_' . str_replace('-','_',$lang);
122                 if(! function_exists($f))
123                         $f = 'string_plural_select_default';
124                 $k = $f($count);
125                 return is_array($t)?$t[$k]:$t;
126         }
127         
128         if ($count!=1){
129                 return $plural;
130         } else {
131                 return $singular;
132         }
133 }}
134
135 // provide a fallback which will not collide with 
136 // a function defined in any language file 
137
138 if(! function_exists('string_plural_select_default')) {
139 function string_plural_select_default($n) {
140         return ($n != 1);
141 }}
142