Skip to content

[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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

6.1
---

* Add new implementations of `TranslatableInterface` to format parameters
separately as recommended per the ICU for DateTime, money, and decimal values.
Comment on lines +7 to +8
Copy link
Member

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


5.4
---

Expand Down
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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest adding @param \IntlDateFormatter::* $dateType (and same for the other parameters in this constructor and static factory methods) so that static analysis tool can report invalid values

\DateTimeInterface $dateTime,
int $dateType = \IntlDateFormatter::SHORT,
int $timeType = \IntlDateFormatter::SHORT
) {
$this->dateTime = $dateTime;
$this->dateType = $dateType;
$this->timeType = $timeType;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use constructor promotion for all features targeting 6.1?

Copy link
Member

Choose a reason for hiding this comment

The 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()]);
if (!isset(self::$formatters[$key])) {
self::$formatters[$key] = new \IntlDateFormatter(
$locale ?? $translator->getLocale(),
Copy link
Member

Choose a reason for hiding this comment

The 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);
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Component/Translation/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We stopped using suggest in the composer packages.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thus, your PR does not even contain the fromMoney static method described in the PR description.

},
"autoload": {
"files": [ "Resources/functions.php" ],
Expand Down