]> git.mxchange.org Git - friendica.git/blob - src/Core/L10n.php
Improve Console/Config display for array values
[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 extends \Friendica\BaseObject
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                 $a = self::getApp();
66
67                 $a->langsave = Config::get('system', 'language');
68
69                 if ($language === $a->langsave) {
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                 Config::set('system', 'language', $language);
79         }
80
81         /**
82          * Pop language off the top of the stack
83          */
84         public static function popLang()
85         {
86                 $a = self::getApp();
87
88                 if (Config::get('system', 'language') === $a->langsave) {
89                         return;
90                 }
91
92                 if (isset($a->stringsave)) {
93                         $a->strings = $a->stringsave;
94                 } else {
95                         $a->strings = [];
96                 }
97
98                 Config::set('system', 'language', $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 = self::getApp();
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 = self::getApp();
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          * @param string $singular
177          * @param string $plural
178          * @param int $count
179          * @return string
180          */
181         public static function tt($singular, $plural, $count)
182         {
183                 $lang = Config::get('system', 'language');
184
185                 if (!empty($a->strings[$singular])) {
186                         $t = $a->strings[$singular];
187                         if (is_array($t)) {
188                                 $plural_function = 'string_plural_select_' . str_replace('-', '_', $lang);
189                                 if (function_exists($plural_function)) {
190                                         $i = $plural_function($count);
191                                 } else {
192                                         $i = self::stringPluralSelectDefault($count);
193                                 }
194                                 $s = $t[$i];
195                         } else {
196                                 $s = $t;
197                         }
198                 } elseif (self::stringPluralSelectDefault($count)) {
199                         $s = $plural;
200                 } else {
201                         $s = $singular;
202                 }
203
204                 $s = @sprintf($s, $count);
205
206                 return $s;
207         }
208
209         /**
210          * Provide a fallback which will not collide with a function defined in any language file
211          */
212         private static function stringPluralSelectDefault($n)
213         {
214                 return $n != 1;
215         }
216
217
218
219         /**
220          * @brief Return installed languages codes as associative array
221          *
222          * Scans the view/lang directory for the existence of "strings.php" files, and
223          * returns an alphabetical list of their folder names (@-char language codes).
224          * Adds the english language if it's missing from the list.
225          *
226          * Ex: array('de' => 'de', 'en' => 'en', 'fr' => 'fr', ...)
227          *
228          * @return array
229          */
230         public static function getAvailableLanguages()
231         {
232                 $langs = [];
233                 $strings_file_paths = glob('view/lang/*/strings.php');
234
235                 if (is_array($strings_file_paths) && count($strings_file_paths)) {
236                         if (!in_array('view/lang/en/strings.php', $strings_file_paths)) {
237                                 $strings_file_paths[] = 'view/lang/en/strings.php';
238                         }
239                         asort($strings_file_paths);
240                         foreach ($strings_file_paths as $strings_file_path) {
241                                 $path_array = explode('/', $strings_file_path);
242                                 $langs[$path_array[2]] = $path_array[2];
243                         }
244                 }
245                 return $langs;
246         }
247 }