Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Translation] allow using the ICU message format using domains with the "+intl-icu" suffix#28952
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
5c0f147 toa4fb360Comparejvasseur commentedOct 22, 2018
There is one downside to this compared to the |
nicolas-grekas commentedOct 22, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
|
a4fb360 to6e544b4Comparejaviereguiluz commentedOct 23, 2018
The current proposal (adding |
6e544b4 to1e9b58dComparenicolas-grekas commentedOct 23, 2018
nothing I can think of: a |
stof commentedOct 23, 2018
This requires updating all places using the translator to use an explicit domain though, making the migrating painful. |
stof commentedOct 23, 2018
the question of course would be: what is the way for loaders to decide whether the resource they load is using the Intl-ICU format or the Symfony one ? |
stof commentedOct 23, 2018
hmm, actually, looking at the implementation, the |
nicolas-grekas commentedOct 23, 2018
Yes! Sorry if that wasn't clear. I updated the PR description. |
Uh oh!
There was an error while loading.Please reload this page.
| thrownewLogicException('Cannot parse message translation: please install the "intl" PHP extension or the "symfony/polyfill-intl-messageformatter" package.'); | ||
| } | ||
| try { | ||
| $this->cache[$locale][$message] =$formatter =new \MessageFormatter($locale,$message); |
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.
should we actually memoize all the formatters ? This increases memory usage for each message being formatted. In my experience, a page is much more likely to translate lots of different messages than lots of messages being the same.
What is the benefit in terms of speed when skipping the instantiation the second time (when using the intl extension. I don't care about the performance of the polyfill here, as the way to improve performance in such case would be to install ext-intl). Does it actually justify such memory leak ?
nicolas-grekasOct 24, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
I benched thiswith the extension:\MessageFormatter::formatMessage('Hello {name}', 'en', array('name' => 'Bob'));
vs$formatter->format(array('name' => 'Bob'));
the later is 3 times faster than the former and each $formatter object takes 226 bytes.
With that in mind, I think it's worth doing it: for run-once requests we don't care, and for long-running ones, the benefit is worth it IMHO.
| foreach ($parametersas$key =>$value) { | ||
| if ('%' === ($key[0] ??null)) { | ||
| unset($parameters[$key]); | ||
| $parameters[trim($key,'%')] =$value; |
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.
should we also support the{{ name }} convention, which is used in the Validator component for instance ?
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.
good idead, updated
src/Symfony/Component/Translation/Formatter/IntlMessageFormatter.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
f221f4a to326dd08Compare…he "+intl-icu" suffix
326dd08 tod95cc4dCompare| $this->assertInstanceof(IntlFormatterInterface::class,$formatter); | ||
| $this->assertSame('Hello Fab',$formatter->formatIntl('Hello {name}','en',array('name' =>'Fab'))); | ||
| $this->assertSame('Hello Fab',$formatter->formatIntl('Hello {name}','en',array('%name%' =>'Fab'))); | ||
| $this->assertSame('Hello Fab',$formatter->formatIntl('Hello {name}','en',array('{{ name }}' =>'Fab'))); |
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.
Nice
| if ($catalogue->defines($id,$domain)) { | ||
| break; | ||
| } | ||
| if ($cat =$catalogue->getFallbackCatalogue()) { |
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.
Are we really sure this always will returnnull eventually?
nicolas-grekasOct 26, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
I suppose: this is the exact same logic as in transChoice. Not having it here was a bug actually (with no side effects since trans wasn't locale sensitive before)
nicolas-grekas commentedOct 28, 2018
Ping @symfony/deciders |
fabpot commentedOct 28, 2018
Thank you@nicolas-grekas. |
… domains with the "+intl-icu" suffix (nicolas-grekas)This PR was merged into the 4.2-dev branch.Discussion----------[Translation] allow using the ICU message format using domains with the "+intl-icu" suffix| Q | A| ------------- | ---| Branch? | 4.2| Bug fix? | no| New feature? | yes| BC breaks? | no (unless merged after 4.2)| Deprecations? | no| Tests pass? | yes| Fixed tickets | -| License | MIT| Doc PR | -This PR replaces the `FallbackFormatter` logic by explicit opt-in.This allows triggering accurate exceptions when one wants to use the ICU message format but the extension (or the polyfill) is missing.The way to opt-in is to put messages in a domain with the `+intl-icu` suffix *at loading/declaration time*.E.g. translations in `messages+intl-icu.en.yaml` will be read as part of the `messages` domain , but will be parsed by intl's `MessageFormatter` instead of the default Symfony one.To make it seamless to adopt the ICU format, `%`s are trimmed from parameter keys (idea borrowed fromhttps://github.com/webfactory/WebfactoryIcuTranslationBundle)This is for 4.2 as it changes a feature that is not released yet.ping@Nyholm - we drafted this together.Commits-------d95cc4d [Translation] allow using the ICU message format using domains with the "+intl-icu" suffix
Uh oh!
There was an error while loading.Please reload this page.
This PR replaces the
FallbackFormatterlogic by explicit opt-in.This allows triggering accurate exceptions when one wants to use the ICU message format but the extension (or the polyfill) is missing.
The way to opt-in is to put messages in a domain with the
+intl-icusuffixat loading/declaration time.E.g. translations in
messages+intl-icu.en.yamlwill be read as part of themessagesdomain , but will be parsed by intl'sMessageFormatterinstead of the default Symfony one.To make it seamless to adopt the ICU format,
%s are trimmed from parameter keys (idea borrowed fromhttps://github.com/webfactory/WebfactoryIcuTranslationBundle)This is for 4.2 as it changes a feature that is not released yet.
ping@Nyholm - we drafted this together.