t helper does not accept a hash object directly
See original GitHub issue- I am on the latest ember-intl version
- Not on the latest, but I believe this still applies to the latest version (looking at the code)
- I have searched the issues of this repo and believe that this is not a duplicate
Environment
- Ember Version: 3.20.5
- Ember CLI Version: 3.15.2
- Ember Intl Version: 5.5.0
- Browser(s): Any
- Node Version: 12.18.4
Steps to Reproduce
If I have a translation with multiple placeholders,
"myTranslation": "You can either {option1} or {option2}"
I need to pass them individually, like so:
{{t myTranslationPath option1='run' option2='hide'}}
It would be ideal if we could pass the options hash directly, like so:
{{t myTranslationPath optionsHash}}
where the optionsHash
is
{option1="run", option2="hide"}
However - because of how helpers work, any non-named arguments are passed as an array, and those are not picked up by the ‘t’ helper (just get an error saying that I did not pass the placeholders).
In the above example, it’s just two arguments - but imagine the case where we have variable translation strings, and thus variable arguments - it’s extra legwork that should not be necessary.
Not sure if this is a bug or considered an enhancement (or if there’s a good workaround that I am missing).
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (7 by maintainers)
Top GitHub Comments
Thanks a lot @jesdavpet! Your PR looks great. ✨
To answer your questions:
Great idea!
Ideally we could solve this using Spreadable Arguments (aka “splarguments”), however the RFC is not even finished yet.
I think extending
{{t}}
or rather the underlyingAbstractHelper
is safe, because it currently only accepts a singlevalue
:https://github.com/ember-intl/ember-intl/blob/d4c9d86aa809f9b8215e6cadad52278f8b5e7f89/addon/helpers/-format-base.js#L31-L43
We could change it to something like:
Then you could invoke it like so:
You can also combine it with named parameters and they override the ones from the
(hash)
, which I think is sensible.Because we’re changing this in the
AbstractHelper
, this would also affect / improve the other formatting helpers, which I think is positive as well.From my POV I would happily accept a PR, unless anyone steps forward with good objections.