]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/L10n/L10n.php
Make L10n immutable
[friendica.git] / src / Core / L10n / L10n.php
index ce930b402011641112de09c428d9bd78a86479fc..be9e1419f39ab60cf2dbac73a41ef186ab2926f9 100644 (file)
@@ -4,7 +4,7 @@ namespace Friendica\Core\L10n;
 
 use Friendica\Core\Config\Configuration;
 use Friendica\Core\Hook;
-use Friendica\Core\Session;
+use Friendica\Core\Session\ISession;
 use Friendica\Database\Database;
 use Friendica\Util\Strings;
 use Psr\Log\LoggerInterface;
@@ -23,12 +23,6 @@ class L10n
         * @var string
         */
        private $lang = '';
-       /**
-        * A language code saved for later after pushLang() has been called.
-        *
-        * @var string
-        */
-       private $langSave = '';
 
        /**
         * An array of translation strings whose key is the neutral english message.
@@ -36,12 +30,6 @@ class L10n
         * @var array
         */
        private $strings = [];
-       /**
-        * An array of translation strings saved for later after pushLang() has been called.
-        *
-        * @var array
-        */
-       private $stringsSave = [];
 
        /**
         * @var Database
@@ -53,12 +41,14 @@ class L10n
         */
        private $logger;
 
-       public function __construct(Configuration $config, Database $dba, LoggerInterface $logger, array $server, array $get)
+       public function __construct(Configuration $config, Database $dba, LoggerInterface $logger, ISession $session, array $server, array $get)
        {
                $this->dba    = $dba;
                $this->logger = $logger;
 
                $this->loadTranslationTable(L10n::detectLanguage($server, $get, $config->get('system', 'language', 'en')));
+               $this->setSessionVariable($session);
+               $this->setLangFromSession($session);
        }
 
        /**
@@ -74,73 +64,29 @@ class L10n
        /**
         * Sets the language session variable
         */
-       public function setSessionVariable()
+       private function setSessionVariable(ISession $session)
        {
-               if (Session::get('authenticated') && !Session::get('language')) {
-                       $_SESSION['language'] = $this->lang;
+               if ($session->get('authenticated') && !$session->get('language')) {
+                       $session->set('language', $this->lang);
                        // we haven't loaded user data yet, but we need user language
-                       if (Session::get('uid')) {
+                       if ($session->get('uid')) {
                                $user = $this->dba->selectFirst('user', ['language'], ['uid' => $_SESSION['uid']]);
                                if ($this->dba->isResult($user)) {
-                                       $_SESSION['language'] = $user['language'];
+                                       $session->set('language', $user['language']);
                                }
                        }
                }
 
                if (isset($_GET['lang'])) {
-                       Session::set('language', $_GET['lang']);
-               }
-       }
-
-       public function setLangFromSession()
-       {
-               if (Session::get('language') !== $this->lang) {
-                       $this->loadTranslationTable(Session::get('language'));
-               }
-       }
-
-       /**
-        * This function should be called before formatting messages in a specific target language
-        * different from the current user/system language.
-        *
-        * It saves the current translation strings in a separate variable and loads new translations strings.
-        *
-        * If called repeatedly, it won't save the translation strings again, just load the new ones.
-        *
-        * @param string $lang Language code
-        *
-        * @throws \Exception
-        * @see   popLang()
-        * @brief Stores the current language strings and load a different language.
-        */
-       public function pushLang($lang)
-       {
-               if ($lang === $this->lang) {
-                       return;
-               }
-
-               if (empty($this->langSave)) {
-                       $this->langSave    = $this->lang;
-                       $this->stringsSave = $this->strings;
+                       $session->set('language', $_GET['lang']);
                }
-
-               $this->loadTranslationTable($lang);
        }
 
-       /**
-        * Restores the original user/system language after having used pushLang()
-        */
-       public function popLang()
+       private function setLangFromSession(ISession $session)
        {
-               if (!isset($this->langSave)) {
-                       return;
+               if ($session->get('language') !== $this->lang) {
+                       $this->loadTranslationTable($session->get('language'));
                }
-
-               $this->strings = $this->stringsSave;
-               $this->lang    = $this->langSave;
-
-               $this->stringsSave = null;
-               $this->langSave    = null;
        }
 
        /**
@@ -456,4 +402,19 @@ class L10n
 
                return $arr;
        }
+
+       /**
+        * Creates a new L10n instance based on the given langauge
+        *
+        * @param string $lang The new language
+        *
+        * @return static A new L10n instance
+        * @throws \Exception
+        */
+       public function withLang(string $lang)
+       {
+               $newL10n = clone $this;
+               $newL10n->loadTranslationTable($lang);
+               return $newL10n;
+       }
 }