]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - application/Languages.php
Merge pull request #1698 from ArthurHoaro/feature/plugins-search-filter
[github/shaarli/Shaarli.git] / application / Languages.php
index 357c7524ed458a128148522b124dd17876faac87..7177db2ccda5fcd6c190d70e3383d091b6bda48a 100644 (file)
@@ -3,7 +3,6 @@
 namespace Shaarli;
 
 use Gettext\GettextTranslator;
-use Gettext\Merge;
 use Gettext\Translations;
 use Gettext\Translator;
 use Gettext\TranslatorInterface;
@@ -42,7 +41,7 @@ class Languages
     /**
      * Core translations domain
      */
-    const DEFAULT_DOMAIN = 'shaarli';
+    public const DEFAULT_DOMAIN = 'shaarli';
 
     /**
      * @var TranslatorInterface
@@ -69,13 +68,16 @@ class Languages
     {
         $this->conf = $conf;
         $confLanguage = $this->conf->get('translation.language', 'auto');
+        // Auto mode or invalid parameter, use the detected language.
+        // If the detected language is invalid, it doesn't matter, it will use English.
         if ($confLanguage === 'auto' || ! $this->isValidLanguage($confLanguage)) {
             $this->language = substr($language, 0, 5);
         } else {
             $this->language = $confLanguage;
         }
 
-        if (! extension_loaded('gettext')
+        if (
+            ! extension_loaded('gettext')
             || in_array($this->conf->get('translation.mode', 'auto'), ['auto', 'php'])
         ) {
             $this->initPhpTranslator();
@@ -90,12 +92,18 @@ class Languages
     /**
      * Initialize the translator using php gettext extension (gettext dependency act as a wrapper).
      */
-    protected function initGettextTranslator ()
+    protected function initGettextTranslator()
     {
         $this->translator = new GettextTranslator();
         $this->translator->setLanguage($this->language);
         $this->translator->loadDomain(self::DEFAULT_DOMAIN, 'inc/languages');
 
+        // Default extension translation from the current theme
+        $themeTransFolder = rtrim($this->conf->get('raintpl_tpl'), '/') . '/' . $this->conf->get('theme') . '/language';
+        if (is_dir($themeTransFolder)) {
+            $this->translator->loadDomain($this->conf->get('theme'), $themeTransFolder, false);
+        }
+
         foreach ($this->conf->get('translation.extensions', []) as $domain => $translationPath) {
             if ($domain !== self::DEFAULT_DOMAIN) {
                 $this->translator->loadDomain($domain, $translationPath, false);
@@ -114,12 +122,27 @@ class Languages
         $translations = new Translations();
         // Core translations
         try {
-            /** @var Translations $translations */
-            $translations = $translations->addFromPoFile('inc/languages/'. $this->language .'/LC_MESSAGES/shaarli.po');
+            $translations = $translations->addFromPoFile(
+                'inc/languages/' . $this->language . '/LC_MESSAGES/shaarli.po'
+            );
             $translations->setDomain('shaarli');
             $this->translator->loadTranslations($translations);
-        } catch (\InvalidArgumentException $e) {}
+        } catch (\InvalidArgumentException $e) {
+        }
 
+        // Default extension translation from the current theme
+        $theme = $this->conf->get('theme');
+        $themeTransFolder = rtrim($this->conf->get('raintpl_tpl'), '/') . '/' . $theme . '/language';
+        if (is_dir($themeTransFolder)) {
+            try {
+                $translations = Translations::fromPoFile(
+                    $themeTransFolder . '/' . $this->language . '/LC_MESSAGES/' . $theme . '.po'
+                );
+                $translations->setDomain($theme);
+                $this->translator->loadTranslations($translations);
+            } catch (\InvalidArgumentException $e) {
+            }
+        }
 
         // Extension translations (plugins, themes, etc.).
         foreach ($this->conf->get('translation.extensions', []) as $domain => $translationPath) {
@@ -128,11 +151,13 @@ class Languages
             }
 
             try {
-                /** @var Translations $extension */
-                $extension = Translations::fromPoFile($translationPath . $this->language .'/LC_MESSAGES/'. $domain .'.po');
+                $extension = Translations::fromPoFile(
+                    $translationPath . $this->language . '/LC_MESSAGES/' . $domain . '.po'
+                );
                 $extension->setDomain($domain);
                 $this->translator->loadTranslations($extension);
-            } catch (\InvalidArgumentException $e) {}
+            } catch (\InvalidArgumentException $e) {
+            }
         }
     }
 
@@ -157,8 +182,11 @@ class Languages
     {
         return [
             'auto' => t('Automatic'),
+            'de' => t('German'),
             'en' => t('English'),
             'fr' => t('French'),
+            'jp' => t('Japanese'),
+            'ru' => t('Russian'),
         ];
     }
 }