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() {
22 if (x($_SERVER, 'HTTP_ACCEPT_LANGUAGE')) {
23 // break up string into pieces (languages and q factors)
24 preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
25 $_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 for ($i = 0; $i < count($lang_list); $i++) {
45 $lang = $lang_list[$i];
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 function push_lang($language) {
65 if ($language === $lang) {
69 if (isset($a->strings) && count($a->strings)) {
70 $a->stringsave = $a->strings;
72 $a->strings = array();
73 load_translation_table($language);
80 if ($lang === $a->langsave) {
84 if (isset($a->stringsave)) {
85 $a->strings = $a->stringsave;
87 $a->strings = array();
96 * load string translation table for alternate language
98 * first plugin strings are loaded, then globals
100 * @param string $lang language code to load
102 function load_translation_table($lang) {
105 $a->strings = array();
106 // load enabled plugins strings
107 $plugins = dba::select('addon', array('name'), array('installed' => true));
108 while ($p = dba::fetch($plugins)) {
110 if (file_exists("addon/$name/lang/$lang/strings.php")) {
111 include("addon/$name/lang/$lang/strings.php");
115 if (file_exists("view/lang/$lang/strings.php")) {
116 include("view/lang/$lang/strings.php");
122 * @brief Return the localized version of the provided string with optional string interpolation
124 * This function takes a english string as parameter, and if a localized version
125 * exists for the current language, substitutes it before performing an eventual
126 * string interpolation (sprintf) with additional optional arguments.
129 * - t('This is an example')
130 * - t('URL %s returned no result', $url)
131 * - t('Current version: %s, new version: %s', $current_version, $new_version)
140 if (x($a->strings, $s)) {
141 $t = $a->strings[$s];
142 $s = is_array($t) ? $t[0] : $t;
144 if (func_num_args() > 1) {
145 $args = array_slice(func_get_args(), 1);
146 $s = @vsprintf($s, $args);
153 * @brief Return the localized version of a singular/plural string with optional string interpolation
155 * This function takes two english strings as parameters, singular and plural, as
156 * well as a count. If a localized version exists for the current language, they
157 * are used instead. Discrimination between singular and plural is done using the
158 * localized function if any or the default one. Finally, a string interpolation
159 * is performed using the count as parameter.
162 * - tt('Like', 'Likes', $count)
163 * - tt("%s user deleted", "%s users deleted", count($users))
166 * @param string $singular
167 * @param string $plural
171 function tt($singular, $plural, $count)
176 if (x($a->strings, $singular)) {
177 $t = $a->strings[$singular];
179 $plural_function = 'string_plural_select_' . str_replace('-', '_', $lang);
180 if (function_exists($plural_function)) {
181 $plural_function = 'string_plural_select_default';
183 $i = $plural_function($count);
188 } elseif (string_plural_select_default($count)) {
194 $s = @sprintf($s, $count);
199 // provide a fallback which will not collide with
200 // a function defined in any language file
201 function string_plural_select_default($n)
209 * @brief Return installed languages codes as associative array
211 * Scans the view/lang directory for the existence of "strings.php" files, and
212 * returns an alphabetical list of their folder names (@-char language codes).
213 * Adds the english language if it's missing from the list.
215 * Ex: array('de' => 'de', 'en' => 'en', 'fr' => 'fr', ...)
219 function get_available_languages() {
221 $strings_file_paths = glob('view/lang/*/strings.php');
223 if (is_array($strings_file_paths) && count($strings_file_paths)) {
224 if (!in_array('view/lang/en/strings.php', $strings_file_paths)) {
225 $strings_file_paths[] = 'view/lang/en/strings.php';
227 asort($strings_file_paths);
228 foreach ($strings_file_paths as $strings_file_path) {
229 $path_array = explode('/', $strings_file_path);
230 $langs[$path_array[2]] = $path_array[2];