X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2FLanguages.php;h=5cda802e0c1faaf57058e0dca653cc713c90c0c3;hb=dea72c711ff740b3b829d238fcf85648465143a0;hp=4ba32f29384d55490af8db47d5416de615216f80;hpb=12266213d098a53c5f005b9afcbbe62771fd580c;p=github%2Fshaarli%2FShaarli.git diff --git a/application/Languages.php b/application/Languages.php index 4ba32f29..5cda802e 100644 --- a/application/Languages.php +++ b/application/Languages.php @@ -3,7 +3,6 @@ namespace Shaarli; use Gettext\GettextTranslator; -use Gettext\Merge; use Gettext\Translations; use Gettext\Translator; use Gettext\TranslatorInterface; @@ -62,13 +61,15 @@ class Languages /** * Languages constructor. * - * @param string $language lang determined by autoLocale(), can be override. + * @param string $language lang determined by autoLocale(), can be overridden. * @param ConfigManager $conf instance. */ public function __construct($language, $conf) { $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 { @@ -90,12 +91,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 +121,25 @@ 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) {} + } 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 +148,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) { + } } } @@ -147,4 +169,19 @@ class Languages { return preg_match('/^[a-z]{2}(_[A-Z]{2})?/', $language) === 1; } + + /** + * Get the list of available languages for Shaarli. + * + * @return array List of available languages, with their label. + */ + public static function getAvailableLanguages() + { + return [ + 'auto' => t('Automatic'), + 'en' => t('English'), + 'fr' => t('French'), + 'de' => t('German'), + ]; + } }