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

Interpolation formatting does not support default format(s) #1384

Closed
mjbcopland opened this issue Jan 22, 2020 · 4 comments
Closed

Interpolation formatting does not support default format(s) #1384

mjbcopland opened this issue Jan 22, 2020 · 4 comments

Comments

@mjbcopland
Copy link
Contributor

mjbcopland commented Jan 22, 2020

The format argument of the formatter function is optional but must be supplied during interpolation in order for any custom formatting to take effect.

Consider the following example

i18next.init({
  lng: "en",
  interpolation: {
    escapeValue: false,
    format: function(value, format, lng) {
      return value.toLocaleString(lng);
    },
  }
});

console.log(
  i18next.t("Total {{amount}} since {{date}}", {
    amount: 123456.789,
    date: new Date()
  })
);
Total 123456.789 since Wed Jan 22 2020 17:03:40 GMT+0000 (Greenwich Mean Time)

I don't see any reason why the interpolated values should not be formatted, yet that is the case. We can force formatting by supplying an empty format (include the separator but no value), but this is unintuitive, easily missed, and only discoverable by inspecting the source.

console.log(
  i18next.t("Total {{amount,}} since {{date,}}", {
    amount: 123456.789,
    date: new Date()
  })
);
Total 123,456.789 since 1/22/2020, 17:03:40
@mjbcopland mjbcopland changed the title Interpolation formatting does not support default values Interpolation formatting does not support default format(s) Jan 22, 2020
@jamuhl
Copy link
Member

jamuhl commented Jan 22, 2020

And why should I expect any formatting happening with this:

i18next.t("Total {{amount}} since {{date}}", {
    amount: 123456.789,
    date: new Date()
  })

This says use the raw value (toString if needed)

i18next.t("Total {{amount, number}} since {{date, dateShort}}", {
    amount: 123456.789,
    date: new Date()
  })

says to format it and can be changed by translators as needed.

@mjbcopland
Copy link
Contributor Author

I guess my question is why are we using the raw value and/or toString when a custom formatter exists? I feel like my toLocaleString example above is a reasonable enough use-case.

@jamuhl
Copy link
Member

jamuhl commented Jan 23, 2020

You do not like to call functions if not needed to call them...just out of performance reason...

@jamuhl
Copy link
Member

jamuhl commented Jan 23, 2020

Can merge your PR - but really do not agree in the behaviour is / should be default anytime in future

@jamuhl jamuhl closed this as completed Mar 20, 2020
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

2 participants