Skip to content
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

Possibility to extend formatters? #1636

Closed
erictheswift opened this issue Jan 25, 2016 · 16 comments
Closed

Possibility to extend formatters? #1636

erictheswift opened this issue Jan 25, 2016 · 16 comments

Comments

@erictheswift
Copy link

Words passed as parameters should be capitalized/lowercased depending on translation.

We're using message formats as the only source of translations.
It would be great to extend built-in formatters select, plural with own ones, like lowercased, capitalized.

I see this change would also involve intl-messageformat-parser package.
Do you think it's a good idea?

@caridy
Copy link
Collaborator

caridy commented Jan 25, 2016

@metallium we are implementing the specified formatters from ICU Messages: http://userguide.icu-project.org/formatparse/messages.

I believe lowercase, and capitalization should be baked into the translation rather than a runtime cost. It might require some duplications in the messages you send to the client side, but I think it is worthy to keep that separation.

@erictheswift
Copy link
Author

These injected words are dynamic and are used in lots of messages. Thought about that, but it seems more natural to polish it in concrete translation.

@erictheswift
Copy link
Author

formatter customization is available by default in c# implementation
https://github.com/jeffijoe/messageformat.net/blob/master/src/Jeffijoe.MessageFormat/Formatting/FormatterLibrary.cs#L25

@erictheswift
Copy link
Author

@caridy you can still implement the specified formatters from ICU Messages, but make the set of formatters customizable.

@caridy
Copy link
Collaborator

caridy commented Jan 25, 2016

ah, thanks for the link @metallium. I will talk to the other folks about this, give us some time to review it. /cc @ericf

@ericf
Copy link
Collaborator

ericf commented Jan 26, 2016

@metallium can you write an example message in the message syntax to demonstrate what you're suggesting? I think it would help be better understand what you're running into and how you're proposing to solve it.

@erictheswift
Copy link
Author

example that is unsolveable now:

format('Add 1 {itemName}', {itemName: 'Bug'}) => 'Add 1 Bug'

data remains the same for all languages, but specific language can force lowercasing of words inside string (I still keep using English here)

format('Add 1 {itemName, lowercased}', {itemName: 'Bug'}) => 'Add 1 bug'

as for me, formatters seem to be good extensibility point, allowing to define

  • text/numbers transformation aliases
  • specific stuff like html wrappers around text parts (allowing to add specific behavior on message parts while preserving source string sanity)
  • ...

@ericf
Copy link
Collaborator

ericf commented Jan 27, 2016

@metallium I see, so you want to define lowercased() in your app code, pass it to IntlMessageFormat() as a custom format, and when used in a message it'll be called the locale and value?

@erictheswift
Copy link
Author

yep, to make it possible to provide custom formatters, e.g.

new IntlMessageFormat('The price is: {price, number, USD}', 'en-US', {
    lowercased: function(value) {
        return value.toLowerCase();
    }
})

in general it would be great if default formatters (plural, select, selectordinal, ...) would use the same API as custom ones

@rlivsey
Copy link

rlivsey commented Jan 28, 2016

Another potential use-case for this I've had in the past (#99) is to turn lists into sentences, it would be great to be able to define a sentence formatter:

format('Selected {items, sentence}', {items: ['one', 'two', 'three'}) 
// => "Selected one, two and three"

@caridy
Copy link
Collaborator

caridy commented Jan 28, 2016

@rlivsey that's a different feature request, and it is an ongoing effort: tc39/ecma402#33

@rlivsey
Copy link

rlivsey commented Jan 28, 2016

@caridy awesome, thanks for pointing me towards that, I've added a comment to the original issue pointing others over there too.

@nadavivry
Copy link

Pretty sure it's the same feature request - in my case, I have a requirement for adding a new "style" option for the number formatter.

I need to insert the following function as a custom style:

function (val) {
  var v = "";

  var negative = false;
  if (val < 0) {
    negative = true;
    val = Math.abs(val);
  }

  if (val >= 1e12) {
    v = String(Math.round(val / 1e11) / 10) + "T";
  } else if (val >= 1e9) {
    v = String(Math.round(val / 1e8) / 10) + "G";
  } else if (val >= 1e6) {
    v = String(Math.round(val / 1e5) / 10) + "M";
  } else if (val >= 1e3) {
    v = String(Math.round(val / 1e2) / 10) + "K";
  } else {
    v = String(Math.round(val * 10) / 10);
  }

  if (v == "undefined") {
    v = "";
  } else {
    if (negative) {
      v = "-" + v;
    }
  }

  return v;
}

@longlho longlho transferred this issue from formatjs/intl-messageformat May 22, 2019
longlho referenced this issue in formatjs/formatjs-old May 28, 2019
Improve browser bundling with Browserify/Webpack
longlho referenced this issue in formatjs/formatjs-old May 28, 2019
Improve browser bundling with Browserify/Webpack
@longlho longlho transferred this issue from formatjs/formatjs-old Apr 27, 2020
@longlho
Copy link
Member

longlho commented May 21, 2020

This use case should be handled by our skeleton support + numberformat es2020 polyfill

@longlho longlho closed this as completed May 21, 2020
@anosov-otg
Copy link

anosov-otg commented Mar 13, 2021

@longlho how can I specify currency dynamically using skeleton formatting?

No problem (from documentation):

The price of this bagel is {num, number, ::sign-always compact-short currency/GBP}

Problem: I don't know the currency at this point, it must be dynamic. This format doesn't work:

The price of this bagel is {num, number, ::sign-always compact-short currency/{currency}}

@longlho
Copy link
Member

longlho commented Mar 13, 2021

You cannot. You'll have to format it as a passed in values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants