3 * @file src/Core/L10n.php
5 namespace Friendica\Core;
7 use Friendica\Core\Config;
10 require_once 'include/dba.php';
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").
19 * @brief get the prefered language from the HTTP_ACCEPT_LANGUAGE header
21 public static function getBrowserLanguage()
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);
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));
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"))) {
51 if (isset($preferred)) {
55 // in case none matches, get the system wide configured language, or fall back to English
56 return Config::get('system', 'language', 'en');
60 * @param string $language language
62 public static function pushLang($language)
68 if ($language === $lang) {
72 if (isset($a->strings) && count($a->strings)) {
73 $a->stringsave = $a->strings;
76 self::loadTranslationTable($language);
81 * Pop language off the top of the stack
83 public static function popLang()
87 if ($lang === $a->langsave) {
91 if (isset($a->stringsave)) {
92 $a->strings = $a->stringsave;
101 * load string translation table for alternate language
103 * first addon strings are loaded, then globals
105 * @param string $lang language code to load
107 public static function loadTranslationTable($lang)
112 // load enabled addons strings
113 $addons = dba::select('addon', ['name'], ['installed' => true]);
114 while ($p = dba::fetch($addons)) {
116 if (file_exists("addon/$name/lang/$lang/strings.php")) {
117 include("addon/$name/lang/$lang/strings.php");
121 if (file_exists("view/lang/$lang/strings.php")) {
122 include("view/lang/$lang/strings.php");
127 * @brief Return the localized version of the provided string with optional string interpolation
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.
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)
141 public static function t($s)
145 if (x($a->strings, $s)) {
146 $t = $a->strings[$s];
147 $s = is_array($t) ? $t[0] : $t;
149 if (func_num_args() > 1) {
150 $args = array_slice(func_get_args(), 1);
151 $s = @vsprintf($s, $args);
158 * @brief Return the localized version of a singular/plural string with optional string interpolation
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.
167 * - tt('Like', 'Likes', $count)
168 * - tt("%s user deleted", "%s users deleted", count($users))
171 * @param string $singular
172 * @param string $plural
176 function tt($singular, $plural, $count)
181 if (x($a->strings, $singular)) {
182 $t = $a->strings[$singular];
184 $plural_function = 'string_plural_select_' . str_replace('-', '_', $lang);
185 if (function_exists($plural_function)) {
186 $plural_function = 'string_plural_select_default';
188 $i = $plural_function($count);
193 } elseif (string_plural_select_default($count)) {
199 $s = @sprintf($s, $count);
204 // provide a fallback which will not collide with
205 // a function defined in any language file
206 function string_plural_select_default($n)
214 * @brief Return installed languages codes as associative array
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.
220 * Ex: array('de' => 'de', 'en' => 'en', 'fr' => 'fr', ...)
224 function get_available_languages() {
226 $strings_file_paths = glob('view/lang/*/strings.php');
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';
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];