Skip to content

Commit

Permalink
Merge pull request #2740 from briannesbitt/fix/sr-pluralization
Browse files Browse the repository at this point in the history
Fix Serbian pluralization
  • Loading branch information
kylekatarnls authored Jan 1, 2023
2 parents 94d6290 + 74bd3be commit 38610c2
Show file tree
Hide file tree
Showing 23 changed files with 290 additions and 111 deletions.
28 changes: 28 additions & 0 deletions lazy/Carbon/MessageFormatter/MessageFormatterMapperStrongType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Carbon\MessageFormatter;

use Symfony\Component\Translation\Formatter\MessageFormatterInterface;

if (!class_exists(LazyMessageFormatter::class, false)) {
abstract class LazyMessageFormatter implements MessageFormatterInterface
{
public function format(string $message, string $locale, array $parameters = []): string
{
return $this->formatter->format(
$message,
$this->transformLocale($locale),
$parameters
);
}
}
}
36 changes: 36 additions & 0 deletions lazy/Carbon/MessageFormatter/MessageFormatterMapperWeakType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Carbon\MessageFormatter;

use Symfony\Component\Translation\Formatter\ChoiceMessageFormatterInterface;
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;

if (!class_exists(LazyMessageFormatter::class, false)) {
abstract class LazyMessageFormatter implements MessageFormatterInterface, ChoiceMessageFormatterInterface
{
abstract protected function transformLocale(?string $locale): ?string;

public function format($message, $locale, array $parameters = [])
{
return $this->formatter->format(
$message,
$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
5 changes: 3 additions & 2 deletions src/Carbon/AbstractTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Carbon;

use Carbon\MessageFormatter\MessageFormatterMapper;
use Closure;
use ReflectionException;
use ReflectionFunction;
Expand Down Expand Up @@ -51,7 +52,7 @@ abstract class AbstractTranslator extends Translation\Translator
/**
* List of locales aliases.
*
* @var string[]
* @var array<string, string>
*/
protected $aliases = [
'me' => 'sr_Latn_ME',
Expand Down Expand Up @@ -83,7 +84,7 @@ public function __construct($locale, MessageFormatterInterface $formatter = null
$this->initializing = true;
$this->directories = [__DIR__.'/Lang'];
$this->addLoader('array', new ArrayLoader());
parent::__construct($locale, $formatter, $cacheDir, $debug);
parent::__construct($locale, new MessageFormatterMapper($formatter), $cacheDir, $debug);
$this->initializing = false;
}

Expand Down
7 changes: 3 additions & 4 deletions src/Carbon/Lang/be.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
* file that was distributed with this source code.
*/

// @codeCoverageIgnoreStart

use Carbon\CarbonInterface;
use Symfony\Component\Translation\PluralizationRules;

if (class_exists('Symfony\\Component\\Translation\\PluralizationRules')) {
PluralizationRules::set(function ($number) {
// @codeCoverageIgnoreStart
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
10 changes: 10 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,15 @@
* file that was distributed with this source code.
*/

use Symfony\Component\Translation\PluralizationRules;

// @codeCoverageIgnoreStart
if (class_exists(PluralizationRules::class)) {
PluralizationRules::set(static function ($number) {
return PluralizationRules::get($number, 'ca');
}, 'ca_ES_Valencia');
}
// @codeCoverageIgnoreEnd

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
22 changes: 11 additions & 11 deletions src/Carbon/Lang/sr_Cyrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@
use Carbon\CarbonInterface;

return [
'year' => '{2,3,4,22,23,24,32,33,34,42,43,44,52,53,54}:count године|[0,Inf[:count година',
'year' => ':count година|:count године|:count година',
'y' => ':count г.',
'month' => '{1}:count месец|{2,3,4}:count месеца|[0,Inf[:count месеци',
'month' => ':count месец|:count месеца|:count месеци',
'm' => ':count м.',
'week' => '{1}:count недеља|{2,3,4}:count недеље|[0,Inf[:count недеља',
'week' => ':count недеља|:count недеље|:count недеља',
'w' => ':count нед.',
'day' => '{1,21,31}:count дан|[0,Inf[:count дана',
'day' => ':count дан|:count дана|:count дана',
'd' => ':count д.',
'hour' => '{1,21}:count сат|{2,3,4,22,23,24}:count сата|[0,Inf[:count сати',
'hour' => ':count сат|:count сата|:count сати',
'h' => ':count ч.',
'minute' => '{1,21,31,41,51}:count минут|[0,Inf[:count минута',
'minute' => ':count минут|:count минута|:count минута',
'min' => ':count мин.',
'second' => '{1,21,31,41,51}:count секунд|{2,3,4,22,23,24,32,33,34,42,43,44,52,53,54}:count секунде|[0,Inf[:count секунди',
'second' => ':count секунд|:count секунде|:count секунди',
's' => ':count сек.',
'ago' => 'пре :time',
'from_now' => 'за :time',
'after' => ':time након',
'before' => ':time пре',
'year_from_now' => '{1,21,31,41,51}:count годину|{2,3,4,22,23,24,32,33,34,42,43,44,52,53,54}:count године|[0,Inf[:count година',
'year_ago' => '{1,21,31,41,51}:count годину|{2,3,4,22,23,24,32,33,34,42,43,44,52,53,54}:count године|[0,Inf[:count година',
'week_from_now' => '{1}:count недељу|{2,3,4}:count недеље|[0,Inf[:count недеља',
'week_ago' => '{1}:count недељу|{2,3,4}:count недеље|[0,Inf[:count недеља',
'year_from_now' => ':count годину|:count године|:count година',
'year_ago' => ':count годину|:count године|:count година',
'week_from_now' => ':count недељу|:count недеље|:count недеља',
'week_ago' => ':count недељу|:count недеље|:count недеља',
'diff_now' => 'управо сада',
'diff_today' => 'данас',
'diff_today_regexp' => 'данас(?:\\s+у)?',
Expand Down
10 changes: 10 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,16 @@
* file that was distributed with this source code.
*/

use Symfony\Component\Translation\PluralizationRules;

// @codeCoverageIgnoreStart
if (class_exists(PluralizationRules::class)) {
PluralizationRules::set(static function ($number) {
return PluralizationRules::get($number, 'sr');
}, 'sr_Cyrl_BA');
}
// @codeCoverageIgnoreEnd

return array_replace_recursive(require __DIR__.'/sr_Cyrl.php', [
'formats' => [
'LT' => 'HH:mm',
Expand Down
31 changes: 20 additions & 11 deletions src/Carbon/Lang/sr_Cyrl_ME.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,41 @@
*/

use Carbon\CarbonInterface;
use Symfony\Component\Translation\PluralizationRules;

// @codeCoverageIgnoreStart
if (class_exists(PluralizationRules::class)) {
PluralizationRules::set(static function ($number) {
return PluralizationRules::get($number, 'sr');
}, 'sr_Cyrl_ME');
}
// @codeCoverageIgnoreEnd

return [
'year' => '{2,3,4,22,23,24,32,33,34,42,43,44,52,53,54}:count године|[0,Inf[:count година',
'year' => ':count година|:count године|:count година',
'y' => ':count г.',
'month' => '{1}:count мјесец|{2,3,4}:count мјесеца|[0,Inf[:count мјесеци',
'month' => ':count мјесец|:count мјесеца|:count мјесеци',
'm' => ':count мј.',
'week' => '{1}:count недјеља|{2,3,4}:count недјеље|[0,Inf[:count недјеља',
'week' => ':count недјеља|:count недјеље|:count недјеља',
'w' => ':count нед.',
'day' => '{1,21,31}:count дан|[0,Inf[:count дана',
'day' => ':count дан|:count дана|:count дана',
'd' => ':count д.',
'hour' => '{1,21}:count сат|{2,3,4,22,23,24}:count сата|[0,Inf[:count сати',
'hour' => ':count сат|:count сата|:count сати',
'h' => ':count ч.',
'minute' => '{1,21,31,41,51}:count минут|[0,Inf[:count минута',
'minute' => ':count минут|:count минута|:count минута',
'min' => ':count мин.',
'second' => '{1,21,31,41,51}:count секунд|{2,3,4,22,23,24,32,33,34,42,43,44,52,53,54}:count секунде|[0,Inf[:count секунди',
'second' => ':count секунд|:count секунде|:count секунди',
's' => ':count сек.',
'ago' => 'прије :time',
'from_now' => 'за :time',
'after' => ':time након',
'before' => ':time прије',

'year_from_now' => '{1,21,31,41,51}:count годину|{2,3,4,22,23,24,32,33,34,42,43,44,52,53,54}:count године|[0,Inf[:count година',
'year_ago' => '{1,21,31,41,51}:count годину|{2,3,4,22,23,24,32,33,34,42,43,44,52,53,54}:count године|[0,Inf[:count година',
'year_from_now' => ':count годину|:count године|:count година',
'year_ago' => ':count годину|:count године|:count година',

'week_from_now' => '{1}:count недјељу|{2,3,4}:count недјеље|[0,Inf[:count недјеља',
'week_ago' => '{1}:count недјељу|{2,3,4}:count недјеље|[0,Inf[:count недјеља',
'week_from_now' => ':count недјељу|:count недјеље|:count недјеља',
'week_ago' => ':count недјељу|:count недјеље|:count недјеља',

'diff_now' => 'управо сада',
'diff_today' => 'данас',
Expand Down
10 changes: 10 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,16 @@
* file that was distributed with this source code.
*/

use Symfony\Component\Translation\PluralizationRules;

// @codeCoverageIgnoreStart
if (class_exists(PluralizationRules::class)) {
PluralizationRules::set(static function ($number) {
return PluralizationRules::get($number, 'sr');
}, 'sr_Cyrl_XK');
}
// @codeCoverageIgnoreEnd

return array_replace_recursive(require __DIR__.'/sr_Cyrl_BA.php', [
'weekdays' => ['недеља', 'понедељак', 'уторак', 'среда', 'четвртак', 'петак', 'субота'],
]);
10 changes: 10 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,16 @@
* file that was distributed with this source code.
*/

use Symfony\Component\Translation\PluralizationRules;

// @codeCoverageIgnoreStart
if (class_exists(PluralizationRules::class)) {
PluralizationRules::set(static function ($number) {
return PluralizationRules::get($number, 'sr');
}, 'sr_Latn_BA');
}
// @codeCoverageIgnoreEnd

return array_replace_recursive(require __DIR__.'/sr_Latn.php', [
'formats' => [
'LT' => 'HH:mm',
Expand Down
10 changes: 10 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,15 @@
*/

use Carbon\CarbonInterface;
use Symfony\Component\Translation\PluralizationRules;

// @codeCoverageIgnoreStart
if (class_exists(PluralizationRules::class)) {
PluralizationRules::set(static function ($number) {
return PluralizationRules::get($number, 'sr');
}, 'sr_Latn_ME');
}
// @codeCoverageIgnoreEnd

return array_replace_recursive(require __DIR__.'/sr.php', [
'month' => ':count mjesec|:count mjeseca|:count mjeseci',
Expand All @@ -27,6 +36,7 @@
'before' => ':time prije',
'week_from_now' => ':count nedjelju|:count nedjelje|:count nedjelja',
'week_ago' => ':count nedjelju|:count nedjelje|:count nedjelja',
'second_ago' => ':count sekund|:count sekunde|:count sekundi',
'diff_tomorrow' => 'sjutra',
'calendar' => [
'nextDay' => '[sjutra u] LT',
Expand Down
10 changes: 10 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,16 @@
* file that was distributed with this source code.
*/

use Symfony\Component\Translation\PluralizationRules;

// @codeCoverageIgnoreStart
if (class_exists(PluralizationRules::class)) {
PluralizationRules::set(static function ($number) {
return PluralizationRules::get($number, 'sr');
}, 'sr_Latn_XK');
}
// @codeCoverageIgnoreEnd

return array_replace_recursive(require __DIR__.'/sr_Latn_BA.php', [
'weekdays' => ['nedelja', 'ponedeljak', 'utorak', 'sreda', 'četvrtak', 'petak', 'subota'],
]);
42 changes: 42 additions & 0 deletions src/Carbon/MessageFormatter/MessageFormatterMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Carbon\MessageFormatter;

use ReflectionMethod;
use Symfony\Component\Translation\Formatter\MessageFormatter;
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;

$transMethod = new ReflectionMethod(MessageFormatterInterface::class, 'format');

require $transMethod->getParameters()[0]->hasType()
? __DIR__.'/../../../lazy/Carbon/MessageFormatter/MessageFormatterMapperStrongType.php'
: __DIR__.'/../../../lazy/Carbon/MessageFormatter/MessageFormatterMapperWeakType.php';

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
Loading

0 comments on commit 38610c2

Please sign in to comment.