Skip to content

Commit

Permalink
Implement ChoiceMessageFormatterInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Jan 1, 2023
1 parent fea280e commit 7dfcb04
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down
23 changes: 9 additions & 14 deletions lazy/Carbon/MessageFormatter/MessageFormatterMapperWeakType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
parameters:
scanFiles:
- lazy/Carbon/MessageFormatter/MessageFormatterMapperStrongType.php
- lazy/Carbon/PHPStan/MacroStrongType.php
- lazy/Carbon/TranslatorStrongType.php
- vendor/autoload.php
Expand All @@ -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/*'
Expand Down
4 changes: 2 additions & 2 deletions src/Carbon/Lang/be.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
8 changes: 8 additions & 0 deletions src/Carbon/Lang/ca_ES_Valencia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
]);
2 changes: 1 addition & 1 deletion src/Carbon/Lang/oc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Carbon/Lang/sh.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
8 changes: 8 additions & 0 deletions src/Carbon/Lang/sr_Cyrl_BA.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 7 additions & 0 deletions src/Carbon/Lang/sr_Cyrl_ME.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 година',
Expand Down
8 changes: 8 additions & 0 deletions src/Carbon/Lang/sr_Cyrl_XK.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => ['недеља', 'понедељак', 'уторак', 'среда', 'четвртак', 'петак', 'субота'],
]);
8 changes: 8 additions & 0 deletions src/Carbon/Lang/sr_Latn_BA.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 7 additions & 0 deletions src/Carbon/Lang/sr_Latn_ME.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 8 additions & 0 deletions src/Carbon/Lang/sr_Latn_XK.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
]);
17 changes: 17 additions & 0 deletions src/Carbon/MessageFormatter/MessageFormatterMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions tests/Carbon/LocalizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@ public function testAustriaGermanJanuary()

public function testDeclensionModes()
{
Carbon::setTestNow('2022-12-30');
$this->assertSame(
'2 жил 3 сар 1 өдөр 1с өмнө',
Carbon::now()
Expand Down
1 change: 1 addition & 0 deletions tests/CarbonImmutable/LocalizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,7 @@ public function testPolishDeclensions()

public function testDeclensionModes()
{
Carbon::setTestNow('2022-12-30');
$this->assertSame(
'2 жил 3 сар 1 өдөр 1с өмнө',
Carbon::now()
Expand Down

0 comments on commit 7dfcb04

Please sign in to comment.