question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pluggable formatters

See original GitHub issue

I 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:

https://github.com/ember-intl/ember-intl/blob/4ab18a4524c2f42c57fa4533cbffdce2dea9a1d1/addon/services/intl.js#L107-L113

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:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
snewcomercommented, Mar 18, 2019

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

0reactions
jasonmitcommented, Mar 25, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using the Pluggable Formatters - Oracle Help Center
Learn how to use the pluggable formatters to convert operations from the Oracle GoldenGate trail file into formatted messages that you can send...
Read more >
Formatters - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
GoldenGate for Big Data Pluggable Formatters, Avro OCF - BPI ...
GoldenGate for Big Data "Pluggable Formatters" are great tools for formatting various message types. This blog contains details about Avro OCF Formatter.
Read more >
Pluggable custom formatters · Issue #87 - GitHub
Hi, I'm interested in this. In my company we have our own version of black https://github.com/globality-corp/globality-black and I would ...
Read more >
US Patent for Pluggable formatters Patent (Patent # 7,120,639 ...
A system and method is provided for facilitating employment of pluggable formatters by providing a variety of functions or services outside the formatter ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found