]> git.mxchange.org Git - friendica.git/blob - src/Core/L10n.php
Remove extraneous dba::connect calls
[friendica.git] / src / Core / L10n.php
1 <?php
2 /**
3  * @file src/Core/L10n.php
4  */
5 namespace Friendica\Core;
6
7 use Friendica\Core\Config;
8 use dba;
9
10 require_once 'boot.php';
11 require_once 'include/dba.php';
12
13 /**
14  * Provide Languange, Translation, and Localisation functions to the application
15  * Localisation can be referred to by the numeronym L10N (as in: "L", followed by ten more letters, and then "N").
16  */
17 class L10n
18 {
19         /**
20          * @brief get the prefered language from the HTTP_ACCEPT_LANGUAGE header
21          */
22         public static function getBrowserLanguage()
23         {
24                 $lang_list = [];
25
26                 if (x($_SERVER, 'HTTP_ACCEPT_LANGUAGE')) {
27                         // break up string into pieces (languages and q factors)
28                         preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
29
30                         if (count($lang_parse[1])) {
31                                 // go through the list of prefered languages and add a generic language
32                                 // for sub-linguas (e.g. de-ch will add de) if not already in array
33                                 for ($i = 0; $i < count($lang_parse[1]); $i++) {
34                                         $lang_list[] = strtolower($lang_parse[1][$i]);
35                                         if (strlen($lang_parse[1][$i])>3) {
36                                                 $dashpos = strpos($lang_parse[1][$i], '-');
37                                                 if (!in_array(substr($lang_parse[1][$i], 0, $dashpos), $lang_list)) {
38                                                         $lang_list[] = strtolower(substr($lang_parse[1][$i], 0, $dashpos));
39                                                 }
40                                         }
41                                 }
42                         }
43                 }
44
45                 // check if we have translations for the preferred languages and pick the 1st that has
46                 foreach ($lang_list as $lang) {
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          * @param string $language language
62          */
63         public static function pushLang($language)
64         {
65                 global $lang, $a;
66
67                 $a->langsave = $lang;
68
69                 if ($language === $lang) {
70                         return;
71                 }
72
73                 if (isset($a->strings) && count($a->strings)) {
74                         $a->stringsave = $a->strings;
75                 }
76                 $a->strings = [];
77                 self::loadTranslationTable($language);
78                 $lang = $language;
79         }
80
81         /**
82          * Pop language off the top of the stack
83          */
84         public static function popLang()
85         {
86                 global $lang, $a;
87
88                 if ($lang === $a->langsave) {
89                         return;
90                 }
91
92                 if (isset($a->stringsave)) {
93                         $a->strings = $a->stringsave;
94                 } else {
95                         $a->strings = [];
96                 }
97
98                 $lang = $a->langsave;
99         }
100
101         /**
102          * load string translation table for alternate language
103          *
104          * first addon strings are loaded, then globals
105          *
106          * @param string $lang language code to load
107          */
108         public static function loadTranslationTable($lang)
109         {
110                 $a = get_app();
111
112                 $a->strings = [];
113                 // load enabled addons strings
114                 $addons = dba::select('addon', ['name'], ['installed' => true]);
115                 while ($p = dba::fetch($addons)) {
116                         $name = $p['name'];
117                         if (file_exists("addon/$name/lang/$lang/strings.php")) {
118                                 include "addon/$name/lang/$lang/strings.php";
119                         }
120                 }
121
122                 if (file_exists("view/lang/$lang/strings.php")) {
123                         include "view/lang/$lang/strings.php";
124                 }
125         }
126
127         /**
128          * @brief Return the localized version of the provided string with optional string interpolation
129          *
130          * This function takes a english string as parameter, and if a localized version
131          * exists for the current language, substitutes it before performing an eventual
132          * string interpolation (sprintf) with additional optional arguments.
133          *
134          * Usages:
135          * - L10n::t('This is an example')
136          * - L10n::t('URL %s returned no result', $url)
137          * - L10n::t('Current version: %s, new version: %s', $current_version, $new_version)
138          *
139          * @param string $s
140          * @param array  $vars Variables to interpolate in the translation string
141          * @return string
142          */
143         public static function t($s, ...$vars)
144         {
145                 $a = get_app();
146
147                 if (empty($s)) {
148                         return '';
149                 }
150
151                 if (x($a->strings, $s)) {
152                         $t = $a->strings[$s];
153                         $s = is_array($t) ? $t[0] : $t;
154                 }
155
156                 if (count($vars) > 0) {
157                         $s = sprintf($s, ...$vars);
158                 }
159
160                 return $s;
161         }
162
163         /**
164          * @brief Return the localized version of a singular/plural string with optional string interpolation
165          *
166          * This function takes two english strings as parameters, singular and plural, as
167          * well as a count. If a localized version exists for the current language, they
168          * are used instead. Discrimination between singular and plural is done using the
169          * localized function if any or the default one. Finally, a string interpolation
170          * is performed using the count as parameter.
171          *
172          * Usages:
173          * - L10n::tt('Like', 'Likes', $count)
174          * - L10n::tt("%s user deleted", "%s users deleted", count($users))
175          *
176          * @global type $lang
177          * @param string $singular
178          * @param string $plural
179          * @param int $count
180          * @return string
181          */
182         public static function tt($singular, $plural, $count)
183         {
184                 global $lang;
185                 $a = get_app();
186
187                 if (x($a->strings, $singular)) {
188                         $t = $a->strings[$singular];
189                         if (is_array($t)) {
190                                 $plural_function = 'string_plural_select_' . str_replace('-', '_', $lang);
191                                 if (function_exists($plural_function)) {
192                                         $i = $plural_function($count);
193                                 } else {
194                                         $i = self::stringPluralSelectDefault($count);
195                                 }
196                                 $s = $t[$i];
197                         } else {
198                                 $s = $t;
199                         }
200                 } elseif (self::stringPluralSelectDefault($count)) {
201                         $s = $plural;
202                 } else {
203                         $s = $singular;
204                 }
205
206                 $s = @sprintf($s, $count);
207
208                 return $s;
209         }
210
211         /**
212          * Provide a fallback which will not collide with a function defined in any language file
213          */
214         private static function stringPluralSelectDefault($n)
215         {
216                 return $n != 1;
217         }
218
219
220
221         /**
222          * @brief Return installed languages codes as associative array
223          *
224          * Scans the view/lang directory for the existence of "strings.php" files, and
225          * returns an alphabetical list of their folder names (@-char language codes).
226          * Adds the english language if it's missing from the list.
227          *
228          * Ex: array('de' => 'de', 'en' => 'en', 'fr' => 'fr', ...)
229          *
230          * @return array
231          */
232         public static function getAvailableLanguages()
233         {
234                 $langs = [];
235                 $strings_file_paths = glob('view/lang/*/strings.php');
236
237                 if (is_array($strings_file_paths) && count($strings_file_paths)) {
238                         if (!in_array('view/lang/en/strings.php', $strings_file_paths)) {
239                                 $strings_file_paths[] = 'view/lang/en/strings.php';
240                         }
241                         asort($strings_file_paths);
242                         foreach ($strings_file_paths as $strings_file_path) {
243                                 $path_array = explode('/', $strings_file_path);
244                                 $langs[$path_array[2]] = $path_array[2];
245                         }
246                 }
247                 return $langs;
248         }
249 }