From 68c6afc56f3758154cfb96cba6fd48a6b5535590 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Mon, 26 Feb 2018 22:53:00 +0100 Subject: [PATCH] Load theme translations files automatically Fixes #1077 Take a look at the docs update to see how it works --- application/Languages.php | 20 ++++++++++++-- doc/md/Translations.md | 12 ++++++++ tests/LanguagesTest.php | 26 ++++++++++++++++++ tests/languages/fr/LanguagesFrTest.php | 26 ++++++++++++++++++ .../dummy/language/fr/LC_MESSAGES/dummy.mo | Bin 0 -> 431 bytes .../dummy/language/fr/LC_MESSAGES/dummy.po | 16 +++++++++++ 6 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 tests/utils/customtpl/dummy/language/fr/LC_MESSAGES/dummy.mo create mode 100644 tests/utils/customtpl/dummy/language/fr/LC_MESSAGES/dummy.po diff --git a/application/Languages.php b/application/Languages.php index 3eb3388f4..db4b84aea 100644 --- a/application/Languages.php +++ b/application/Languages.php @@ -98,6 +98,12 @@ protected function initGettextTranslator () $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 @@ protected function initPhpTranslator() $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 @@ protected function initPhpTranslator() } try { - /** @var Translations $extension */ $extension = Translations::fromPoFile($translationPath . $this->language .'/LC_MESSAGES/'. $domain .'.po'); $extension->setDomain($domain); $this->translator->loadTranslations($extension); diff --git a/doc/md/Translations.md b/doc/md/Translations.md index 54a366552..c7d338553 100644 --- a/doc/md/Translations.md +++ b/doc/md/Translations.md @@ -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//language//LC_MESSAGES/.po + tpl//language//LC_MESSAGES/.mo + +Where `` 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, diff --git a/tests/LanguagesTest.php b/tests/LanguagesTest.php index 864ce6306..4951e09a3 100644 --- a/tests/LanguagesTest.php +++ b/tests/LanguagesTest.php @@ -175,6 +175,32 @@ public function testTranslateWithInvalidAutoLanguagePhp() $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 */ diff --git a/tests/languages/fr/LanguagesFrTest.php b/tests/languages/fr/LanguagesFrTest.php index 79d051729..0cf74891b 100644 --- a/tests/languages/fr/LanguagesFrTest.php +++ b/tests/languages/fr/LanguagesFrTest.php @@ -172,4 +172,30 @@ public function testTranslationExtensionPhp() $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 index 0000000000000000000000000000000000000000..8daae0c9f2fcada91c744cf5fed5712a102eb73f GIT binary patch literal 431 zcmY+9&rSj{5XRSkG1;SM4|CM$V0RIOB3=*-3B(mwqbF0B0oE)nw#Dd!_!PdA&*H2k z62J79PA6ZdU%&UZKLge#v(FqeJIpy#Dq(h+o05$nc;tPH`Qn<_Kb12AYmD<`gT$H_ z$~^YRcu&^lMoUO%G$){UPr7V?C+EZDI>nA9<++2G%9DgxL}w^!7z>E1vUYkTVjVKMHS;ZL`^B0E8jplkEvDK70MecAsi?@U8*TzN@_0QcBYgq^1rr(iGAvg z!eM{dU5AY-BJ3E=S$W#Wf@673ezGW3u3M1Jly&6WcWDnBYhU^5ge-P-X2!XmN@zUi z{KsNxRe?QY=T3rN{2pC9Zo#T-A85FM=&%(&;1y}ItKf;Daqc0mh)O*SGV>bz05WrP A_y7O^ literal 0 HcmV?d00001 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 index 000000000..90d1abb0a --- /dev/null +++ b/tests/utils/customtpl/dummy/language/fr/LC_MESSAGES/dummy.po @@ -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"