]> git.mxchange.org Git - friendica.git/blob - include/pgettext.php
de7cdda9e8575cfa41a3d117062225666e8e2ca2
[friendica.git] / include / pgettext.php
1 <?php
2
3 /**
4  * @brief translation support
5  *
6  * Get the language setting directly from system variables, bypassing get_config()
7  * as database may not yet be configured.
8  *
9  * If possible, we use the value from the browser.
10  *
11  */
12
13 require_once("include/dba.php");
14
15 if(! function_exists('get_browser_language')) {
16 /**
17  * @brief get the prefered language from the HTTP_ACCEPT_LANGUAGE header
18  */
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                 $lang_list = [];
27                 if (count($lang_parse[1])) {
28                         // go through the list of prefered languages and add a generic language
29                         // for sub-linguas (e.g. de-ch will add de) if not already in array
30                         for ($i=0; $i<count($lang_parse[1]); $i++) {
31                                 $lang_list[] = strtolower($lang_parse[1][$i]);
32                                 if ( strlen($lang_parse[1][$i])>3 ) {
33                                         $dashpos = strpos($lang_parse[1][$i], '-');
34                                         if (! in_array(substr($lang_parse[1][$i], 0, $dashpos), $lang_list ) ) {
35                                                 $lang_list[] = strtolower(substr($lang_parse[1][$i], 0, $dashpos));
36                                         }
37                                 }
38                         }
39                 }
40         }
41
42         // check if we have translations for the preferred languages and pick the 1st that has
43         for ($i=0; $i<count($lang_list); $i++) {
44                 $lang = $lang_list[$i];
45                 if(file_exists("view/lang/$lang") && is_dir("view/lang/$lang")) {
46                         $preferred = $lang;
47                         break;
48                 }
49         }
50         if(isset($preferred))
51                 return $preferred;
52
53         // in case none matches, get the system wide configured language, or fall back to English
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 // l
91
92 if(! function_exists('load_translation_table')) {
93 /**
94  * load string translation table for alternate language
95  *
96  * first plugin strings are loaded, then globals
97  *
98  * @param string $lang language code to load
99  */
100 function load_translation_table($lang) {
101         global $a;
102
103         $a->strings = array();
104         // load enabled plugins strings
105         $plugins = q("SELECT name FROM addon WHERE installed=1;");
106         if ($plugins!==false) {
107                 foreach($plugins as $p) {
108                         $name = $p['name'];
109                         if(file_exists("addon/$name/lang/$lang/strings.php")) {
110                                 include("addon/$name/lang/$lang/strings.php");
111                         }
112                 }
113         }
114
115         if(file_exists("view/lang/$lang/strings.php")) {
116                 include("view/lang/$lang/strings.php");
117         }
118
119 }}
120
121 // translate string if translation exists
122
123 if(! function_exists('t')) {
124 function t($s) {
125
126         $a = get_app();
127
128         if(x($a->strings,$s)) {
129                 $t = $a->strings[$s];
130                 return is_array($t)?$t[0]:$t;
131         }
132         return $s;
133 }}
134
135 if(! function_exists('tt')){
136 function tt($singular, $plural, $count){
137         global $lang;
138         $a = get_app();
139
140         if(x($a->strings,$singular)) {
141                 $t = $a->strings[$singular];
142                 $f = 'string_plural_select_' . str_replace('-','_',$lang);
143                 if(! function_exists($f))
144                         $f = 'string_plural_select_default';
145                 $k = $f($count);
146                 return is_array($t)?$t[$k]:$t;
147         }
148
149         if ($count!=1){
150                 return $plural;
151         } else {
152                 return $singular;
153         }
154 }}
155
156 // provide a fallback which will not collide with
157 // a function defined in any language file
158
159 if(! function_exists('string_plural_select_default')) {
160 function string_plural_select_default($n) {
161         return ($n != 1);
162 }}
163
164
165
166 /**
167  * @brief Return installed languages codes as associative array
168  *
169  * Scans the view/lang directory for the existence of "strings.php" files, and
170  * returns an alphabetical list of their folder names (@-char language codes).
171  * Adds the english language if it's missing from the list.
172  *
173  * Ex: array('de' => 'de', 'en' => 'en', 'fr' => 'fr', ...)
174  *
175  * @return array
176  */
177 function get_available_languages() {
178         $langs = array();
179         $strings_file_paths = glob('view/lang/*/strings.php');
180
181         if (is_array($strings_file_paths) && count($strings_file_paths)) {
182                 if (!in_array('view/lang/en/strings.php', $strings_file_paths)) {
183                         $strings_file_paths[] = 'view/lang/en/strings.php';
184                 }
185                 asort($strings_file_paths);
186                 foreach($strings_file_paths as $strings_file_path) {
187                         $path_array = explode('/', $strings_file_path);
188                         $langs[$path_array[2]] = $path_array[2];
189                 }
190         }
191         return $langs;
192 }