3 * @file src/Core/L10n.php
5 namespace Friendica\Core;
7 use Friendica\BaseObject;
8 use Friendica\Database\DBA;
9 use Friendica\Core\System;
11 require_once 'boot.php';
12 require_once 'include/dba.php';
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").
18 class L10n extends BaseObject
21 * @brief get the prefered language from the HTTP_ACCEPT_LANGUAGE header
23 public static function getBrowserLanguage()
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);
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));
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"))) {
53 if (isset($preferred)) {
57 // in case none matches, get the system wide configured language, or fall back to English
58 return Config::get('system', 'language', 'en');
62 * @param string $language language
64 public static function pushLang($language)
68 $a->langsave = Config::get('system', 'language');
70 if ($language === $a->langsave) {
74 if (isset($a->strings) && count($a->strings)) {
75 $a->stringsave = $a->strings;
78 self::loadTranslationTable($language);
79 Config::set('system', 'language', $language);
83 * Pop language off the top of the stack
85 public static function popLang()
89 if (Config::get('system', 'language') === $a->langsave) {
93 if (isset($a->stringsave)) {
94 $a->strings = $a->stringsave;
99 Config::set('system', 'language', $a->langsave);
103 * load string translation table for alternate language
105 * first addon strings are loaded, then globals
107 * @param string $lang language code to load
109 public static function loadTranslationTable($lang)
114 // load enabled addons strings
115 $addons = DBA::select('addon', ['name'], ['installed' => true]);
116 while ($p = DBA::fetch($addons)) {
118 if (file_exists("addon/$name/lang/$lang/strings.php")) {
119 include "addon/$name/lang/$lang/strings.php";
123 if (file_exists("view/lang/$lang/strings.php")) {
124 include "view/lang/$lang/strings.php";
129 * @brief Return the localized version of the provided string with optional string interpolation
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.
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)
141 * @param array $vars Variables to interpolate in the translation string
144 public static function t($s, ...$vars)
152 if (x($a->strings, $s)) {
153 $t = $a->strings[$s];
154 $s = is_array($t) ? $t[0] : $t;
157 if (count($vars) > 0) {
158 $s = sprintf($s, ...$vars);
165 * @brief Return the localized version of a singular/plural string with optional string interpolation
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.
174 * - L10n::tt('Like', 'Likes', $count)
175 * - L10n::tt("%s user deleted", "%s users deleted", count($users))
177 * @param string $singular
178 * @param string $plural
182 public static function tt($singular, $plural, $count)
186 if (!is_numeric($count)) {
187 logger('Non numeric count called by ' . System::callstack(20));
190 $lang = Config::get('system', 'language');
192 if (!empty($a->strings[$singular])) {
193 $t = $a->strings[$singular];
195 $plural_function = 'string_plural_select_' . str_replace('-', '_', $lang);
196 if (function_exists($plural_function)) {
197 $i = $plural_function($count);
199 $i = self::stringPluralSelectDefault($count);
202 // for some languages there is only a single array item
203 if (!isset($t[$i])) {
211 } elseif (self::stringPluralSelectDefault($count)) {
217 $s = @sprintf($s, $count);
223 * Provide a fallback which will not collide with a function defined in any language file
225 private static function stringPluralSelectDefault($n)
233 * @brief Return installed languages codes as associative array
235 * Scans the view/lang directory for the existence of "strings.php" files, and
236 * returns an alphabetical list of their folder names (@-char language codes).
237 * Adds the english language if it's missing from the list.
239 * Ex: array('de' => 'de', 'en' => 'en', 'fr' => 'fr', ...)
243 public static function getAvailableLanguages()
246 $strings_file_paths = glob('view/lang/*/strings.php');
248 if (is_array($strings_file_paths) && count($strings_file_paths)) {
249 if (!in_array('view/lang/en/strings.php', $strings_file_paths)) {
250 $strings_file_paths[] = 'view/lang/en/strings.php';
252 asort($strings_file_paths);
253 foreach ($strings_file_paths as $strings_file_path) {
254 $path_array = explode('/', $strings_file_path);
255 $langs[$path_array[2]] = $path_array[2];