Localized date and number formatting
ide
Currently the JavaScript runtime on Android doesn't account for the user's locale when formatting dates and numbers. This affects methods like:
Date.prototype.toLocaleDateString()
With en-US this would print MM/DD/YYYY and with en-GB it would print DD/MM/YYYY, and also take time zones into account.
Number.prototype.toLocaleString()
With en-US it would print 1000 as 1,000 and with de-DE it would print 1.000.
We can include support for more locales in Expo for Android, but it will make the APK size about 2MB larger so we're looking to see what developers want.
A
Artem Vichurin
Would be nice to have!
Eugene Chybisov
Bump, this is still not available in managed expo workflow. Do you have any estimation?
Rufus Russell
For managed expo projects, it is not possible to format a date according to locale on Android. I am using the luxon library for dates. See the note for React Native at the bottom of this page. https://moment.github.io/luxon/docs/manual/install.html
Am I correct in thinking that this task is not possible without ejecting? Is there a way that correct locale formatting on Android can be provided through expo?
Daan van Klinken
Bump, this is still not available in Expo today. When can we expect this?
L
Luke Williams
You can read/write localized numbers in Expo with code like this:
Danny
Luke Williams: Thanks, this /almost/ works perfectly ... unfortunately if you set language to English but Location to Netherlands, expo-localization will give you "en-GB" as preferred language and while the iOS keyboard lets you only enter , as decimal seperator ... the decimal seperator is "detected" as .
L
Luke Williams
Danny: Interesting, we will check this out - that's the first I've heard of a setup that conflicts. Thanks for the info on your experience!
Danny
Luke Williams: more specifically: English (UK) set as preferred language, Netherlands as second language. Region Italy ... it's an edge case I know :D
Peter Murray
ide unless Im missing something this is tricky to do correctly in the expo environment. I have users reporting that they have setups like "en-RU" that show a numeric input keypad with a comma for decimal separator - and yet others with "en-AR" that show a decimal (both iOS). We need to know specifically what the phone is showing for thousands & decimal separator in order to properly parse these numbers. The https://github.com/fixd/react-native-locale module detects the settings for the thousand and decimal separators directly from native code and make those available.
Is this not something Expo could easily do (perhaps all of the react-native-locale stuff) to ensure we have high-fidelity visibility into what the phone thinks is the right formatting options?
ide
Peter Murray: I opened this feature request thinking about values that the JS runtime would print but we could also provide an API with more precise locale information like the one you linked.
Peter Murray
ide: To my mind, this would be great - we can let the phone decide what the separators are based on whatever logic or detailed settings it may expose. Rather than having us intuit how the phone treats different locales / languages.
We also need to parse numbers.
This kind of solution would allow fine grained locale number support without a huge database of locale settings in JS, no?
Patrick O'Meara
Peter Murray: Hi Peter, I am a maintainer of https://github.com/fixd/react-native-locale I've recently started using expo and I'm surprised that there isn't a solution for this.
I've looked into i18n, globalize and a few other solutions but none of them offer the
numberFromDecimalString
that react-native-locale
has.I'm going to reach out to the expo team and see what it takes to get
react-native-locale
integrated into the expo core.Ernesto R. C. Pereda
I usually use this for work with dates in javascript. It works great and also has support for I18n