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
How to change locale in momentJS in React Native? #4422
Comments
Confirming that, |
does the trick!
results in |
If you don't want to use |
@Leeds-eBooks Could you explain how you import moment? I rolled back to 2.18 and yet I need to import moment from moment-with-locales in to get local to work properly in react-native Thanks |
@msevestre Yeah of course, although I think my solution may be out of date now, I made it work in my code and moved on, I think a later version of moment might have fixed it. Nevertheless, my fix is:
import moment from 'moment'
import 'moment/src/locale/en-gb'
import 'moment/src/locale/es'
import 'moment/src/locale/fr'
import 'moment/src/locale/pl'
import 'moment/src/locale/pt'
// ...etc
// bonus trick!
import DeviceInfo from 'react-native-device-info'
const deviceLocale = DeviceInfo.getDeviceLocale()
moment.locale([deviceLocale, 'en']) This is what I'm doing, and it's working for me, but as I say it may no longer be necessary. |
@Leeds-eBooks Awesome. Thanks for your quick feedback. Really helped |
@VanessaChu - do you still have a problem? |
This solution works for me |
Hey peoples, |
I hope most discussion participants got their problems sorted out :) Thank you @Leeds-eBooks For 3.x we might have more versions of moment with/without the dynamic import/es6 so this is less of an issue. |
Hello @MayeuldP , |
I'm using v2.18 and this method works.
|
Hi, @KashifAhmed In v.2.22.2 you should use moment.updateLocale('fr',momentFR)
|
Check this:
This include all languajes configurations from moment, it's js, so no problem with size. I had problem before when some one install the app with an mobile with different languaje. From this you can set any locale defined by moment without any problem,
Or using device locale without worries about it is out of your languajes specs as example from @benadamstyles |
In my case, import the locale wasn't enough, so I used the imported dictionary and remove the first character that is an underscore import moment from 'moment';
import momentPTBR from 'moment/src/locale/pt-br';
function prepareLocale(locale) {
for (const key in locale) {
if (locale.hasOwnProperty(key)) {
// remove first character underscore fom key, i.e. '_calendar' => 'calendar'
locale[key.substring(1)] = locale[key];
}
}
return locale;
}
moment.defineLocale('pt-br', prepareLocale(momentPTBR)); |
"dependencies": {
"expo": "32.0.0",
"moment": "2.24.0",
"react": "16.5.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz"
}, In the upper-most file in the tree, here's how my code looks: import moment from 'moment'
import 'moment/locale/en-gb'
const momentLocale = moment.locale('en-gb')
if (momentLocale !== 'en-gb') {
throw new Error(`Moment fell back to locale ${momentLocale}`)
}
export default () => null |
could you please give some examples. |
I changed my version from 2.24 to 2.18 and it works now. There is a bug somewhere after 2.20. |
its work for me
|
Make sure you're checking that what you get back from |
I don't understand how this is working for you guys. In other words |
Set it in your app root, not in a view. Mine is above the call to |
This is my
"moment": "^2.29.1", Works on the first view and when I navigate away it stops working. If I refresh the simulator it works on all the app. But on my physical device, it just works only in the first view like mentioned above. This is so frustrating. |
Opened the project I've done this on to check, and I don't have an index. I let react-native take care of whatever it wants to in there, and set my locale in What's the advantage of hand-rolling your own index? Is the react-native provided one doing something yours isn't? |
this is what you look for:
|
MUITO OBRIGADO MESMO <3 Voltei para a versão 2.18.0, importei dessa forma:
FUNCIONOU!!!!! |
I use v2.29.4
and IT'S WORK!! |
I use version v2.29.4
|
This is what I have but it does not work.
console log outputs: en, en, en
expected console log output: en, zh-cn, zh-cn
When I tried changing import moment from 'moment' to import moment from 'moment/min/moment-with-locales' but it throws an error on this line:
const total = moment.duration(this.props.stoveUsage.total, 'seconds').format('H:mm', { trim: false })
error: momentWithLocales2.default.duration(...).format is not a function
The text was updated successfully, but these errors were encountered: