From 7dfcb047338c4c936b6698b148a75cd1f1e3fc27 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Sun, 1 Jan 2023 17:02:05 +0100 Subject: [PATCH] Implement ChoiceMessageFormatterInterface --- .../MessageFormatterMapperStrongType.php | 15 +----------- .../MessageFormatterMapperWeakType.php | 23 ++++++++----------- phpstan.neon | 2 ++ src/Carbon/Lang/be.php | 4 ++-- src/Carbon/Lang/ca_ES_Valencia.php | 8 +++++++ src/Carbon/Lang/oc.php | 2 +- src/Carbon/Lang/sh.php | 2 +- src/Carbon/Lang/sr_Cyrl_BA.php | 8 +++++++ src/Carbon/Lang/sr_Cyrl_ME.php | 7 ++++++ src/Carbon/Lang/sr_Cyrl_XK.php | 8 +++++++ src/Carbon/Lang/sr_Latn_BA.php | 8 +++++++ src/Carbon/Lang/sr_Latn_ME.php | 7 ++++++ src/Carbon/Lang/sr_Latn_XK.php | 8 +++++++ .../MessageFormatterMapper.php | 17 ++++++++++++++ tests/Carbon/LocalizationTest.php | 1 + tests/CarbonImmutable/LocalizationTest.php | 1 + 16 files changed, 89 insertions(+), 32 deletions(-) diff --git a/lazy/Carbon/MessageFormatter/MessageFormatterMapperStrongType.php b/lazy/Carbon/MessageFormatter/MessageFormatterMapperStrongType.php index 660d71cec2..c2f4bf013b 100644 --- a/lazy/Carbon/MessageFormatter/MessageFormatterMapperStrongType.php +++ b/lazy/Carbon/MessageFormatter/MessageFormatterMapperStrongType.php @@ -11,29 +11,16 @@ namespace Carbon\MessageFormatter; -use Symfony\Component\Translation\Formatter\MessageFormatter; use Symfony\Component\Translation\Formatter\MessageFormatterInterface; if (!class_exists(LazyMessageFormatter::class, false)) { abstract class LazyMessageFormatter implements MessageFormatterInterface { - /** - * Wrapped formatter. - * - * @var MessageFormatterInterface - */ - private $formatter; - - public function __construct(?MessageFormatterInterface $formatter = null) - { - $this->formatter = $formatter ?? new MessageFormatter(); - } - public function format(string $message, string $locale, array $parameters = []): string { return $this->formatter->format( $message, - preg_replace('/[_@][A-Za-z][a-z]{2,}/', '', $locale), + $this->transformLocale($locale), $parameters ); } diff --git a/lazy/Carbon/MessageFormatter/MessageFormatterMapperWeakType.php b/lazy/Carbon/MessageFormatter/MessageFormatterMapperWeakType.php index b7c1c3cea3..cbd890d5b6 100644 --- a/lazy/Carbon/MessageFormatter/MessageFormatterMapperWeakType.php +++ b/lazy/Carbon/MessageFormatter/MessageFormatterMapperWeakType.php @@ -11,31 +11,26 @@ namespace Carbon\MessageFormatter; -use Symfony\Component\Translation\Formatter\MessageFormatter; +use Symfony\Component\Translation\Formatter\ChoiceMessageFormatterInterface; use Symfony\Component\Translation\Formatter\MessageFormatterInterface; if (!class_exists(LazyMessageFormatter::class, false)) { - abstract class LazyMessageFormatter implements MessageFormatterInterface + abstract class LazyMessageFormatter implements MessageFormatterInterface, ChoiceMessageFormatterInterface { - /** - * Wrapped formatter. - * - * @var MessageFormatterInterface - */ - private $formatter; - - public function __construct(?MessageFormatterInterface $formatter = null) - { - $this->formatter = $formatter ?? new MessageFormatter(); - } + abstract protected function transformLocale(?string $locale): ?string; public function format($message, $locale, array $parameters = []) { return $this->formatter->format( $message, - preg_replace('/[_@][A-Za-z][a-z]{2,}/', '', $locale), + $this->transformLocale($locale), $parameters ); } + + public function choiceFormat($message, $number, $locale, array $parameters = []) + { + return $this->formatter->choiceFormat($message, $number, $locale, $parameters); + } } } diff --git a/phpstan.neon b/phpstan.neon index e6a28c05e4..82acf43dc3 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,6 @@ parameters: scanFiles: + - lazy/Carbon/MessageFormatter/MessageFormatterMapperStrongType.php - lazy/Carbon/PHPStan/MacroStrongType.php - lazy/Carbon/TranslatorStrongType.php - vendor/autoload.php @@ -22,6 +23,7 @@ parameters: - '#^Method Carbon\\Carbon(Immutable)?::getTranslator\(\) should return Symfony\\Component\\Translation\\TranslatorInterface but returns Carbon\\Translator\.$#' - '#^Property Carbon\\Carbon(Period|Interval|Immutable)?::\$localTranslator \(Symfony\\Component\\Translation\\TranslatorInterface\) does not accept Carbon\\Translator\.$#' - '#^Static property Carbon\\Carbon(Period|Interval|Immutable)?::\$translator \(Symfony\\Component\\Translation\\TranslatorInterface\) does not accept Carbon\\Translator\.$#' + - '#^Call to static method get\(\) on an unknown class Symfony\\Component\\Translation\\PluralizationRules\.$#' excludePaths: - '*/src/Carbon/Laravel/ServiceProvider.php' - '*/src/Carbon/PHPStan/*' diff --git a/src/Carbon/Lang/be.php b/src/Carbon/Lang/be.php index 51b4d0cc14..567ac001a8 100644 --- a/src/Carbon/Lang/be.php +++ b/src/Carbon/Lang/be.php @@ -14,8 +14,8 @@ use Carbon\CarbonInterface; use Symfony\Component\Translation\PluralizationRules; -if (class_exists('Symfony\\Component\\Translation\\PluralizationRules')) { - PluralizationRules::set(function ($number) { +if (class_exists(PluralizationRules::class)) { + PluralizationRules::set(static function ($number) { return (($number % 10 == 1) && ($number % 100 != 11)) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2); }, 'be'); } diff --git a/src/Carbon/Lang/ca_ES_Valencia.php b/src/Carbon/Lang/ca_ES_Valencia.php index 861acd2a06..df45f56646 100644 --- a/src/Carbon/Lang/ca_ES_Valencia.php +++ b/src/Carbon/Lang/ca_ES_Valencia.php @@ -9,5 +9,13 @@ * file that was distributed with this source code. */ +use Symfony\Component\Translation\PluralizationRules; + +if (class_exists(PluralizationRules::class)) { + PluralizationRules::set(static function ($number) { + return PluralizationRules::get($number, 'ca'); + }, 'ca_ES_Valencia'); +} + return array_replace_recursive(require __DIR__.'/ca.php', [ ]); diff --git a/src/Carbon/Lang/oc.php b/src/Carbon/Lang/oc.php index 89693e674b..c9411d69de 100644 --- a/src/Carbon/Lang/oc.php +++ b/src/Carbon/Lang/oc.php @@ -17,7 +17,7 @@ use Symfony\Component\Translation\PluralizationRules; if (class_exists('Symfony\\Component\\Translation\\PluralizationRules')) { - PluralizationRules::set(function ($number) { + PluralizationRules::set(static function ($number) { return $number == 1 ? 0 : 1; }, 'oc'); } diff --git a/src/Carbon/Lang/sh.php b/src/Carbon/Lang/sh.php index e4aa5a1c71..e03b506752 100644 --- a/src/Carbon/Lang/sh.php +++ b/src/Carbon/Lang/sh.php @@ -13,7 +13,7 @@ use Symfony\Component\Translation\PluralizationRules; if (class_exists('Symfony\\Component\\Translation\\PluralizationRules')) { - PluralizationRules::set(function ($number) { + PluralizationRules::set(static function ($number) { return (($number % 10 == 1) && ($number % 100 != 11)) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2); }, 'sh'); } diff --git a/src/Carbon/Lang/sr_Cyrl_BA.php b/src/Carbon/Lang/sr_Cyrl_BA.php index 0fb63d769d..56985f2562 100644 --- a/src/Carbon/Lang/sr_Cyrl_BA.php +++ b/src/Carbon/Lang/sr_Cyrl_BA.php @@ -9,6 +9,14 @@ * file that was distributed with this source code. */ +use Symfony\Component\Translation\PluralizationRules; + +if (class_exists(PluralizationRules::class)) { + PluralizationRules::set(static function ($number) { + return PluralizationRules::get($number, 'sr'); + }, 'sr_Cyrl_BA'); +} + return array_replace_recursive(require __DIR__.'/sr_Cyrl.php', [ 'formats' => [ 'LT' => 'HH:mm', diff --git a/src/Carbon/Lang/sr_Cyrl_ME.php b/src/Carbon/Lang/sr_Cyrl_ME.php index 2e6717720a..b8cf63b50c 100644 --- a/src/Carbon/Lang/sr_Cyrl_ME.php +++ b/src/Carbon/Lang/sr_Cyrl_ME.php @@ -16,6 +16,13 @@ */ use Carbon\CarbonInterface; +use Symfony\Component\Translation\PluralizationRules; + +if (class_exists(PluralizationRules::class)) { + PluralizationRules::set(static function ($number) { + return PluralizationRules::get($number, 'sr'); + }, 'sr_Cyrl_ME'); +} return [ 'year' => ':count година|:count године|:count година', diff --git a/src/Carbon/Lang/sr_Cyrl_XK.php b/src/Carbon/Lang/sr_Cyrl_XK.php index 492baf0cbd..8dec918e91 100644 --- a/src/Carbon/Lang/sr_Cyrl_XK.php +++ b/src/Carbon/Lang/sr_Cyrl_XK.php @@ -9,6 +9,14 @@ * file that was distributed with this source code. */ +use Symfony\Component\Translation\PluralizationRules; + +if (class_exists(PluralizationRules::class)) { + PluralizationRules::set(static function ($number) { + return PluralizationRules::get($number, 'sr'); + }, 'sr_Cyrl_XK'); +} + return array_replace_recursive(require __DIR__.'/sr_Cyrl_BA.php', [ 'weekdays' => ['недеља', 'понедељак', 'уторак', 'среда', 'четвртак', 'петак', 'субота'], ]); diff --git a/src/Carbon/Lang/sr_Latn_BA.php b/src/Carbon/Lang/sr_Latn_BA.php index 897c674a51..b5dbd50fc8 100644 --- a/src/Carbon/Lang/sr_Latn_BA.php +++ b/src/Carbon/Lang/sr_Latn_BA.php @@ -9,6 +9,14 @@ * file that was distributed with this source code. */ +use Symfony\Component\Translation\PluralizationRules; + +if (class_exists(PluralizationRules::class)) { + PluralizationRules::set(static function ($number) { + return PluralizationRules::get($number, 'sr'); + }, 'sr_Latn_BA'); +} + return array_replace_recursive(require __DIR__.'/sr_Latn.php', [ 'formats' => [ 'LT' => 'HH:mm', diff --git a/src/Carbon/Lang/sr_Latn_ME.php b/src/Carbon/Lang/sr_Latn_ME.php index 44a4d75f65..9e9f37bb3a 100644 --- a/src/Carbon/Lang/sr_Latn_ME.php +++ b/src/Carbon/Lang/sr_Latn_ME.php @@ -16,6 +16,13 @@ */ use Carbon\CarbonInterface; +use Symfony\Component\Translation\PluralizationRules; + +if (class_exists(PluralizationRules::class)) { + PluralizationRules::set(static function ($number) { + return PluralizationRules::get($number, 'sr'); + }, 'sr_Latn_ME'); +} return array_replace_recursive(require __DIR__.'/sr.php', [ 'month' => ':count mjesec|:count mjeseca|:count mjeseci', diff --git a/src/Carbon/Lang/sr_Latn_XK.php b/src/Carbon/Lang/sr_Latn_XK.php index d0b9d10bb4..cb884953b4 100644 --- a/src/Carbon/Lang/sr_Latn_XK.php +++ b/src/Carbon/Lang/sr_Latn_XK.php @@ -9,6 +9,14 @@ * file that was distributed with this source code. */ +use Symfony\Component\Translation\PluralizationRules; + +if (class_exists(PluralizationRules::class)) { + PluralizationRules::set(static function ($number) { + return PluralizationRules::get($number, 'sr'); + }, 'sr_Latn_XK'); +} + return array_replace_recursive(require __DIR__.'/sr_Latn_BA.php', [ 'weekdays' => ['nedelja', 'ponedeljak', 'utorak', 'sreda', 'četvrtak', 'petak', 'subota'], ]); diff --git a/src/Carbon/MessageFormatter/MessageFormatterMapper.php b/src/Carbon/MessageFormatter/MessageFormatterMapper.php index e9c1dd1c7c..a230b06315 100644 --- a/src/Carbon/MessageFormatter/MessageFormatterMapper.php +++ b/src/Carbon/MessageFormatter/MessageFormatterMapper.php @@ -12,6 +12,7 @@ namespace Carbon\MessageFormatter; use ReflectionMethod; +use Symfony\Component\Translation\Formatter\MessageFormatter; use Symfony\Component\Translation\Formatter\MessageFormatterInterface; $transMethod = new ReflectionMethod(MessageFormatterInterface::class, 'format'); @@ -22,4 +23,20 @@ final class MessageFormatterMapper extends LazyMessageFormatter { + /** + * Wrapped formatter. + * + * @var MessageFormatterInterface + */ + protected $formatter; + + public function __construct(?MessageFormatterInterface $formatter = null) + { + $this->formatter = $formatter ?? new MessageFormatter(); + } + + protected function transformLocale(?string $locale): ?string + { + return $locale ? preg_replace('/[_@][A-Za-z][a-z]{2,}/', '', $locale) : $locale; + } } diff --git a/tests/Carbon/LocalizationTest.php b/tests/Carbon/LocalizationTest.php index 6d9c216893..d01c0513c8 100644 --- a/tests/Carbon/LocalizationTest.php +++ b/tests/Carbon/LocalizationTest.php @@ -917,6 +917,7 @@ public function testAustriaGermanJanuary() public function testDeclensionModes() { + Carbon::setTestNow('2022-12-30'); $this->assertSame( '2 жил 3 сар 1 өдөр 1с өмнө', Carbon::now() diff --git a/tests/CarbonImmutable/LocalizationTest.php b/tests/CarbonImmutable/LocalizationTest.php index 089aec6e08..bc89a32b22 100644 --- a/tests/CarbonImmutable/LocalizationTest.php +++ b/tests/CarbonImmutable/LocalizationTest.php @@ -867,6 +867,7 @@ public function testPolishDeclensions() public function testDeclensionModes() { + Carbon::setTestNow('2022-12-30'); $this->assertSame( '2 жил 3 сар 1 өдөр 1с өмнө', Carbon::now()