From fc463bc0952695ce7bdcd5d937cf3182e27df9d4 Mon Sep 17 00:00:00 2001 From: Vainonen <8686308+Vainonen@users.noreply.github.com> Date: Wed, 31 May 2023 10:31:27 +0300 Subject: [PATCH 01/10] Added Symfony translation component --- composer.json | 6 +++++- src/controller/Controller.php | 4 ++-- src/controller/WebController.php | 23 ++++++++++++++++++++++- src/index.php | 1 + src/view/base-template.twig | 10 +++++----- 5 files changed, 35 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 5923ba803..a92884363 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,11 @@ "ext-intl": "*", "ext-mbstring": "*", "ext-xsl": "*", - "monolog/monolog": "1.27.*" + "monolog/monolog": "1.27.*", + "symfony/translation": "6.0.*", + "symfony/translation-contracts": "3.0.*", + "symfony/twig-bundle": "6.0.*", + "symfony/config": "6.0.*" }, "require-dev": { "phpunit/phpunit": "9.5.*", diff --git a/src/controller/Controller.php b/src/controller/Controller.php index 07e52063d..d9fed7ad7 100644 --- a/src/controller/Controller.php +++ b/src/controller/Controller.php @@ -42,8 +42,8 @@ public function __construct($model) foreach ($this->model->getConfig()->getLanguages() as $langcode => $locale) { $this->languages[$langcode] = array('locale' => $locale); $this->setLanguageProperties($langcode); - $this->languages[$langcode]['name'] = gettext('in_this_language'); - $this->languages[$langcode]['lemma'] = Punic\Language::getName($langcode, $langcode); + //$this->languages[$langcode]['name'] = gettext('in_this_language'); + //$this->languages[$langcode]['lemma'] = Punic\Language::getName($langcode, $langcode); } } diff --git a/src/controller/WebController.php b/src/controller/WebController.php index 2fdc8cf78..76fc1c7a0 100644 --- a/src/controller/WebController.php +++ b/src/controller/WebController.php @@ -4,7 +4,9 @@ * Importing the dependencies. */ use Punic\Language; - +use Symfony\Component\Translation\Translator; +use Symfony\Component\Translation\Loader\PoFileLoader; +use Symfony\Bridge\Twig\Extension\TranslationExtension; /** * WebController is an extension of the Controller that handles all * the requests originating from the view of the website. @@ -64,6 +66,13 @@ public function __construct($model) }); $this->twig->addFilter($langFilter); + foreach( $this->languages as $langcode => $value ) { + $translator = new Translator($langcode); + $translator->addLoader('po', new PoFileLoader()); + $translator->addResource('po', __DIR__.'/../../resource/translations/skosmos_' . $langcode . '.po', $langcode); + $this->languages[$langcode]['name'] = $translator->trans('in_this_language'); + } + // create the honeypot $this->honeypot = new \Honeypot(); if (!$this->model->getConfig()->getHoneypotEnabled()) { @@ -72,6 +81,18 @@ public function __construct($model) $this->twig->addGlobal('honeypot', $this->honeypot); } + /** + * Creates and registers the translation extension and loads translation file. + * @param string $lang two character language code + */ + public function addTranslations($lang) + { + $translator = new Translator($lang); + $translator->addLoader('po', new PoFileLoader()); + $translator->addResource('po', __DIR__.'/../../resource/translations/skosmos_' . $lang . '.po', $lang); + $this->twig->addExtension(new TranslationExtension($translator)); + } + /** * Guess the language of the user. Return a language string that is one * of the supported languages defined in the $LANGUAGES setting, e.g. "fi". diff --git a/src/index.php b/src/index.php index d00b70e64..b11d8b2b2 100644 --- a/src/index.php +++ b/src/index.php @@ -31,6 +31,7 @@ } else { if (array_key_exists($parts[1], $config->getLanguages())) { // global pages $request->setLang($parts[1]); + $controller->addTranslations($parts[1]); $content_lang = $request->getQueryParam('clang'); $request->setContentLang($content_lang); ($parts[2] == 'about' || $parts[2] == 'feedback' || $parts[2] == 'search') ? $request->setPage($parts[2]) : $request->setPage(''); diff --git a/src/view/base-template.twig b/src/view/base-template.twig index a1bd29bef..0857af599 100644 --- a/src/view/base-template.twig +++ b/src/view/base-template.twig @@ -29,24 +29,24 @@