]> git.mxchange.org Git - friendica.git/blob - include/pgettext.php
3a89c024d77c851b7245c7483ae1801e6eab9517
[friendica.git] / include / pgettext.php
1 <?php
2
3 /**
4  * @brief translation support
5  *
6  * Get the language setting directly from system variables, bypassing Config::get()
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 /**
18  * @brief get the prefered language from the HTTP_ACCEPT_LANGUAGE header
19  */
20 function get_browser_language() {
21
22         if (x($_SERVER, 'HTTP_ACCEPT_LANGUAGE')) {
23                 // break up string into pieces (languages and q factors)
24                 preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
25                         $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
26
27                 $lang_list = [];
28                 if (count($lang_parse[1])) {
29                         // go through the list of prefered languages and add a generic language
30                         // for sub-linguas (e.g. de-ch will add de) if not already in array
31                         for ($i = 0; $i < count($lang_parse[1]); $i++) {
32                                 $lang_list[] = strtolower($lang_parse[1][$i]);
33                                 if (strlen($lang_parse[1][$i])>3 ) {
34                                         $dashpos = strpos($lang_parse[1][$i], '-');
35                                         if (!in_array(substr($lang_parse[1][$i], 0, $dashpos), $lang_list ) ) {
36                                                 $lang_list[] = strtolower(substr($lang_parse[1][$i], 0, $dashpos));
37                                         }
38                                 }
39                         }
40                 }
41         }
42
43         // check if we have translations for the preferred languages and pick the 1st that has
44         for ($i = 0; $i < count($lang_list); $i++) {
45                 $lang = $lang_list[$i];
46                 if ($lang === 'en' || (file_exists("view/lang/$lang") && is_dir("view/lang/$lang"))) {
47                         $preferred = $lang;
48                         break;
49                 }
50         }
51         if (isset($preferred)) {
52                 return $preferred;
53         }
54
55         // in case none matches, get the system wide configured language, or fall back to English
56         return Config::get('system', 'language', 'en');
57 }
58
59
60 function push_lang($language) {
61         global $lang, $a;
62
63         $a->langsave = $lang;
64
65         if ($language === $lang) {
66                 return;
67         }
68
69         if (isset($a->strings) && count($a->strings)) {
70                 $a->stringsave = $a->strings;
71         }
72         $a->strings = array();
73         load_translation_table($language);
74         $lang = $language;
75 }
76
77 function pop_lang() {
78         global $lang, $a;
79
80         if ($lang === $a->langsave) {
81                 return;
82         }
83
84         if (isset($a->stringsave)) {
85                 $a->strings = $a->stringsave;
86         } else {
87                 $a->strings = array();
88         }
89
90         $lang = $a->langsave;
91 }
92
93 // l
94
95 /**
96  * load string translation table for alternate language
97  *
98  * first plugin strings are loaded, then globals
99  *
100  * @param string $lang language code to load
101  */
102 function load_translation_table($lang) {
103         $a = get_app();
104
105         $a->strings = array();
106         // load enabled plugins strings
107         $plugins = dba::select('addon', array('name'), array('installed' => true));
108         while ($p = dba::fetch($plugins)) {
109                 $name = $p['name'];
110                 if (file_exists("addon/$name/lang/$lang/strings.php")) {
111                         include("addon/$name/lang/$lang/strings.php");
112                 }
113         }
114
115         if (file_exists("view/lang/$lang/strings.php")) {
116                 include("view/lang/$lang/strings.php");
117         }
118
119 }
120
121 /**
122  * @brief Return the localized version of the provided string with optional string interpolation
123  *
124  * This function takes a english string as parameter, and if a localized version
125  * exists for the current language, substitutes it before performing an eventual
126  * string interpolation (sprintf) with additional optional arguments.
127  *
128  * Usages:
129  * - t('This is an example')
130  * - t('URL %s returned no result', $url)
131  * - t('Current version: %s, new version: %s', $current_version, $new_version)
132  *
133  * @param string $s
134  * @return string
135  */
136 function t($s)
137 {
138         $a = get_app();
139
140         if (x($a->strings, $s)) {
141                 $t = $a->strings[$s];
142                 $s = is_array($t) ? $t[0] : $t;
143         }
144         if (func_num_args() > 1) {
145                 $args = array_slice(func_get_args(), 1);
146                 $s = @vsprintf($s, $args);
147         }
148
149         return $s;
150 }
151
152 /**
153  * @brief Return the localized version of a singular/plural string with optional string interpolation
154  *
155  * This function takes two english strings as parameters, singular and plural, as
156  * well as a count. If a localized version exists for the current language, they
157  * are used instead. Discrimination between singular and plural is done using the
158  * localized function if any or the default one. Finally, a string interpolation
159  * is performed using the count as parameter.
160  *
161  * Usages:
162  * - tt('Like', 'Likes', $count)
163  * - tt("%s user deleted", "%s users deleted", count($users))
164  *
165  * @global type $lang
166  * @param string $singular
167  * @param string $plural
168  * @param int $count
169  * @return string
170  */
171 function tt($singular, $plural, $count)
172 {
173         global $lang;
174         $a = get_app();
175
176         if (x($a->strings, $singular)) {
177                 $t = $a->strings[$singular];
178                 if (is_array($t)) {
179                         $plural_function = 'string_plural_select_' . str_replace('-', '_', $lang);
180                         if (function_exists($plural_function)) {
181                                 $plural_function = 'string_plural_select_default';
182                         }
183                         $i = $plural_function($count);
184                         $s = $t[$i];
185                 } else {
186                         $s = $t;
187                 }
188         } elseif (string_plural_select_default($count)) {
189                 $s = $plural;
190         } else {
191                 $s = $singular;
192         }
193
194         $s = @sprintf($s, $count);
195
196         return $s;
197 }
198
199 // provide a fallback which will not collide with
200 // a function defined in any language file
201 function string_plural_select_default($n)
202 {
203         return $n != 1;
204 }
205
206
207
208 /**
209  * @brief Return installed languages codes as associative array
210  *
211  * Scans the view/lang directory for the existence of "strings.php" files, and
212  * returns an alphabetical list of their folder names (@-char language codes).
213  * Adds the english language if it's missing from the list.
214  *
215  * Ex: array('de' => 'de', 'en' => 'en', 'fr' => 'fr', ...)
216  *
217  * @return array
218  */
219 function get_available_languages() {
220         $langs = array();
221         $strings_file_paths = glob('view/lang/*/strings.php');
222
223         if (is_array($strings_file_paths) && count($strings_file_paths)) {
224                 if (!in_array('view/lang/en/strings.php', $strings_file_paths)) {
225                         $strings_file_paths[] = 'view/lang/en/strings.php';
226                 }
227                 asort($strings_file_paths);
228                 foreach ($strings_file_paths as $strings_file_path) {
229                         $path_array = explode('/', $strings_file_path);
230                         $langs[$path_array[2]] = $path_array[2];
231                 }
232         }
233         return $langs;
234 }