]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Load theme translations files automatically 1093/head
authorArthurHoaro <arthur@hoa.ro>
Mon, 26 Feb 2018 21:53:00 +0000 (22:53 +0100)
committerArthurHoaro <arthur@hoa.ro>
Mon, 26 Mar 2018 17:20:25 +0000 (19:20 +0200)
Fixes #1077

Take a look at the docs update to see how it works

application/Languages.php
doc/md/Translations.md
tests/LanguagesTest.php
tests/languages/fr/LanguagesFrTest.php
tests/utils/customtpl/dummy/language/fr/LC_MESSAGES/dummy.mo [new file with mode: 0644]
tests/utils/customtpl/dummy/language/fr/LC_MESSAGES/dummy.po [new file with mode: 0644]

index 3eb3388f48f6a01ac87c872c408018c9c574cdec..db4b84aea23a0e8862e9b41afb1c3c5aec233328 100644 (file)
@@ -98,6 +98,12 @@ class Languages
         $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);
@@ -116,12 +122,23 @@ class Languages
         $translations = new Translations();
         // Core translations
         try {
-            /** @var Translations $translations */
             $translations = $translations->addFromPoFile('inc/languages/'. $this->language .'/LC_MESSAGES/shaarli.po');
             $translations->setDomain('shaarli');
             $this->translator->loadTranslations($translations);
         } 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) {
@@ -130,7 +147,6 @@ class Languages
             }
 
             try {
-                /** @var Translations $extension */
                 $extension = Translations::fromPoFile($translationPath . $this->language .'/LC_MESSAGES/'. $domain .'.po');
                 $extension->setDomain($domain);
                 $this->translator->loadTranslations($extension);
index 54a3665529c0216a92acbb88e0f7abbbe7d5f593..c7d3385532451c031661f202e33f2f6aa2fdf61f 100644 (file)
@@ -76,6 +76,18 @@ Then click on the "Update" button, and you can start to translate every availabl
 
 Save when you're done, then you can submit a pull request containing the new `shaarli.po`.
 
+### Theme translations 
+
+Theme translation extensions are loaded automatically if they're present.
+
+As a theme developer, all you have to do is to add the `.po` and `.mo` compiled file like this:
+
+    tpl/<theme name>/language/<lang>/LC_MESSAGES/<theme name>.po
+    tpl/<theme name>/language/<lang>/LC_MESSAGES/<theme name>.mo
+
+Where `<lang>` is the ISO 3166-1 alpha-2 language code. 
+Read the following section "Extend Shaarli's translation" to learn how to generate those files.
+
 ### Extend Shaarli's translation
 
 If you're writing a custom theme, or a non official plugin, you might want to use the translation system,
index 864ce63060de1b9fb0c98f4599d5e40fe5064170..4951e09a3e2f31261ca9b119d700fb817a3e860f 100644 (file)
@@ -175,6 +175,32 @@ class LanguagesTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($text, t($text));
     }
 
+    /**
+     * Test t() with an extension language file coming from the theme in gettext mode
+     */
+    public function testTranslationThemeExtensionGettext()
+    {
+        $this->conf->set('translation.mode', 'gettext');
+        $this->conf->set('raintpl_tpl', 'tests/utils/customtpl/');
+        $this->conf->set('theme', 'dummy');
+        new Languages('en', $this->conf);
+        $txt = 'rooster'; // ignore me poedit
+        $this->assertEquals('rooster', t($txt, $txt, 1, 'dummy'));
+    }
+
+    /**
+     * Test t() with an extension language file coming from the theme in PHP mode
+     */
+    public function testTranslationThemeExtensionPhp()
+    {
+        $this->conf->set('translation.mode', 'php');
+        $this->conf->set('raintpl_tpl', 'tests/utils/customtpl/');
+        $this->conf->set('theme', 'dummy');
+        new Languages('en', $this->conf);
+        $txt = 'rooster'; // ignore me poedit
+        $this->assertEquals('rooster', t($txt, $txt, 1, 'dummy'));
+    }
+
     /**
      * Test t() with an extension language file in gettext mode
      */
index 79d05172971896c67119513e739ce581f4a86737..0cf74891b7ab937403c2b6f6184975edeb5a2671 100644 (file)
@@ -172,4 +172,30 @@ class LanguagesFrTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('voiture', t($txt, $txt, 1, 'test'));
         $this->assertEquals('Fouille', t('Search', 'Search', 1, 'test'));
     }
+
+    /**
+     * Test t() with an extension language file coming from the theme in gettext mode
+     */
+    public function testTranslationThemeExtensionGettext()
+    {
+        $this->conf->set('translation.mode', 'gettext');
+        $this->conf->set('raintpl_tpl', 'tests/utils/customtpl/');
+        $this->conf->set('theme', 'dummy');
+        new Languages('en', $this->conf);
+        $txt = 'rooster'; // ignore me poedit
+        $this->assertEquals('coq', t($txt, $txt, 1, 'dummy'));
+    }
+
+    /**
+     * Test t() with an extension language file coming from the theme in PHP mode
+     */
+    public function testTranslationThemeExtensionPhp()
+    {
+        $this->conf->set('translation.mode', 'php');
+        $this->conf->set('raintpl_tpl', 'tests/utils/customtpl/');
+        $this->conf->set('theme', 'dummy');
+        new Languages('en', $this->conf);
+        $txt = 'rooster'; // ignore me poedit
+        $this->assertEquals('coq', t($txt, $txt, 1, 'dummy'));
+    }
 }
diff --git a/tests/utils/customtpl/dummy/language/fr/LC_MESSAGES/dummy.mo b/tests/utils/customtpl/dummy/language/fr/LC_MESSAGES/dummy.mo
new file mode 100644 (file)
index 0000000..8daae0c
Binary files /dev/null and b/tests/utils/customtpl/dummy/language/fr/LC_MESSAGES/dummy.mo differ
diff --git a/tests/utils/customtpl/dummy/language/fr/LC_MESSAGES/dummy.po b/tests/utils/customtpl/dummy/language/fr/LC_MESSAGES/dummy.po
new file mode 100644 (file)
index 0000000..90d1abb
--- /dev/null
@@ -0,0 +1,16 @@
+msgid ""
+msgstr ""
+"Project-Id-Version: Theme extension test\n"
+"POT-Creation-Date: 2017-05-20 13:54+0200\n"
+"PO-Revision-Date: 2018-03-26 19:09+0200\n"
+"Last-Translator: \n"
+"Language-Team: Shaarli\n"
+"Language: fr_FR\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2; plural=(n > 1);\n"
+"X-Generator: Poedit 2.0.6\n"
+
+msgid "rooster"
+msgstr "coq"