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

Intl API for parsing locale aware strings #850

Closed
JeremiePat opened this issue Jan 8, 2024 · 2 comments
Closed

Intl API for parsing locale aware strings #850

JeremiePat opened this issue Jan 8, 2024 · 2 comments

Comments

@JeremiePat
Copy link

The Intl API is a fundamental API to format some values to a local aware string. Unfortunatly, it is missing an API to do the opposite operation : parsing strings to get a value. Currently, only Intl.segmenter is available to properly split out a string based on its locale format. It would be a huge enhancement to have some locale aware parser, for, at least, numbers and date.

Parsing locale aware strings is complicated and would benefits from a native support.

Date & Time

Currently, Date.parse and the future Temporal.*.from are not locale aware (and very likely will never be)

It would be a bliss to have somethind along this line:

const frenchDateParser = new Intl.DateTimeParser('fr') // very likely with some options when ambiguity needs to be handled.

const dateA = frenchDateParser.parse('12/01/2024') // Provide a Date or Temporal object for December 1st, 2024 (not January 12th, 2024)
const dateB = frenchDateParser.parse('Lundi 8 Janvier 2024') // Provide a Date or Temporal object for January 8th, 2024
const timeA = frenchDateParser.parse('Le 8 janvier 2024 à 17h') // Provide a Date or Temporal object for January 8th, 2024 at 5pm
const timeB = frenchDateParser.parse('18h34') // Provide a Date or Temporal object for the time 6:34pm

Some libraries offer some solutions:

Numbers

Currently JS doesn't have a native way to parse a locale formated number such as "10 345,56" (10345.56 in fr-fr locale) or "12,340" (12340 in en-us or 12.34 in fr-fr. Currencies are also tricky as some locales use the symbol as prefix ("$24.35") and others as suffix ("24,35 €")

Having a parser like that would be quite usefull:

const usCurrencyParser = new Intl.NumberParser('en-us', { style: "currency", currency: 'USD' }) // options should be very similar to the one used by NumberFormat

const numA = usCurrencyParser.parse('$24.40') // 24.4
const numB = usCurrencyParser.parse('34,67 €') // NaN ?

Some libraries offer some solutions:

  • Numeral (not locale aware but very versatile)
  • BrighSpacetUI (provide also parsing for date and time)
@sffc
Copy link
Contributor

sffc commented Jan 9, 2024

Hello! Please read issue #1 and #342 for why we don't support localized string parsing in Intl.

@sffc
Copy link
Contributor

sffc commented Jan 9, 2024

It would be a huge enhancement to have some locale aware parser, for, at least, numbers and date.

Please also read my blog post on why localized date parsing is probably the wrong solution to begin with: https://blog.sffc.xyz/post/190943794505/why-you-should-not-parse-localized-strings

If you can articulate an additional use case beyond what's listed above, I'm happy to hear and integrate it.

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