X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCore%2FL10n.php;h=542590646d4b21e11a77beda055a62a0f666fe25;hb=9fbaaa1481a609563e00a40db64bdce5e02c5524;hp=381cd930b29d225bd0ab6a7084a3f63a18c1e14b;hpb=417b32c881b8712175c60f8656f179f1d2f26e90;p=friendica.git diff --git a/src/Core/L10n.php b/src/Core/L10n.php index 381cd930b2..542590646d 100644 --- a/src/Core/L10n.php +++ b/src/Core/L10n.php @@ -4,36 +4,38 @@ */ namespace Friendica\Core; -use Friendica\Core\Config; -use dba; +use Friendica\BaseObject; +use Friendica\Database\DBA; +use Friendica\Core\System; +require_once 'boot.php'; require_once 'include/dba.php'; /** * Provide Languange, Translation, and Localisation functions to the application * Localisation can be referred to by the numeronym L10N (as in: "L", followed by ten more letters, and then "N"). */ -class L10n +class L10n extends BaseObject { /** * @brief get the prefered language from the HTTP_ACCEPT_LANGUAGE header */ - function get_browser_language() { - + public static function getBrowserLanguage() + { $lang_list = []; + if (x($_SERVER, 'HTTP_ACCEPT_LANGUAGE')) { // break up string into pieces (languages and q factors) - 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); + 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); if (count($lang_parse[1])) { // go through the list of prefered languages and add a generic language // for sub-linguas (e.g. de-ch will add de) if not already in array for ($i = 0; $i < count($lang_parse[1]); $i++) { $lang_list[] = strtolower($lang_parse[1][$i]); - if (strlen($lang_parse[1][$i])>3 ) { + if (strlen($lang_parse[1][$i])>3) { $dashpos = strpos($lang_parse[1][$i], '-'); - if (!in_array(substr($lang_parse[1][$i], 0, $dashpos), $lang_list ) ) { + if (!in_array(substr($lang_parse[1][$i], 0, $dashpos), $lang_list)) { $lang_list[] = strtolower(substr($lang_parse[1][$i], 0, $dashpos)); } } @@ -56,13 +58,16 @@ class L10n return Config::get('system', 'language', 'en'); } + /** + * @param string $language language + */ + public static function pushLang($language) + { + $a = self::getApp(); - function push_lang($language) { - global $lang, $a; - - $a->langsave = $lang; + $a->langsave = Config::get('system', 'language'); - if ($language === $lang) { + if ($language === $a->langsave) { return; } @@ -70,14 +75,18 @@ class L10n $a->stringsave = $a->strings; } $a->strings = []; - load_translation_table($language); - $lang = $language; + self::loadTranslationTable($language); + Config::set('system', 'language', $language); } - function pop_lang() { - global $lang, $a; + /** + * Pop language off the top of the stack + */ + public static function popLang() + { + $a = self::getApp(); - if ($lang === $a->langsave) { + if (Config::get('system', 'language') === $a->langsave) { return; } @@ -87,35 +96,33 @@ class L10n $a->strings = []; } - $lang = $a->langsave; + Config::set('system', 'language', $a->langsave); } - // l - /** * load string translation table for alternate language * - * first plugin strings are loaded, then globals + * first addon strings are loaded, then globals * * @param string $lang language code to load */ - function load_translation_table($lang) { - $a = get_app(); + public static function loadTranslationTable($lang) + { + $a = self::getApp(); $a->strings = []; - // load enabled plugins strings - $plugins = dba::select('addon', ['name'], ['installed' => true]); - while ($p = dba::fetch($plugins)) { + // load enabled addons strings + $addons = DBA::select('addon', ['name'], ['installed' => true]); + while ($p = DBA::fetch($addons)) { $name = $p['name']; if (file_exists("addon/$name/lang/$lang/strings.php")) { - include("addon/$name/lang/$lang/strings.php"); + include "addon/$name/lang/$lang/strings.php"; } } if (file_exists("view/lang/$lang/strings.php")) { - include("view/lang/$lang/strings.php"); + include "view/lang/$lang/strings.php"; } - } /** @@ -126,24 +133,29 @@ class L10n * string interpolation (sprintf) with additional optional arguments. * * Usages: - * - t('This is an example') - * - t('URL %s returned no result', $url) - * - t('Current version: %s, new version: %s', $current_version, $new_version) + * - L10n::t('This is an example') + * - L10n::t('URL %s returned no result', $url) + * - L10n::t('Current version: %s, new version: %s', $current_version, $new_version) * * @param string $s + * @param array $vars Variables to interpolate in the translation string * @return string */ - function t($s) + public static function t($s, ...$vars) { - $a = get_app(); + $a = self::getApp(); + + if (empty($s)) { + return ''; + } if (x($a->strings, $s)) { $t = $a->strings[$s]; $s = is_array($t) ? $t[0] : $t; } - if (func_num_args() > 1) { - $args = array_slice(func_get_args(), 1); - $s = @vsprintf($s, $args); + + if (count($vars) > 0) { + $s = sprintf($s, ...$vars); } return $s; @@ -159,33 +171,44 @@ class L10n * is performed using the count as parameter. * * Usages: - * - tt('Like', 'Likes', $count) - * - tt("%s user deleted", "%s users deleted", count($users)) + * - L10n::tt('Like', 'Likes', $count) + * - L10n::tt("%s user deleted", "%s users deleted", count($users)) * - * @global type $lang * @param string $singular * @param string $plural * @param int $count * @return string */ - function tt($singular, $plural, $count) + public static function tt($singular, $plural, $count) { - global $lang; - $a = get_app(); + $a = self::getApp(); + + if (!is_numeric($count)) { + logger('Non numeric count called by ' . System::callstack(20)); + } - if (x($a->strings, $singular)) { + $lang = Config::get('system', 'language'); + + if (!empty($a->strings[$singular])) { $t = $a->strings[$singular]; if (is_array($t)) { $plural_function = 'string_plural_select_' . str_replace('-', '_', $lang); if (function_exists($plural_function)) { - $plural_function = 'string_plural_select_default'; + $i = $plural_function($count); + } else { + $i = self::stringPluralSelectDefault($count); + } + + // for some languages there is only a single array item + if (!isset($t[$i])) { + $s = $t[0]; + } else { + $s = $t[$i]; } - $i = $plural_function($count); - $s = $t[$i]; } else { $s = $t; } - } elseif (string_plural_select_default($count)) { + } elseif (self::stringPluralSelectDefault($count)) { $s = $plural; } else { $s = $singular; @@ -196,9 +219,10 @@ class L10n return $s; } - // provide a fallback which will not collide with - // a function defined in any language file - function string_plural_select_default($n) + /** + * Provide a fallback which will not collide with a function defined in any language file + */ + private static function stringPluralSelectDefault($n) { return $n != 1; } @@ -216,7 +240,8 @@ class L10n * * @return array */ - function get_available_languages() { + public static function getAvailableLanguages() + { $langs = []; $strings_file_paths = glob('view/lang/*/strings.php');