From 0a081ab96927bc9d4d4723f43d4f8655890ca225 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Wed, 5 Oct 2022 21:05:08 +0200 Subject: [PATCH 1/2] Set default locale to helpers (#565) --- composer.json | 2 +- src/DependencyInjection/SonataIntlExtension.php | 16 +++++++++------- src/Helper/BaseHelper.php | 6 +++++- src/Helper/DateTimeFormatter.php | 9 ++++++--- src/Helper/NumberFormatter.php | 4 ++-- src/Resources/config/intl.php | 7 +++++++ .../LocaleAwareBasedTimezoneDetector.php | 6 +++++- 7 files changed, 35 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index d723ec27..83d937d5 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,7 @@ "phpunit/phpunit": "^9.5", "psalm/plugin-phpunit": "^0.17", "psalm/plugin-symfony": "^3.0", - "rector/rector": "^0.13", + "rector/rector": "^0.14", "symfony/phpunit-bridge": "^6.1", "symfony/security-core": "^4.4.23 || ^5.4 || ^6.0", "symfony/templating": "^4.4 || ^5.4 || ^6.0", diff --git a/src/DependencyInjection/SonataIntlExtension.php b/src/DependencyInjection/SonataIntlExtension.php index efee2d00..0c6e0cb6 100644 --- a/src/DependencyInjection/SonataIntlExtension.php +++ b/src/DependencyInjection/SonataIntlExtension.php @@ -50,8 +50,6 @@ public function load(array $configs, ContainerBuilder $container) } $this->configureTimezone($container, $config); - - // NEXT_MAJOR: Remove this code $this->configureLocale($container, $config); } @@ -106,17 +104,21 @@ protected function configureTimezone(ContainerBuilder $container, array $config) } /** - * NEXT_MAJOR: remove this. - * - * @deprecated since sonata-project/intl-bundle 2.13, to be removed in version 3.0. - * * @param mixed[] $config * * @return void */ protected function configureLocale(ContainerBuilder $container, array $config) { - $container->getDefinition('sonata.intl.locale_detector.request_stack')->replaceArgument(1, $config['locale'] ?? '%kernel.default_locale%'); + $locale = $config['locale'] ?? '%kernel.default_locale%'; + + $container->getDefinition('sonata.intl.helper.datetime')->replaceArgument(2, $locale); + $container->getDefinition('sonata.intl.helper.locale')->replaceArgument(1, $locale); + $container->getDefinition('sonata.intl.helper.number')->replaceArgument(4, $locale); + $container->getDefinition('sonata.intl.timezone_detector.locale_aware')->replaceArgument(1, $locale); + + // NEXT_MAJOR: Remove the two following line. + $container->getDefinition('sonata.intl.locale_detector.request_stack')->replaceArgument(1, $locale); $container->setAlias('sonata.intl.locale_detector', 'sonata.intl.locale_detector.request_stack'); } diff --git a/src/Helper/BaseHelper.php b/src/Helper/BaseHelper.php index 0d7d7690..b60b04a8 100644 --- a/src/Helper/BaseHelper.php +++ b/src/Helper/BaseHelper.php @@ -39,9 +39,13 @@ abstract class BaseHelper implements LocaleAwareInterface /** * @param string $charset The output charset of the helper */ - public function __construct(string $charset) + public function __construct(string $charset, ?string $defaultLocale = null) { $this->setCharset($charset); + + if (null !== $defaultLocale) { + $this->setLocale($defaultLocale); + } } /** diff --git a/src/Helper/DateTimeFormatter.php b/src/Helper/DateTimeFormatter.php index 4d39e69d..0b0ff2e6 100644 --- a/src/Helper/DateTimeFormatter.php +++ b/src/Helper/DateTimeFormatter.php @@ -31,9 +31,12 @@ final class DateTimeFormatter extends BaseHelper implements DateTimeFormatterInt */ private static ?\ReflectionClass $reflection = null; - public function __construct(TimezoneDetectorInterface $timezoneDetector, string $charset) - { - parent::__construct($charset); + public function __construct( + TimezoneDetectorInterface $timezoneDetector, + string $charset, + ?string $defaultLocale = null + ) { + parent::__construct($charset, $defaultLocale); $this->timezoneDetector = $timezoneDetector; } diff --git a/src/Helper/NumberFormatter.php b/src/Helper/NumberFormatter.php index d3d1ad09..7901d2e0 100644 --- a/src/Helper/NumberFormatter.php +++ b/src/Helper/NumberFormatter.php @@ -42,9 +42,9 @@ final class NumberFormatter extends BaseHelper implements NumberFormatterInterfa * @param array $textAttributes The default text attributes to apply to the \NumberFormatter instance * @param array $symbols The default symbols to apply to the \NumberFormatter instance */ - public function __construct(string $charset, array $attributes = [], array $textAttributes = [], array $symbols = []) + public function __construct(string $charset, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $defaultLocale = null) { - parent::__construct($charset); + parent::__construct($charset, $defaultLocale); $this->attributes = $attributes; $this->textAttributes = $textAttributes; diff --git a/src/Resources/config/intl.php b/src/Resources/config/intl.php index 034b7b83..5799d044 100644 --- a/src/Resources/config/intl.php +++ b/src/Resources/config/intl.php @@ -77,6 +77,7 @@ ->tag('kernel.locale_aware') ->args([ '%kernel.charset%', + '', ]) ->set('sonata.intl.helper.number', '%sonata.intl.helper.number.class%') @@ -84,6 +85,10 @@ ->tag('kernel.locale_aware') ->args([ '%kernel.charset%', + [], + [], + [], + '', ]) ->set('sonata.intl.helper.datetime', '%sonata.intl.helper.datetime.class%') @@ -92,6 +97,7 @@ ->args([ new ReferenceConfigurator('sonata.intl.timezone_detector'), '%kernel.charset%', + '', ]) ->set('sonata.intl.twig.extension.locale', '%sonata.intl.twig.helper.locale.class%') @@ -170,5 +176,6 @@ ->tag('kernel.locale_aware') ->args([ '', + '', ]); }; diff --git a/src/Timezone/LocaleAwareBasedTimezoneDetector.php b/src/Timezone/LocaleAwareBasedTimezoneDetector.php index bbcdc323..c50d9f48 100644 --- a/src/Timezone/LocaleAwareBasedTimezoneDetector.php +++ b/src/Timezone/LocaleAwareBasedTimezoneDetector.php @@ -32,9 +32,13 @@ final class LocaleAwareBasedTimezoneDetector implements TimezoneDetectorInterfac /** * @param array $timezoneMap */ - public function __construct(array $timezoneMap = []) + public function __construct(array $timezoneMap = [], ?string $defaultLocale = null) { $this->timezoneMap = $timezoneMap; + + if (null !== $defaultLocale) { + $this->setLocale($defaultLocale); + } } public function getTimezone(): ?string From 0d58197da26281ca5541b8b3fdcf81f8a98ffb0b Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 6 Oct 2022 08:45:18 +0200 Subject: [PATCH 2/2] 2.14.1 (#566) --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb5eae65..06502c17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## [2.14.1](https://github.com/sonata-project/SonataIntlBundle/compare/2.14.0...2.14.1) - 2022-10-05 +### Fixed +- [[#565](https://github.com/sonata-project/SonataIntlBundle/pull/565)] Use the sonata_intl.locale or kernel.default_locale when no locale are provided. ([@VincentLanglet](https://github.com/VincentLanglet)) + ## [2.14.0](https://github.com/sonata-project/SonataIntlBundle/compare/2.13.1...2.14.0) - 2022-08-16 ### Added - [[#547](https://github.com/sonata-project/SonataIntlBundle/pull/547)] Added support for `symfony/translation-contracts` ^3 ([@AirBair](https://github.com/AirBair))