3 * @file src/Core/L10n.php
5 namespace Friendica\Core;
7 use Friendica\Core\Config;
10 require_once 'boot.php';
11 require_once 'include/dba.php';
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").
20 * @brief get the prefered language from the HTTP_ACCEPT_LANGUAGE header
22 public static function getBrowserLanguage()
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);
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));
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"))) {
52 if (isset($preferred)) {
56 // in case none matches, get the system wide configured language, or fall back to English
57 return Config::get('system', 'language', 'en');
61 * @param string $language language
63 public static function pushLang($language)
69 if ($language === $lang) {
73 if (isset($a->strings) && count($a->strings)) {
74 $a->stringsave = $a->strings;
77 self::loadTranslationTable($language);
82 * Pop language off the top of the stack
84 public static function popLang()
88 if ($lang === $a->langsave) {
92 if (isset($a->stringsave)) {
93 $a->strings = $a->stringsave;
102 * load string translation table for alternate language
104 * first addon strings are loaded, then globals
106 * @param string $lang language code to load
108 public static function loadTranslationTable($lang)
113 // load enabled addons strings
114 $addons = dba::select('addon', ['name'], ['installed' => true]);
115 while ($p = dba::fetch($addons)) {
117 if (file_exists("addon/$name/lang/$lang/strings.php")) {
118 include "addon/$name/lang/$lang/strings.php";
122 if (file_exists("view/lang/$lang/strings.php")) {
123 include "view/lang/$lang/strings.php";
128 * @brief Return the localized version of the provided string with optional string interpolation
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.
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)
140 * @param array $vars Variables to interpolate in the translation string
143 public static function t($s, ...$vars)
147 if (x($a->strings, $s)) {
148 $t = $a->strings[$s];
149 $s = is_array($t) ? $t[0] : $t;
152 if (count($vars) > 0) {
153 $s = sprintf($s, ...$vars);
160 * @brief Return the localized version of a singular/plural string with optional string interpolation
162 * This function takes two english strings as parameters, singular and plural, as
163 * well as a count. If a localized version exists for the current language, they
164 * are used instead. Discrimination between singular and plural is done using the
165 * localized function if any or the default one. Finally, a string interpolation
166 * is performed using the count as parameter.
169 * - L10n::tt('Like', 'Likes', $count)
170 * - L10n::tt("%s user deleted", "%s users deleted", count($users))
173 * @param string $singular
174 * @param string $plural
178 public static function tt($singular, $plural, $count)
183 if (x($a->strings, $singular)) {
184 $t = $a->strings[$singular];
186 $plural_function = 'string_plural_select_' . str_replace('-', '_', $lang);
187 if (function_exists($plural_function)) {
188 $i = $plural_function($count);
190 $i = self::stringPluralSelectDefault($count);
196 } elseif (self::stringPluralSelectDefault($count)) {
202 $s = @sprintf($s, $count);
208 * Provide a fallback which will not collide with a function defined in any language file
210 private static function stringPluralSelectDefault($n)
218 * @brief Return installed languages codes as associative array
220 * Scans the view/lang directory for the existence of "strings.php" files, and
221 * returns an alphabetical list of their folder names (@-char language codes).
222 * Adds the english language if it's missing from the list.
224 * Ex: array('de' => 'de', 'en' => 'en', 'fr' => 'fr', ...)
228 public static function getAvailableLanguages()
231 $strings_file_paths = glob('view/lang/*/strings.php');
233 if (is_array($strings_file_paths) && count($strings_file_paths)) {
234 if (!in_array('view/lang/en/strings.php', $strings_file_paths)) {
235 $strings_file_paths[] = 'view/lang/en/strings.php';
237 asort($strings_file_paths);
238 foreach ($strings_file_paths as $strings_file_path) {
239 $path_array = explode('/', $strings_file_path);
240 $langs[$path_array[2]] = $path_array[2];