Pluggable formatters
See original GitHub issueI propose we make the formatters that may be used in translations officially pluggable.
Motivation
Our API returns monetary values as objects that look like this:
interface Money {
value: number; // integer
currency: 'USD' | 'EUR';
e?: number; // default: -2
}
We have added this method to our IntlService
:
formatMoney(money: Money, options, ctx) {
const { value, currency, e = -2 } = money;
const normalizedValue *= 10 ** e;
return super.formatNumber(normalizedValue, {
format: currency,
...options,
}, ctx);
}
And also added an accompanying helper:
import BaseHelper from 'ember-intl/helpers/-format-base';
import Money from 'client/interfaces/money';
export default class FormatMoneyHelper extends BaseHelper {
format(value: Money, options): string {
return this.intl.formatMoney(value, options);
}
}
We represent monetary values like this for two reasons:
- to avoid floating point arithmetic imprecision when performing calculations with these values, as
0.1 + 0.2 !== 0.3
- to automatically deal with different currencies
This has been working great for us so far. But we can’t yet use this formatter in translations directly, like:
amountDue: 'Amount due: {due, money}'
To be able to use this formatter in translations directly, we would need to add a formatter to IntlService#_formatters
:
Proposal
We could of course continue monkey-patching our way through ember-intl, but I think that making the formatter system officially pluggable would be quite useful.
I think we should open up the formatter system officially, so that users / addons can add custom formatters.
Open Questions
- How does ember-intl / the message formatter find the formatters? Via the ember-resolver container maybe?
- What about template helpers? Should the addons just be responsible for providing such helpers?
- What about
IntlService#formatXXX
methods?
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
https://github.com/messageformat/messageformat/search?utf8=✓&q=rtl&type=
Ah I guess I was referring to this. Looks like it detects rtl locale. Happens via ‘biDiSupport’ config variable
Going to close this for now as it’s out of scope for this project.
I just finished updating the intl-messageformat compiler & parser. If and when we support this, it will need to happen upstream. Track this issue if you’re interested: https://github.com/formatjs/formatjs/issues/20