]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/L10n.php
Merge pull request #11662 from MrPetovan/bug/11661-splitattachment-null
[friendica.git] / src / Core / L10n.php
index 33c670443674c2ecaa7f21a1e968aa5105146809..2c45fb95519f47473e7ce867b0881e4758c69423 100644 (file)
@@ -25,7 +25,6 @@ use Friendica\Core\Config\Capability\IManageConfigValues;
 use Friendica\Core\Session\Capability\IHandleSessions;
 use Friendica\Database\Database;
 use Friendica\Util\Strings;
-use Psr\Log\LoggerInterface;
 
 /**
  * Provide Language, Translation, and Localization functions to the application
@@ -35,6 +34,35 @@ class L10n
 {
        /** @var string The default language */
        const DEFAULT = 'en';
+       /** @var string[] The language names in their language */
+       const LANG_NAMES = [
+               'ar'    => 'العربية',
+               'bg'    => 'Български',
+               'ca'    => 'Català',
+               'cs'    => 'Česky',
+               'da-dk' => 'Dansk (Danmark)',
+               'de'    => 'Deutsch',
+               'en-gb' => 'English (United Kingdom)',
+               'en-us' => 'English (United States)',
+               'en'    => 'English (Default)',
+               'eo'    => 'Esperanto',
+               'es'    => 'Español',
+               'et'    => 'Eesti',
+               'fi-fi' => 'Suomi',
+               'fr'    => 'Français',
+               'hu'    => 'Magyar',
+               'is'    => 'Íslenska',
+               'it'    => 'Italiano',
+               'ja'    => '日本語',
+               'nb-no' => 'Norsk bokmål',
+               'nl'    => 'Nederlands',
+               'pl'    => 'Polski',
+               'pt-br' => 'Português Brasileiro',
+               'ro'    => 'Română',
+               'ru'    => 'Русский',
+               'sv'    => 'Svenska',
+               'zh-cn' => '简体中文',
+       ];
 
        /**
         * A string indicating the current language used for translation:
@@ -57,15 +85,9 @@ class L10n
         */
        private $dba;
 
-       /**
-        * @var LoggerInterface
-        */
-       private $logger;
-
-       public function __construct(IManageConfigValues $config, Database $dba, LoggerInterface $logger, IHandleSessions $session, array $server, array $get)
+       public function __construct(IManageConfigValues $config, Database $dba, IHandleSessions $session, array $server, array $get)
        {
                $this->dba    = $dba;
-               $this->logger = $logger;
 
                $this->loadTranslationTable(L10n::detectLanguage($server, $get, $config->get('system', 'language', self::DEFAULT)));
                $this->setSessionVariable($session);
@@ -106,7 +128,7 @@ class L10n
        private function setLangFromSession(IHandleSessions $session)
        {
                if ($session->get('language') !== $this->lang) {
-                       $this->loadTranslationTable($session->get('language'));
+                       $this->loadTranslationTable($session->get('language') ?? $this->lang);
                }
        }
 
@@ -118,10 +140,10 @@ class L10n
         * Uses an App object shim since all the strings files refer to $a->strings
         *
         * @param string $lang language code to load
-        *
+        * @return void
         * @throws \Exception
         */
-       private function loadTranslationTable($lang)
+       private function loadTranslationTable(string $lang)
        {
                $lang = Strings::sanitizeFilePathItem($lang);
 
@@ -161,7 +183,7 @@ class L10n
         *
         * @return string The two-letter language code
         */
-       public static function detectLanguage(array $server, array $get, string $sysLang = self::DEFAULT)
+       public static function detectLanguage(array $server, array $get, string $sysLang = self::DEFAULT): string
        {
                $lang_variable = $server['HTTP_ACCEPT_LANGUAGE'] ?? null;
 
@@ -247,7 +269,7 @@ class L10n
         *
         * @return string
         */
-       public function t($s, ...$vars)
+       public function t(string $s, ...$vars): string
        {
                if (empty($s)) {
                        return '';
@@ -285,7 +307,7 @@ class L10n
         * @return string
         * @throws \Exception
         */
-       public function tt(string $singular, string $plural, int $count)
+       public function tt(string $singular, string $plural, int $count): string
        {
                $s = null;
 
@@ -330,7 +352,7 @@ class L10n
         *
         * @return bool
         */
-       private function stringPluralSelectDefault($n)
+       private function stringPluralSelectDefault(int $n): bool
        {
                return $n != 1;
        }
@@ -347,37 +369,10 @@ class L10n
         *
         * @return array
         */
-       public static function getAvailableLanguages()
+       public static function getAvailableLanguages(): array
        {
                $langs              = [];
                $strings_file_paths = glob('view/lang/*/strings.php');
-               $lang_names = [
-                       'ar'    => 'العربية',
-                       'bg'    => 'Български',
-                       'ca'    => 'Català',
-                       'cs'    => 'Česky',
-                       'de'    => 'Deutsch',
-                       'en-gb' => 'English (United Kingdom)',
-                       'en-us' => 'English (United States)',
-                       'en'    => 'English (Default)',
-                       'eo'    => 'Esperanto',
-                       'es'    => 'Español',
-                       'et'    => 'Eesti',
-                       'fi-fi' => 'Suomi',
-                       'fr'    => 'Français',
-                       'hu'    => 'Magyar',
-                       'is'    => 'Íslenska',
-                       'it'    => 'Italiano',
-                       'ja'    => '日本語',
-                       'nb-no' => 'Norsk bokmål',
-                       'nl'    => 'Nederlands',
-                       'pl'    => 'Polski',
-                       'pt-br' => 'Português Brasileiro',
-                       'ro'    => 'Română',
-                       'ru'    => 'Русский',
-                       'sv'    => 'Svenska',
-                       'zh-cn' => '简体中文',
-               ];
 
                if (is_array($strings_file_paths) && count($strings_file_paths)) {
                        if (!in_array('view/lang/en/strings.php', $strings_file_paths)) {
@@ -386,7 +381,7 @@ class L10n
                        asort($strings_file_paths);
                        foreach ($strings_file_paths as $strings_file_path) {
                                $path_array            = explode('/', $strings_file_path);
-                               $langs[$path_array[2]] = isset($lang_names[$path_array[2]]) ? $lang_names[$path_array[2]] : $path_array[2];
+                               $langs[$path_array[2]] = self::LANG_NAMES[$path_array[2]] ?? $path_array[2];
                        }
                }
                return $langs;
@@ -396,10 +391,9 @@ class L10n
         * Translate days and months names.
         *
         * @param string $s String with day or month name.
-        *
         * @return string Translated string.
         */
-       public function getDay($s)
+       public function getDay(string $s): string
        {
                $ret = str_replace(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
                        [$this->t('Monday'), $this->t('Tuesday'), $this->t('Wednesday'), $this->t('Thursday'), $this->t('Friday'), $this->t('Saturday'), $this->t('Sunday')],
@@ -416,10 +410,9 @@ class L10n
         * Translate short days and months names.
         *
         * @param string $s String with short day or month name.
-        *
         * @return string Translated string.
         */
-       public function getDayShort($s)
+       public function getDayShort(string $s): string
        {
                $ret = str_replace(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
                        [$this->t('Mon'), $this->t('Tue'), $this->t('Wed'), $this->t('Thu'), $this->t('Fri'), $this->t('Sat'), $this->t('Sun')],
@@ -440,7 +433,7 @@ class L10n
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @hook poke_verbs pokes array
         */
-       public function getPokeVerbs()
+       public function getPokeVerbs(): array
        {
                // index is present tense verb
                // value is array containing past tense verb, translation of present, translation of past
@@ -466,7 +459,7 @@ class L10n
         * @return static A new L10n instance
         * @throws \Exception
         */
-       public function withLang(string $lang)
+       public function withLang(string $lang): L10n
        {
                // Don't create a new instance for same language
                if ($lang === $this->lang) {