X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCore%2FL10n.php;h=542590646d4b21e11a77beda055a62a0f666fe25;hb=761bdafa34bfdf1b2b43a3f06ae092e0925898ac;hp=88697a2a2c2391283808875d9a42328ea55fc786;hpb=3b0f69599e0a7c704ff0410eefc376c156ccf04d;p=friendica.git diff --git a/src/Core/L10n.php b/src/Core/L10n.php index 88697a2a2c..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,20 +108,20 @@ 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"); + 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"; } } @@ -132,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 */ - 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; @@ -165,10 +171,9 @@ 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 @@ -176,18 +181,30 @@ 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)); + } + + $lang = Config::get('system', 'language'); - if (x($a->strings, $singular)) { + 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 = 'self::stringPluralSelectDefault'; + $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; }