]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/L10n.php
Replace global $lang with system.language
[friendica.git] / src / Core / L10n.php
index 4fe15b504bbfaa950c29eb115a3a2ec57cf80524..2389817021b2a5bc26e9e511f94ec8a4df7f1ef5 100644 (file)
@@ -7,13 +7,14 @@ namespace Friendica\Core;
 use Friendica\Core\Config;
 use dba;
 
+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 \Friendica\BaseObject
 {
        /**
         * @brief get the prefered language from the HTTP_ACCEPT_LANGUAGE header
@@ -61,11 +62,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;
                }
 
@@ -74,7 +75,7 @@ class L10n
                }
                $a->strings = [];
                self::loadTranslationTable($language);
-               $lang = $language;
+               Config::set('system', 'language', $language);
        }
 
        /**
@@ -82,9 +83,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;
                }
 
@@ -94,7 +95,7 @@ class L10n
                        $a->strings = [];
                }
 
-               $lang = $a->langsave;
+               Config::set('system', 'language', $a->langsave);
        }
 
        /**
@@ -106,7 +107,7 @@ class L10n
         */
        public static function loadTranslationTable($lang)
        {
-               $a = get_app();
+               $a = self::getApp();
 
                $a->strings = [];
                // load enabled addons strings
@@ -114,12 +115,12 @@ class L10n
                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";
                }
        }
 
@@ -131,24 +132,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;
@@ -164,33 +170,32 @@ 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();
+               $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 = 'string_plural_select_default';
+                                       $i = $plural_function($count);
+                               } else {
+                                       $i = self::stringPluralSelectDefault($count);
                                }
-                               $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;
@@ -201,9 +206,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;
        }
@@ -221,7 +227,8 @@ class L10n
         *
         * @return array
         */
-       function get_available_languages() {
+       public static function getAvailableLanguages()
+       {
                $langs = [];
                $strings_file_paths = glob('view/lang/*/strings.php');