From 97e53698cc56f2687f70fc0084eddb96ac9b56d2 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 7 Sep 2022 16:46:55 +1200 Subject: [PATCH] API Update translation to use symfony 6 --- _config/i18n.yml | 4 ++-- composer.json | 3 ++- src/i18n/Messages/Symfony/ModuleYamlLoader.php | 6 ++---- src/i18n/Messages/Symfony/SymfonyMessageProvider.php | 10 ++++------ 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/_config/i18n.yml b/_config/i18n.yml index a219828b8f8..590ab10b93b 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 45ae114d233..972a14644c4 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,8 @@ "symfony/cache": "^6.1", "symfony/config": "^6.1", "symfony/filesystem": "^6.1", - "symfony/translation": "^4.4.44", + "symfony/translation": "^6.1", + "symfony/translation-contracts": "^3.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 d7f4f3fcefa..d726bca9b34 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 696a1d030f2..31c3a4750aa 100644 --- a/src/i18n/Messages/Symfony/SymfonyMessageProvider.php +++ b/src/i18n/Messages/Symfony/SymfonyMessageProvider.php @@ -115,17 +115,15 @@ public function pluralise($entity, $default, $injection, $count) $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;