X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCore%2FL10n.php;h=542590646d4b21e11a77beda055a62a0f666fe25;hb=be7bd106784ad4e45833b1649e8409ef48f0d19f;hp=215c6f6fb11c1c9a682a22c473d4618a145fecdd;hpb=30c1cc0e8cec5438fd8fe36bd4ea00991dc01934;p=friendica.git diff --git a/src/Core/L10n.php b/src/Core/L10n.php index 215c6f6fb1..542590646d 100644 --- a/src/Core/L10n.php +++ b/src/Core/L10n.php @@ -4,8 +4,9 @@ */ 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'; @@ -14,7 +15,7 @@ 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 @@ -62,11 +63,11 @@ class L10n */ public static function pushLang($language) { - global $lang, $a; + $a = self::getApp(); - $a->langsave = $lang; + $a->langsave = Config::get('system', 'language'); - if ($language === $lang) { + if ($language === $a->langsave) { return; } @@ -75,7 +76,7 @@ class L10n } $a->strings = []; self::loadTranslationTable($language); - $lang = $language; + Config::set('system', 'language', $language); } /** @@ -83,9 +84,9 @@ class L10n */ public static function popLang() { - global $lang, $a; + $a = self::getApp(); - if ($lang === $a->langsave) { + if (Config::get('system', 'language') === $a->langsave) { return; } @@ -95,7 +96,7 @@ class L10n $a->strings = []; } - $lang = $a->langsave; + Config::set('system', 'language', $a->langsave); } /** @@ -107,12 +108,12 @@ class L10n */ public static function loadTranslationTable($lang) { - $a = get_app(); + $a = self::getApp(); $a->strings = []; // load enabled addons strings - $addons = dba::select('addon', ['name'], ['installed' => true]); - while ($p = dba::fetch($addons)) { + $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"; @@ -137,19 +138,24 @@ class L10n * - 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 */ - public static 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; @@ -168,7 +174,6 @@ class L10n * - 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 @@ -176,10 +181,15 @@ class L10n */ 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); @@ -188,7 +198,13 @@ class L10n } else { $i = self::stringPluralSelectDefault($count); } - $s = $t[$i]; + + // for some languages there is only a single array item + if (!isset($t[$i])) { + $s = $t[0]; + } else { + $s = $t[$i]; + } } else { $s = $t; }