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