-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Translation] Separate parameters formatting #41136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation\Tests\Translatable; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Translation\Translatable\DateTimeTranslatable; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
class DateTimeTranslatableTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
if (!\extension_loaded('intl')) { | ||
$this->markTestSkipped('Extension intl is required.'); | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider getValues() | ||
*/ | ||
public function testFormat(string $expected, DateTimeTranslatable $parameter, string $locale) | ||
{ | ||
$translator = $this->createMock(TranslatorInterface::class); | ||
$this->assertSame($expected, $parameter->trans($translator, $locale)); | ||
} | ||
|
||
public function getValues(): iterable | ||
{ | ||
$dateTime = new \DateTime('2021-01-01 23:55:00', new \DateTimeZone('UTC')); | ||
|
||
$parameterDateTime = new DateTimeTranslatable($dateTime); | ||
yield 'DateTime in French' => ['01/01/2021 23:55', $parameterDateTime, 'fr_FR']; | ||
yield 'DateTime in GB English' => ['01/01/2021, 23:55', $parameterDateTime, 'en_GB']; | ||
yield 'DateTime in US English' => ['1/1/21, 11:55 PM', $parameterDateTime, 'en_US']; | ||
|
||
$dateTimeParis = new \DateTime('2021-01-01 23:55:00', new \DateTimeZone('UTC')); | ||
$dateTimeParis->setTimezone(new \DateTimeZone('Europe/Paris')); | ||
|
||
$parameterDateTimeParis = new DateTimeTranslatable($dateTimeParis); | ||
yield 'DateTime in Paris in French' => ['02/01/2021 00:55', $parameterDateTimeParis, 'fr_FR']; | ||
yield 'DateTime in Paris in GB English' => ['02/01/2021, 00:55', $parameterDateTimeParis, 'en_GB']; | ||
yield 'DateTime in Paris in US English' => ['1/2/21, 12:55 AM', $parameterDateTimeParis, 'en_US']; | ||
|
||
$parameterDateParis = DateTimeTranslatable::date($dateTimeParis); | ||
yield 'Date in Paris in French' => ['02/01/2021', $parameterDateParis, 'fr_FR']; | ||
yield 'Date in Paris in GB English' => ['02/01/2021', $parameterDateParis, 'en_GB']; | ||
yield 'Date in Paris in US English' => ['1/2/21', $parameterDateParis, 'en_US']; | ||
|
||
$parameterTimeParis = DateTimeTranslatable::time($dateTimeParis); | ||
yield 'Time in Paris in French' => ['00:55', $parameterTimeParis, 'fr_FR']; | ||
yield 'Time in Paris in GB English' => ['00:55', $parameterTimeParis, 'en_GB']; | ||
yield 'Time in Paris in US English' => ['12:55 AM', $parameterTimeParis, 'en_US']; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation\Tests\Translatable; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Translation\Translatable\DecimalTranslatable; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
class DecimalTranslatableTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
if (!\extension_loaded('intl')) { | ||
$this->markTestSkipped('Extension intl is required.'); | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider getValues() | ||
*/ | ||
public function testFormat(string $expected, DecimalTranslatable $parameter, string $locale) | ||
{ | ||
$translator = $this->createMock(TranslatorInterface::class); | ||
// Non-breakable spaces are added differently depending the PHP version | ||
$cleaned = str_replace(["\u{202f}", "\u{a0}"], ['', ''], $parameter->trans($translator, $locale)); | ||
$this->assertSame($expected, $cleaned); | ||
} | ||
|
||
public function getValues(): iterable | ||
{ | ||
$parameter = new DecimalTranslatable(1000); | ||
|
||
yield 'French' => ['1000', $parameter, 'fr_FR']; | ||
yield 'US English' => ['1,000', $parameter, 'en_US']; | ||
|
||
$parameter = new DecimalTranslatable(1000.01); | ||
|
||
yield 'Float in French' => ['1000,01', $parameter, 'fr_FR']; | ||
yield 'Float in US English' => ['1,000.01', $parameter, 'en_US']; | ||
|
||
$parameter = new DecimalTranslatable(1, \NumberFormatter::PERCENT); | ||
|
||
yield 'Styled in French' => ['100%', $parameter, 'fr_FR']; | ||
yield 'Styled in US English' => ['100%', $parameter, 'en_US']; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation\Tests\Translatable; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Translation\Translatable\MoneyTranslatable; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
class MoneyTranslatableTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
if (!\extension_loaded('intl')) { | ||
$this->markTestSkipped('Extension intl is required.'); | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider getValues() | ||
*/ | ||
public function testTrans(string $expected, MoneyTranslatable $parameter, string $locale) | ||
{ | ||
$translator = $this->createMock(TranslatorInterface::class); | ||
// DecimalMoneyFormatter output may contain non-breakable spaces: | ||
// - this is done for good reasons | ||
// - output "style" changes depending on the PHP version | ||
// This normalization in only done here in the test so a new PHP version won't break the test | ||
$normalized = str_replace(["\u{202f}", "\u{a0}"], ['', ''], $parameter->trans($translator, $locale)); | ||
$this->assertSame($expected, $normalized); | ||
} | ||
|
||
public function getValues(): iterable | ||
{ | ||
$parameterEuros = new MoneyTranslatable(1000, 'EUR'); | ||
$parameterDollars = new MoneyTranslatable(1000, 'USD'); | ||
|
||
yield 'Euros in French' => ['1000,00€', $parameterEuros, 'fr_FR']; | ||
yield 'Euros in US English' => ['€1,000.00', $parameterEuros, 'en_US']; | ||
yield 'US Dollars in French' => ['1000,00$US', $parameterDollars, 'fr_FR']; | ||
yield 'US Dollars in US English' => ['$1,000.00', $parameterDollars, 'en_US']; | ||
|
||
if (\defined('\NumberFormatter::CURRENCY_ACCOUNTING')) { | ||
$parameterEuros = new MoneyTranslatable(-1000, 'EUR', \NumberFormatter::CURRENCY_ACCOUNTING); | ||
yield 'Accounting style in French' => ['(1000,00€)', $parameterEuros, 'fr_FR']; | ||
yield 'Accounting style in US English' => ['(€1,000.00)', $parameterEuros, 'en_US']; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation\Translatable; | ||
|
||
use Symfony\Contracts\Translation\TranslatableInterface; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
/** | ||
* Wrapper around PHP IntlDateFormatter for date and time | ||
* The timezone from the DateTime instance is used instead of the server's timezone. | ||
* | ||
* Implementation of the ICU recommendation to first format advanced parameters before translation. | ||
* | ||
* @see https://unicode-org.github.io/icu/userguide/format_parse/messages/#format-the-parameters-separately-recommended | ||
* | ||
* @author Sylvain Fabre <syl.fabre@gmail.com> | ||
*/ | ||
class DateTimeTranslatable implements TranslatableInterface | ||
{ | ||
private \DateTimeInterface $dateTime; | ||
private int $dateType; | ||
private int $timeType; | ||
|
||
private static array $formatters = []; | ||
|
||
public function __construct( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest adding |
||
\DateTimeInterface $dateTime, | ||
int $dateType = \IntlDateFormatter::SHORT, | ||
int $timeType = \IntlDateFormatter::SHORT | ||
) { | ||
$this->dateTime = $dateTime; | ||
$this->dateType = $dateType; | ||
$this->timeType = $timeType; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use constructor promotion for all features targeting 6.1? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes (especially now that we target 7.1) |
||
|
||
public function trans(TranslatorInterface $translator, string $locale = null): string | ||
{ | ||
if (!$locale) { | ||
$locale = $translator->getLocale(); | ||
} | ||
|
||
$timezone = $this->dateTime->getTimezone(); | ||
$key = implode('.', [$locale, $this->dateType, $this->timeType, $timezone->getName()]); | ||
sylfabre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!isset(self::$formatters[$key])) { | ||
self::$formatters[$key] = new \IntlDateFormatter( | ||
$locale ?? $translator->getLocale(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the coalesce is dead code as the default is already applied in the variable a few lines above |
||
$this->dateType, | ||
$this->timeType, | ||
$timezone | ||
); | ||
} | ||
|
||
return self::$formatters[$key]->format($this->dateTime); | ||
} | ||
|
||
/** | ||
* Short-hand to only format a date. | ||
*/ | ||
public static function date(\DateTimeInterface $dateTime, int $type = \IntlDateFormatter::SHORT): self | ||
{ | ||
return new self($dateTime, $type, \IntlDateFormatter::NONE); | ||
} | ||
|
||
/** | ||
* Short-hand to only format a time. | ||
*/ | ||
public static function time(\DateTimeInterface $dateTime, int $type = \IntlDateFormatter::SHORT): self | ||
{ | ||
return new self($dateTime, \IntlDateFormatter::NONE, $type); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation\Translatable; | ||
|
||
use Symfony\Contracts\Translation\TranslatableInterface; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
/** | ||
* Wrapper around PHP NumberFormatter for decimal values. | ||
* | ||
* Implementation of the ICU recommendation to first format advanced parameters before translation. | ||
* | ||
* @see https://unicode-org.github.io/icu/userguide/format_parse/messages/#format-the-parameters-separately-recommended | ||
* | ||
* @author Sylvain Fabre <syl.fabre@gmail.com> | ||
*/ | ||
class DecimalTranslatable implements TranslatableInterface | ||
{ | ||
private float|int $value; | ||
private int $style; | ||
|
||
private static array $formatters = []; | ||
|
||
public function __construct(float|int $value, int $style = \NumberFormatter::DECIMAL) | ||
{ | ||
$this->value = $value; | ||
$this->style = $style; | ||
} | ||
|
||
public function trans(TranslatorInterface $translator, string $locale = null): string | ||
{ | ||
if (!$locale) { | ||
$locale = $translator->getLocale(); | ||
} | ||
|
||
$key = implode('.', [$locale, $this->style]); | ||
if (!isset(self::$formatters[$key])) { | ||
self::$formatters[$key] = new \NumberFormatter($locale, $this->style); | ||
} | ||
|
||
return self::$formatters[$key]->format($this->value); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation\Translatable; | ||
|
||
use Symfony\Contracts\Translation\TranslatableInterface; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
/** | ||
* Wrapper around PHP NumberFormatter for money | ||
* The provided currency is used instead of the locale's currency. | ||
* | ||
* Implementation of the ICU recommendation to first format advanced parameters before translation. | ||
* | ||
* @see https://unicode-org.github.io/icu/userguide/format_parse/messages/#format-the-parameters-separately-recommended | ||
* | ||
* @author Sylvain Fabre <syl.fabre@gmail.com> | ||
*/ | ||
class MoneyTranslatable implements TranslatableInterface | ||
{ | ||
private float|int $value; | ||
private string $currency; | ||
private int $style; | ||
|
||
private static array $formatters = []; | ||
|
||
public function __construct(float|int $value, string $currency, int $style = \NumberFormatter::CURRENCY) | ||
{ | ||
$this->value = $value; | ||
$this->currency = $currency; | ||
$this->style = $style; | ||
} | ||
|
||
public function trans(TranslatorInterface $translator, string $locale = null): string | ||
{ | ||
if (!$locale) { | ||
$locale = $translator->getLocale(); | ||
} | ||
|
||
$key = implode('.', [$locale, $this->style]); | ||
if (!isset(self::$formatters[$key])) { | ||
self::$formatters[$key] = new \NumberFormatter($locale, $this->style); | ||
} | ||
|
||
return self::$formatters[$key]->formatCurrency($this->value, $this->currency); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,8 @@ | |
"suggest": { | ||
"symfony/config": "", | ||
"symfony/yaml": "", | ||
"psr/log-implementation": "To use logging capability in translator" | ||
"psr/log-implementation": "To use logging capability in translator", | ||
"moneyphp/money": "To use money capability in translator" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We stopped using Thus, this suggestion is useless. If you don't already use this money library, there is no reason to suggest you to use it to use the MoneyTranslatable object (you would use its constructor directly). And if you already use the library, composer won't display the suggestion as it is already part of the project. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thus, your PR does not even contain the |
||
}, | ||
"autoload": { | ||
"files": [ "Resources/functions.php" ], | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After rebase, this should be moved to 6.4 section