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