From a87a657367aed051c945ba1d674a4c39137c38f3 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 8 Sep 2022 11:52:34 +1200 Subject: [PATCH] API Update translation to use symfony 6 --- _config/i18n.yml | 4 ++-- composer.json | 2 +- src/i18n/Messages/Symfony/ModuleYamlLoader.php | 6 ++---- src/i18n/Messages/Symfony/SymfonyMessageProvider.php | 10 ++++------ 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/_config/i18n.yml b/_config/i18n.yml index a219828b8..590ab10b9 100644 --- a/_config/i18n.yml +++ b/_config/i18n.yml @@ -33,7 +33,7 @@ SilverStripe\Core\Injector\Injector: constructor: 0: [ '%$Symfony\Component\Config\Resource\SelfCheckingResourceChecker' ] # Create default translator with standard cache path and our custom loader - Symfony\Component\Translation\TranslatorInterface: + Symfony\Contracts\Translation\TranslatorInterface: class: Symfony\Component\Translation\Translator constructor: 0: 'en' @@ -48,7 +48,7 @@ SilverStripe\Core\Injector\Injector: SilverStripe\i18n\Messages\MessageProvider: class: SilverStripe\i18n\Messages\Symfony\SymfonyMessageProvider properties: - Translator: '%$Symfony\Component\Translation\TranslatorInterface' + Translator: '%$Symfony\Contracts\Translation\TranslatorInterface' --- Name: textcollector --- diff --git a/composer.json b/composer.json index 45ae114d2..1cd80f8cd 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,7 @@ "symfony/cache": "^6.1", "symfony/config": "^6.1", "symfony/filesystem": "^6.1", - "symfony/translation": "^4.4.44", + "symfony/translation": "^6.1", "symfony/yaml": "^6.1", "ext-ctype": "*", "ext-dom": "*", diff --git a/src/i18n/Messages/Symfony/ModuleYamlLoader.php b/src/i18n/Messages/Symfony/ModuleYamlLoader.php index d7f4f3fce..d726bca9b 100644 --- a/src/i18n/Messages/Symfony/ModuleYamlLoader.php +++ b/src/i18n/Messages/Symfony/ModuleYamlLoader.php @@ -2,12 +2,10 @@ namespace SilverStripe\i18n\Messages\Symfony; -use SilverStripe\Core\Config\Configurable; -use SilverStripe\Dev\Debug; use SilverStripe\i18n\i18n; use SilverStripe\i18n\Messages\Reader; use Symfony\Component\Translation\Loader\ArrayLoader; -use Symfony\Component\Translation\PluralizationRules; +use Symfony\Component\Translation\MessageCatalogue; /** * Loads yaml localisations across all modules simultaneously. @@ -23,7 +21,7 @@ class ModuleYamlLoader extends ArrayLoader */ protected $reader = null; - public function load($resource, $locale, $domain = 'messages') + public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue { $messages = []; foreach ($resource as $path) { diff --git a/src/i18n/Messages/Symfony/SymfonyMessageProvider.php b/src/i18n/Messages/Symfony/SymfonyMessageProvider.php index 696a1d030..31c3a4750 100644 --- a/src/i18n/Messages/Symfony/SymfonyMessageProvider.php +++ b/src/i18n/Messages/Symfony/SymfonyMessageProvider.php @@ -115,17 +115,15 @@ class SymfonyMessageProvider implements MessageProvider $this->load($locale); // Prepare arguments - $arguments = $this->templateInjection(array_merge( - $injection, - [ 'count' => $count ] - )); + $arguments = $this->templateInjection($injection); + $arguments['%count%'] = $count; // Pass to symfony translator - $result = $this->getTranslator()->transChoice($entity, $count, $arguments, 'messages', $locale); + $result = $this->getTranslator()->trans($entity, $arguments, 'messages', $locale); // Manually inject default if no translation found if ($entity === $result) { - $result = $this->getTranslator()->transChoice($default, $count, $arguments, 'messages', $locale); + $result = $this->getTranslator()->trans($default, $arguments, 'messages', $locale); } return $result;