From cc9ce4167f64536bce26c21f57710d9c39df00db Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Thu, 18 Nov 2021 09:10:13 +0100 Subject: [PATCH] fix: Upgrade `TranslatableStringHelper` service and create its interface. --- .../Templating/TranslatableStringHelper.php | 110 ++++++------------ .../TranslatableStringHelperInterface.php | 17 +++ .../ChillMainBundle/config/services.yaml | 4 +- 3 files changed, 55 insertions(+), 76 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Templating/TranslatableStringHelperInterface.php diff --git a/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelper.php b/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelper.php index 673b87dbe..cbeb9514e 100644 --- a/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelper.php +++ b/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelper.php @@ -1,92 +1,56 @@ - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ + +declare(strict_types=1); namespace Chill\MainBundle\Templating; use Symfony\Component\HttpFoundation\RequestStack; -use Symfony\Component\Translation\Translator; +use Symfony\Contracts\Translation\TranslatorInterface; -/** - * - * This helper helps to find the string in current locale from translatable_strings - * - * @author Julien Fastré - * - */ -class TranslatableStringHelper +final class TranslatableStringHelper implements TranslatableStringHelperInterface { - /** - * - * @var RequestStack - */ - private $requestStack; - - private $fallbackLocales; - - public function __construct(RequestStack $requestStack, Translator $translator) + private RequestStack $requestStack; + + private TranslatorInterface $translator; + + public function __construct(RequestStack $requestStack, TranslatorInterface $translator) { $this->requestStack = $requestStack; - $this->fallbackLocales = $translator->getFallbackLocales(); + $this->translator = $translator; } - - /** - * return the string in current locale if it exists. - * - * If it does not exists; return the name in the first language available. - * - * Return a blank string if any strings are available. - * Return NULL if $translatableString is NULL - * - * @param array $translatableStrings - * @return string - */ - public function localize(array $translatableStrings) - { - if (NULL === $translatableStrings) { - return NULL; - } - - $language = $this->requestStack->getCurrentRequest()->getLocale(); - - if (isset($translatableStrings[$language])) { - - return $translatableStrings[$language]; - } else { - foreach ($this->fallbackLocales as $locale) { - if (array_key_exists($locale, $translatableStrings)) { - - return $translatableStrings[$locale]; - } - } - + public function localize(array $translatableStrings): ?string + { + if ([] === $translatableStrings) { + return null; } - + + $request = $this->requestStack->getCurrentRequest(); + + if (null === $request) { + return null; + } + + $language = $request->getLocale(); + + if (array_key_exists($language, $translatableStrings)) { + return $translatableStrings[$language]; + } + + foreach ($this->translator->getFallbackLocales() as $locale) { + if (array_key_exists($locale, $translatableStrings)) { + return $translatableStrings[$locale]; + } + } + // no fallback translation... trying the first available $langs = array_keys($translatableStrings); - - if (count($langs) === 0) { + + if ([] === $langs) { return ''; } - - return $translatableStrings[$langs[0]]; + return $translatableStrings[$langs[0]]; } -} \ No newline at end of file +} diff --git a/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelperInterface.php b/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelperInterface.php new file mode 100644 index 000000000..72e9397f8 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelperInterface.php @@ -0,0 +1,17 @@ +