]> git.mxchange.org Git - friendica.git/blob - include/pgettext.php
The function "construct_acl_data" isn't used at all, it seems. it is deactivated now
[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 use Friendica\Core\Config;
14
15 require_once "include/dba.php";
16
17 if (! function_exists('get_browser_language')) {
18 /**
19  * @brief get the prefered language from the HTTP_ACCEPT_LANGUAGE header
20  */
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                 $lang_list = [];
29                 if (count($lang_parse[1])) {
30                         // go through the list of prefered languages and add a generic language
31                         // for sub-linguas (e.g. de-ch will add de) if not already in array
32                         for ($i = 0; $i < count($lang_parse[1]); $i++) {
33                                 $lang_list[] = strtolower($lang_parse[1][$i]);
34                                 if (strlen($lang_parse[1][$i])>3 ) {
35                                         $dashpos = strpos($lang_parse[1][$i], '-');
36                                         if (!in_array(substr($lang_parse[1][$i], 0, $dashpos), $lang_list ) ) {
37                                                 $lang_list[] = strtolower(substr($lang_parse[1][$i], 0, $dashpos));
38                                         }
39                                 }
40                         }
41                 }
42         }
43
44         // check if we have translations for the preferred languages and pick the 1st that has
45         for ($i = 0; $i < count($lang_list); $i++) {
46                 $lang = $lang_list[$i];
47                 if ($lang === 'en' || (file_exists("view/lang/$lang") && is_dir("view/lang/$lang"))) {
48                         $preferred = $lang;
49                         break;
50                 }
51         }
52         if (isset($preferred)) {
53                 return $preferred;
54         }
55
56         // in case none matches, get the system wide configured language, or fall back to English
57         return Config::get('system', 'language', 'en');
58 }}
59
60
61 function push_lang($language) {
62         global $lang, $a;
63
64         $a->langsave = $lang;
65
66         if ($language === $lang) {
67                 return;
68         }
69
70         if (isset($a->strings) && count($a->strings)) {
71                 $a->stringsave = $a->strings;
72         }
73         $a->strings = array();
74         load_translation_table($language);
75         $lang = $language;
76 }
77
78 function pop_lang() {
79         global $lang, $a;
80
81         if ($lang === $a->langsave) {
82                 return;
83         }
84
85         if (isset($a->stringsave)) {
86                 $a->strings = $a->stringsave;
87         } else {
88                 $a->strings = array();
89         }
90
91         $lang = $a->langsave;
92 }
93
94
95 // l
96
97 if (! function_exists('load_translation_table')) {
98 /**
99  * load string translation table for alternate language
100  *
101  * first plugin strings are loaded, then globals
102  *
103  * @param string $lang language code to load
104  */
105 function load_translation_table($lang) {
106         $a = get_app();
107
108         $a->strings = array();
109         // load enabled plugins strings
110         $plugins = q("SELECT name FROM addon WHERE installed=1;");
111         if ($plugins!==false) {
112                 foreach ($plugins as $p) {
113                         $name = $p['name'];
114                         if (file_exists("addon/$name/lang/$lang/strings.php")) {
115                                 include("addon/$name/lang/$lang/strings.php");
116                         }
117                 }
118         }
119
120         if (file_exists("view/lang/$lang/strings.php")) {
121                 include("view/lang/$lang/strings.php");
122         }
123
124 }}
125
126 // translate string if translation exists
127
128 if (! function_exists('t')) {
129 function t($s) {
130
131         $a = get_app();
132
133         if (x($a->strings,$s)) {
134                 $t = $a->strings[$s];
135                 return is_array($t)?$t[0]:$t;
136         }
137         return $s;
138 }}
139
140 if (! function_exists('tt')){
141 function tt($singular, $plural, $count){
142         global $lang;
143         $a = get_app();
144
145         if (x($a->strings,$singular)) {
146                 $t = $a->strings[$singular];
147                 $f = 'string_plural_select_' . str_replace('-','_',$lang);
148                 if (! function_exists($f))
149                         $f = 'string_plural_select_default';
150                 $k = $f($count);
151                 return is_array($t)?$t[$k]:$t;
152         }
153
154         if ($count!=1){
155                 return $plural;
156         } else {
157                 return $singular;
158         }
159 }}
160
161 // provide a fallback which will not collide with
162 // a function defined in any language file
163
164 if (! function_exists('string_plural_select_default')) {
165 function string_plural_select_default($n) {
166         return ($n != 1);
167 }}
168
169
170
171 /**
172  * @brief Return installed languages codes as associative array
173  *
174  * Scans the view/lang directory for the existence of "strings.php" files, and
175  * returns an alphabetical list of their folder names (@-char language codes).
176  * Adds the english language if it's missing from the list.
177  *
178  * Ex: array('de' => 'de', 'en' => 'en', 'fr' => 'fr', ...)
179  *
180  * @return array
181  */
182 function get_available_languages() {
183         $langs = array();
184         $strings_file_paths = glob('view/lang/*/strings.php');
185
186         if (is_array($strings_file_paths) && count($strings_file_paths)) {
187                 if (!in_array('view/lang/en/strings.php', $strings_file_paths)) {
188                         $strings_file_paths[] = 'view/lang/en/strings.php';
189                 }
190                 asort($strings_file_paths);
191                 foreach ($strings_file_paths as $strings_file_path) {
192                         $path_array = explode('/', $strings_file_path);
193                         $langs[$path_array[2]] = $path_array[2];
194                 }
195         }
196         return $langs;
197 }