4 * @brief translation support
6 * Get the language setting directly from system variables, bypassing Config::get()
7 * as database may not yet be configured.
9 * If possible, we use the value from the browser.
13 use Friendica\Core\Config;
15 require_once "include/dba.php";
18 * @brief get the prefered language from the HTTP_ACCEPT_LANGUAGE header
20 function get_browser_language() {
23 if (x($_SERVER, 'HTTP_ACCEPT_LANGUAGE')) {
24 // break up string into pieces (languages and q factors)
25 preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
26 $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
28 if (count($lang_parse[1])) {
29 // go through the list of prefered languages and add a generic language
30 // for sub-linguas (e.g. de-ch will add de) if not already in array
31 for ($i = 0; $i < count($lang_parse[1]); $i++) {
32 $lang_list[] = strtolower($lang_parse[1][$i]);
33 if (strlen($lang_parse[1][$i])>3 ) {
34 $dashpos = strpos($lang_parse[1][$i], '-');
35 if (!in_array(substr($lang_parse[1][$i], 0, $dashpos), $lang_list ) ) {
36 $lang_list[] = strtolower(substr($lang_parse[1][$i], 0, $dashpos));
43 // check if we have translations for the preferred languages and pick the 1st that has
44 foreach ($lang_list as $lang) {
45 if ($lang === 'en' || (file_exists("view/lang/$lang") && is_dir("view/lang/$lang"))) {
50 if (isset($preferred)) {
54 // in case none matches, get the system wide configured language, or fall back to English
55 return Config::get('system', 'language', 'en');
59 function push_lang($language) {
64 if ($language === $lang) {
68 if (isset($a->strings) && count($a->strings)) {
69 $a->stringsave = $a->strings;
72 load_translation_table($language);
79 if ($lang === $a->langsave) {
83 if (isset($a->stringsave)) {
84 $a->strings = $a->stringsave;
95 * load string translation table for alternate language
97 * first addon strings are loaded, then globals
99 * @param string $lang language code to load
101 function load_translation_table($lang) {
105 // load enabled addons strings
106 $addons = dba::select('addon', ['name'], ['installed' => true]);
107 while ($p = dba::fetch($addons)) {
109 if (file_exists("addon/$name/lang/$lang/strings.php")) {
110 include("addon/$name/lang/$lang/strings.php");
114 if (file_exists("view/lang/$lang/strings.php")) {
115 include("view/lang/$lang/strings.php");
121 * @brief Return the localized version of the provided string with optional string interpolation
123 * This function takes a english string as parameter, and if a localized version
124 * exists for the current language, substitutes it before performing an eventual
125 * string interpolation (sprintf) with additional optional arguments.
128 * - t('This is an example')
129 * - t('URL %s returned no result', $url)
130 * - t('Current version: %s, new version: %s', $current_version, $new_version)
139 if (x($a->strings, $s)) {
140 $t = $a->strings[$s];
141 $s = is_array($t) ? $t[0] : $t;
143 if (func_num_args() > 1) {
144 $args = array_slice(func_get_args(), 1);
145 $s = @vsprintf($s, $args);
152 * @brief Return the localized version of a singular/plural string with optional string interpolation
154 * This function takes two english strings as parameters, singular and plural, as
155 * well as a count. If a localized version exists for the current language, they
156 * are used instead. Discrimination between singular and plural is done using the
157 * localized function if any or the default one. Finally, a string interpolation
158 * is performed using the count as parameter.
161 * - tt('Like', 'Likes', $count)
162 * - tt("%s user deleted", "%s users deleted", count($users))
165 * @param string $singular
166 * @param string $plural
170 function tt($singular, $plural, $count)
175 if (x($a->strings, $singular)) {
176 $t = $a->strings[$singular];
178 $plural_function = 'string_plural_select_' . str_replace('-', '_', $lang);
179 if (function_exists($plural_function)) {
180 $plural_function = 'string_plural_select_default';
182 $i = $plural_function($count);
187 } elseif (string_plural_select_default($count)) {
193 $s = @sprintf($s, $count);
198 // provide a fallback which will not collide with
199 // a function defined in any language file
200 function string_plural_select_default($n)
208 * @brief Return installed languages codes as associative array
210 * Scans the view/lang directory for the existence of "strings.php" files, and
211 * returns an alphabetical list of their folder names (@-char language codes).
212 * Adds the english language if it's missing from the list.
214 * Ex: array('de' => 'de', 'en' => 'en', 'fr' => 'fr', ...)
218 function get_available_languages() {
220 $strings_file_paths = glob('view/lang/*/strings.php');
222 if (is_array($strings_file_paths) && count($strings_file_paths)) {
223 if (!in_array('view/lang/en/strings.php', $strings_file_paths)) {
224 $strings_file_paths[] = 'view/lang/en/strings.php';
226 asort($strings_file_paths);
227 foreach ($strings_file_paths as $strings_file_path) {
228 $path_array = explode('/', $strings_file_path);
229 $langs[$path_array[2]] = $path_array[2];