Skip to content

Add a MessageFormatter Phrase renderer #22996

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

Merged

Conversation

navarr
Copy link
Member

@navarr navarr commented May 23, 2019

This gives us the ability to use ICU MessageFormatter formatting strings (inc. placeholders) for better support of internationalization.

MessageFormatter provides this by essentially giving a massive amount of flexibility to translatable strings, for example, consider a phrase describing how many orders there are. In English, you might create:

  • There are no orders.
  • There is 1 order.
  • There are x orders.

Or maybe

  • There are x order(s).

These work fine for english, but the first one creates three phrases for the same string (and falls apart for languages such as Arabic and Ukranian) - and the second one falls apart in foreign languages.

With MessageFormatter:

  • There are {orders, plural, =0 {no orders} =1 {1 order} other {# orders}}

This can then be mapped however necessary in foreign languages. Japanese for example:

  • 注文が{orders, plural, =0 {ない} other {#つある}}

This is just a small, quick example of the flexibility MessageFormatter provides over the current system. Because of this form of processing, it also adds support for plurals and ordinal selection. It also provides support for locale based date and currency formatting, but Magento's built-ins may be preferred for such functionality.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

@m2-assistant
Copy link

m2-assistant bot commented May 23, 2019

Hi @navarr. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento-engcom-team give me test instance - deploy test instance based on PR changes
  • @magento-engcom-team give me 2.3-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Assistant documentation

This gives us the ability to use ICU MessageFormatter formatting strings
(inc. placeholders) for better support of internationalization.
@navarr navarr closed this May 23, 2019
@navarr navarr reopened this May 23, 2019
@navarr
Copy link
Member Author

navarr commented May 23, 2019

Naive performance testing results (testing only the cost of the formatting in different scenarios - data for the comment)

Cost for non-MessageFormatter strings:

Text: This is a translatable string
Arguments: Array()

MessageFormatter: 2.4036829471588 Avg: 2.4036829471588E-6
strpos: 0.55331492424011 Avg: 5.5331492424011E-7
Speed Difference (over 1000000 iterations): 4.3441498536478 or 0.23019463731443

Cost for MessageFormatter strings:

Text: This is {,plural,=1 {a translatable string} other {some translatable strings}}
Arguments: Array
(
    [0] => 1
)

MessageFormatter: 2.2827291488647 Avg: 2.2827291488647E-6
strpos: 3.03768491745 Avg: 3.03768491745E-6
Speed Difference (over 1000000 iterations): 0.75147002105177 or 1.3307250748345

"Speed Difference" is MessageFormatter÷strpos or strpos÷MessageFormatter

All costs are per 1,000,000 operations. Naive code at: https://gist.github.com/navarr/e4c2b69b0d0fd3ca40b764e771640d11

@magento magento deleted a comment from m2-assistant bot May 23, 2019
@navarr navarr force-pushed the add-messageFormat-translator branch from ffba68e to 7f88d91 Compare May 23, 2019 18:41
@ihor-sviziev ihor-sviziev self-assigned this May 24, 2019
Copy link
Contributor

@ihor-sviziev ihor-sviziev left a comment

Choose a reason for hiding this comment

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

Hi @navarr,
Thank you for contribution! Really good job!

Could you review comments from me and from @joni-jones?

* Declares strict types properly
* @inheritdoc -> @inheritdoc
* Simplifies FQNs
@navarr navarr requested a review from ihor-sviziev May 24, 2019 17:54
@navarr
Copy link
Member Author

navarr commented May 24, 2019

I've performed the changes as requested 👍

@magento-engcom-team
Copy link
Contributor

Hi @ihor-sviziev, thank you for the review.
ENGCOM-5167 has been created to process this Pull Request

@sdzhepa
Copy link
Contributor

sdzhepa commented Jul 10, 2019

Hello @navarr
Thank you for your contribution and collaboration!

Unfortunately, we are stuck on the testing stage for this PR.
From the code perspective, it looks very good and useful.
And now we need to test it before merging these changes into develop branch.

So, could you please help us with user scenarios/steps and expected result for manual testing?

Thank you in andvance

@ihor-sviziev
Copy link
Contributor

ihor-sviziev commented Jul 17, 2019

Hi @sdzhepa,

In code we can see examples:
https://github.com/magento/magento2/pull/22996/files#diff-a27e2b7497834215b438835c65ccbf07R45

Even in the issue description here is Japanese example:

注文が{orders, plural, =0 {ない} other {#つある}}

For testing you should use this Japanese locale in your magento store, add ja_JP translation to csv file like this:

"There are {orders, plural, =0 {no orders} =1 {1 order} other {# orders}}", "注文が{orders, plural, =0 {ない} other {#つある}}"

And try to execute following translation with code:

echo __("There are {orders, plural, =0 {no orders} =1 {1 order} other {# orders}}", ['orders' => 0]);
echo __("There are {orders, plural, =0 {no orders} =1 {1 order} other {# orders}}", ['orders' => 1]);
echo __("There are {orders, plural, =0 {no orders} =1 {1 order} other {# orders}}", ['orders' => 3]);
echo __("There are {orders, plural, =0 {no orders} =1 {1 order} other {# orders}}", ['orders' => 15]);

IMO we should add few translated phrases to the tests, but looks like it will be really not so easy

@engcom-Delta engcom-Delta self-assigned this Jul 18, 2019
@engcom-Delta
Copy link
Contributor

@ihor-sviziev Thanks for explanation

@engcom-Delta
Copy link
Contributor

✔️ QA passed

@ihor-sviziev
Copy link
Contributor

Hi @sivaschenko,
Could you add some info why this PR still wasn't merged?

@magento-engcom-team magento-engcom-team merged commit d9e4113 into magento:2.3-develop Sep 5, 2019
@m2-assistant
Copy link

m2-assistant bot commented Sep 5, 2019

Hi @navarr, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

@magento-engcom-team magento-engcom-team added this to the Release: 2.3.4 milestone Sep 5, 2019
@navarr navarr deleted the add-messageFormat-translator branch September 6, 2019 13:47
@sidolov sidolov added the Auto-Tests: Covered All changes in Pull Request is covered by auto-tests label Sep 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.