Skip to content

Commit

Permalink
Merge branch '2.x' into 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Oct 6, 2022
2 parents 881a6e1 + 0d58197 commit a853e96
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/).

## [3.0.2](https://github.com/sonata-project/SonataIntlBundle/compare/3.0.1...3.0.2) - 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))

## [3.0.1](https://github.com/sonata-project/SonataIntlBundle/compare/3.0.0...3.0.1) - 2022-09-27
### Fixed
- [[#560](https://github.com/sonata-project/SonataIntlBundle/pull/560)] All the templates using `sonata_number_format_*` methods ([@VincentLanglet](https://github.com/VincentLanglet))
Expand All @@ -19,6 +23,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- [[#539](https://github.com/sonata-project/SonataIntlBundle/pull/539)] Final keyword to all classes ([@VincentLanglet](https://github.com/VincentLanglet))
- [[#539](https://github.com/sonata-project/SonataIntlBundle/pull/539)] Typehint to all methods ([@VincentLanglet](https://github.com/VincentLanglet))

## [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))
Expand Down
16 changes: 16 additions & 0 deletions src/DependencyInjection/SonataIntlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('intl.php');

$this->configureTimezone($container, $config);
$this->configureLocale($container, $config);
}

/**
Expand Down Expand Up @@ -86,6 +87,21 @@ private function configureTimezone(ContainerBuilder $container, array $config):
}

/**
* @param mixed[] $config
*/
private function configureLocale(ContainerBuilder $container, array $config): void
{
$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);
}

/**
* Validate timezones.
*
* @param array<string> $timezones
*
* @throws \RuntimeException If one of the locales is invalid
Expand Down
6 changes: 5 additions & 1 deletion src/Helper/BaseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ abstract class BaseHelper implements LocaleAwareInterface

private ?string $locale = null;

public function __construct(string $charset)
public function __construct(string $charset, ?string $defaultLocale = null)
{
$this->setCharset($charset);

if (null !== $defaultLocale) {
$this->setLocale($defaultLocale);
}
}

public function setCharset(string $charset): void
Expand Down
5 changes: 3 additions & 2 deletions src/Helper/DateTimeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ final class DateTimeFormatter extends BaseHelper implements DateTimeFormatterInt

public function __construct(
private TimezoneDetectorInterface $timezoneDetector,
string $charset
string $charset,
?string $defaultLocale = null
) {
parent::__construct($charset);
parent::__construct($charset, $defaultLocale);
}

public function formatDate(
Expand Down
5 changes: 3 additions & 2 deletions src/Helper/NumberFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public function __construct(
string $charset,
private array $attributes = [],
private array $textAttributes = [],
private array $symbols = []
private array $symbols = [],
?string $defaultLocale = null
) {
parent::__construct($charset);
parent::__construct($charset, $defaultLocale);
}

public function formatPercent(string|float|int $number, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
Expand Down
7 changes: 7 additions & 0 deletions src/Resources/config/intl.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@
->tag('kernel.locale_aware')
->args([
param('kernel.charset'),
abstract_arg('default locale'),
])

->set('sonata.intl.helper.number', NumberFormatter::class)
->public()
->tag('kernel.locale_aware')
->args([
param('kernel.charset'),
[],
[],
[],
abstract_arg('default locale'),
])

->set('sonata.intl.helper.datetime', DateTimeFormatter::class)
Expand All @@ -49,6 +54,7 @@
->args([
service('sonata.intl.timezone_detector'),
param('kernel.charset'),
abstract_arg('default locale'),
])

->set('sonata.intl.twig.extension.locale', LocaleExtension::class)
Expand Down Expand Up @@ -113,5 +119,6 @@
->tag('kernel.locale_aware')
->args([
abstract_arg('timezone map'),
abstract_arg('default locale'),
]);
};
5 changes: 4 additions & 1 deletion src/Timezone/LocaleAwareBasedTimezoneDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ final class LocaleAwareBasedTimezoneDetector implements TimezoneDetectorInterfac
/**
* @param array<string, string> $timezoneMap
*/
public function __construct(private array $timezoneMap = [])
public function __construct(private array $timezoneMap = [], ?string $defaultLocale = null)
{
if (null !== $defaultLocale) {
$this->setLocale($defaultLocale);
}
}

public function getTimezone(): ?string
Expand Down

0 comments on commit a853e96

Please sign in to comment.