Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed

Conversation

@sylfabre
Copy link
Contributor

@sylfabresylfabre commentedMay 8, 2021
edited
Loading

QA
Branch?5.4
Bug fix?no
New feature?yes
Deprecations?no
TicketsFix#32652
LicenseMIT
Doc PRsymfony/symfony-docs#...

ICU recommends to first format advanced parameters before translation (https://unicode-org.github.io/icu/userguide/format_parse/messages/#format-the-parameters-separately-recommended).

Thanks to#41858,TranslatableMessage now supports recursiveTranslatableInterfaceas params.

This PR implements a few common use-cases for arguments that must be first formatted to improve DX:

  • DateTimeParameter to format date and time
  • MoneyParameter to format monetary values with the support ofhttps://github.com/moneyphp/money
  • DecimalParameter to format decimal values as per locale specs

The following implementations could be considered in later PRs:

  • CountryParameter to format the ISO 3166-2 into a localized country name
# Translation file app+intl-icu.en_US.ymlpayment:"{date, date, short} at {date, time, short} - {value, number, currency}"payment2:"{date} at {time} - {value}"# Translation file app+intl-icu.fr_FR.ymlpayment:"{date, date, short} at {date, time, short} - {value, number, currency}"payment2:"{date} at {time} - {value}"

Before this PR

// BASIC USAGE// en_US: 1/25/19 at 7:30 PM - $100.00// fr_FR: 25/01/2019 à 19:30 - 100,00 €$this->translator->trans('payment', ['date' =>new \DateTime('2019-01-25 11:30:00','America/Los_Angeles'),'value' =>100]);// en_US: 1/25/19 at 10:30 AM - $100.00// fr_FR: 25/01/2019 à 10:30 - 100,00 €$this->translator->trans('payment', ['date' =>new \DateTime('2019-01-25 11:30:00','Europe/Paris'),'value' =>100]);// ADVANCED USAGE// en_US: 1/25/19 at 11:30 AM - $100.00// fr_FR: 25/01/2019 à 11:30 - 100,00 $$userCurrency ='USD';$datetime =new \DateTime('2019-01-25 11:30:00','Europe/Paris');$dateFormatter =newIntlDateFormatter($this->translator->getLocale(), IntlDateFormatter::SHORT, IntlDateFormatter::NONE,$datetime->getTimezone());$timeFormatter =newIntlDateFormatter($this->translator->getLocale(), IntlDateFormatter::NONE, IntlDateFormatter::SHORT,$datetime->getTimezone());$numberFormatter =newNumberFormatter($this->translator->getLocale(), NumberFormatter::CURRENCY);$this->translator->trans('payment2', ['date' =>$dateFormatter->format($datetime),'time' =>$timeFormatter->format($datetime),'value' =>$numberFormatter->formatCurrency(100,$userCurrency)]);

After this PR

// BASIC USAGE// No change// ADVANCED USAGE// en_US: 1/25/19 at 11:30 AM - $100.00// fr_FR: 25/01/2019 à 11:30 - 100,00 $$datetime =new \DateTime('2019-01-25 11:30:00','Europe/Paris');$this->translator->trans('payment2', ['date' => DateTimeTranslatable::date($datetime),'time' => DateTimeTranslatable::time($datetime),'value' =>newMoneyTranslatable(100,'USD'),'value' => MoneyTranslatable::fromMoney(Money::USD(10000)),// Alternative with money-php]);// Or with TranslatableMessage for TwignewTranslatableMessage('payment2', ['date' => DateTimeTranslatable::date($datetime),'time' => DateTimeTranslatable::time($datetime),'value' =>newMoneyTranslatable(100,'USD'),'value' => MoneyTranslatable::fromMoney(Money::USD(10000)),// Alternative with money-php]);

TODO

  • Fix tests
  • AddDecimalParameter
  • Documentation PR

sstok, kylekatarnls, DocFX, 94noni, and OskarStark reacted with thumbs up emoji
@derrabus
Copy link
Member

Please give your PR a meaningful title.

OskarStark and Nyholm reacted with thumbs up emojisylfabre reacted with eyes emoji

@sylfabresylfabre changed the title[Translation] [wip][Translation] [wip] Advanced localization with custom currency and timezoneMay 8, 2021
@sylfabresylfabreforce-pushed thetranslate_date_time_currency branch from08f1df0 to4cde975CompareMay 19, 2021 16:09
ro0NL

This comment was marked as resolved.

@sylfabresylfabreforce-pushed thetranslate_date_time_currency branch 2 times, most recently from60985be tocdc168fCompareMay 30, 2021 19:17
@sylfabresylfabre changed the title[Translation] [wip] Advanced localization with custom currency and timezone[Translation] [wip] Separate parameters formattingMay 30, 2021
@sylfabresylfabreforce-pushed thetranslate_date_time_currency branch 8 times, most recently from539ce31 tof274351CompareMay 30, 2021 20:41
@sylfabre
Copy link
ContributorAuthor

@ro0NL I've actually created another interface:ParameterInterface as my work resolves around parameters formatting instead of translation. Feel free to have a look :)

sstok reacted with rocket emoji

@sylfabresylfabre changed the title[Translation] [wip] Separate parameters formatting[Translation] Separate parameters formattingMay 30, 2021
@sylfabresylfabreforce-pushed thetranslate_date_time_currency branch 4 times, most recently from9b1506d toed8eae3CompareJune 1, 2021 08:04
@sylfabresylfabreforce-pushed thetranslate_date_time_currency branch 3 times, most recently from0ce48cf tocc02f6aCompareDecember 6, 2021 08:45
@sylfabre
Copy link
ContributorAuthor

Thank you@stof for the review, I've fixed my PR

@sylfabresylfabreforce-pushed thetranslate_date_time_currency branch 2 times, most recently from0f0101c tob642d18CompareDecember 6, 2021 09:20
@sylfabresylfabreforce-pushed thetranslate_date_time_currency branch fromb642d18 todab3f2aCompareDecember 6, 2021 09:35
$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)

welcoMattic reacted with thumbs up emoji
fabpot added a commit that referenced this pull requestDec 17, 2021
This PR was merged into the 6.1 branch.Discussion----------[Translation] Translatable parameters| Q             | A| ------------- | ---| Branch?       | 6.1| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Tickets       | N/A| License       | MIT| Doc PR        | TBD✅  The TwigExtension now supports messages implementing `TranslatableInterface` (https://symfony.com/blog/new-in-symfony-5-2-translatable-objects).✅ Thanks to#41858, `TranslatableMessage` now supports recursive `TranslatableInterface` as params.❌ But using `TranslatableInterface` as params with regular messages is not supported yet.💡 This PR addresses this issue and makes it possible to create dedicated `TranslatableInterface` implementations like the one from#41136_Note: I would like also to add `TranslatableInterface` to the `trans(?string $id)` signature method like with the one from the [TwigExtension](https://github.com/symfony/symfony/blob/6.1/src/Symfony/Bridge/Twig/Extension/TranslationExtension.php#L111) but I don't know how to do it without breaking BC. Any advice is welcome!_Commits-------5beaee8 [Translation] Translatable parameters
symfony-splitter pushed a commit to symfony/translation that referenced this pull requestDec 17, 2021
This PR was merged into the 6.1 branch.Discussion----------[Translation] Translatable parameters| Q             | A| ------------- | ---| Branch?       | 6.1| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Tickets       | N/A| License       | MIT| Doc PR        | TBD✅  The TwigExtension now supports messages implementing `TranslatableInterface` (https://symfony.com/blog/new-in-symfony-5-2-translatable-objects).✅ Thanks to #41858, `TranslatableMessage` now supports recursive `TranslatableInterface` as params.❌ But using `TranslatableInterface` as params with regular messages is not supported yet.💡 This PR addresses this issue and makes it possible to create dedicated `TranslatableInterface` implementations like the one fromsymfony/symfony#41136_Note: I would like also to add `TranslatableInterface` to the `trans(?string $id)` signature method like with the one from the [TwigExtension](https://github.com/symfony/symfony/blob/6.1/src/Symfony/Bridge/Twig/Extension/TranslationExtension.php#L111) but I don't know how to do it without breaking BC. Any advice is welcome!_Commits-------5beaee85f9 [Translation] Translatable parameters
@fabpotfabpot modified the milestones:6.1,6.2May 20, 2022
SerhiyMytrovtsiy added a commit to SerhiyMytrovtsiy/translation that referenced this pull requestOct 19, 2022
This PR was merged into the 6.1 branch.Discussion----------[Translation] Translatable parameters| Q             | A| ------------- | ---| Branch?       | 6.1| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Tickets       | N/A| License       | MIT| Doc PR        | TBD✅  The TwigExtension now supports messages implementing `TranslatableInterface` (https://symfony.com/blog/new-in-symfony-5-2-translatable-objects).✅ Thanks to #41858, `TranslatableMessage` now supports recursive `TranslatableInterface` as params.❌ But using `TranslatableInterface` as params with regular messages is not supported yet.💡 This PR addresses this issue and makes it possible to create dedicated `TranslatableInterface` implementations like the one fromsymfony/symfony#41136_Note: I would like also to add `TranslatableInterface` to the `trans(?string $id)` signature method like with the one from the [TwigExtension](https://github.com/symfony/symfony/blob/6.1/src/Symfony/Bridge/Twig/Extension/TranslationExtension.php#L111) but I don't know how to do it without breaking BC. Any advice is welcome!_Commits-------5beaee85f9 [Translation] Translatable parameters
@nicolas-grekasnicolas-grekas modified the milestones:6.2,6.3Nov 5, 2022
@nicolas-grekasnicolas-grekas modified the milestones:6.3,6.4May 23, 2023
Comment on lines +7 to +8
* Add new implementations of`TranslatableInterface` to format parameters
separately as recommended per the ICU for DateTime, money, and decimal values.
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

@welcoMattic
Copy link
Member

@sylfabre what is the status of this PR? There are some not addressed comments and the branch needs to be rebased, but I think we are close to be able to merge it.

@OskarStark
Copy link
Contributor

friendly ping@sylfabre 👋

@sylfabre
Copy link
ContributorAuthor

@welcoMattic@OskarStark Sorry for the late answer: the back-to-school period was challenging on my side 🙈
I need to find some time to get back to work on this one. 😊

Before that:@stof@nicolas-grekas does this suggestion/PR make sense to you? Is working on it worth it?

@nicolas-grekas
Copy link
Member

No objections on my side, let's resume the PR for 7.1.
I guess this can be useful in twig templates also, isn't it?

welcoMattic and sstok reacted with thumbs up emoji

$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


privatestaticarray$formatters = [];

publicfunction__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

$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.

yes (especially now that we target 7.1)

welcoMattic reacted with thumbs up emoji
"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 usingsuggest 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 thefromMoney static method described in the PR description.

@nicolas-grekasnicolas-grekas modified the milestones:6.4,7.1Nov 15, 2023
@fabpot
Copy link
Member

Closing as there is no more activity. Feel to reopen when you have time.

@fabpotfabpot closed thisFeb 4, 2024
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@stofstofstof left review comments

@welcoMatticwelcoMatticwelcoMattic left review comments

@NyholmNyholmAwaiting requested review from Nyholm

+3 more reviewers

@sstoksstoksstok left review comments

@ro0NLro0NLro0NL left review comments

@kylekatarnlskylekatarnlskylekatarnls approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

7.1

Development

Successfully merging this pull request may close these issues.

[Translator] Why does the currency depend on the locale?

12 participants

@sylfabre@derrabus@Nyholm@kylekatarnls@welcoMattic@OskarStark@nicolas-grekas@fabpot@stof@sstok@ro0NL@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp