React-native: [Packager] Requiring momentjs locale fails

Created on 15 Jun 2015  ·  10Comments  ·  Source: facebook/react-native

I'm trying to use a different locale for Moment.js, and I keep receiving the same error. This is the code:

var moment = moment();
moment.locale('es');

And I'm receiving this error in the Chrome Dev Tools console:

Error: Requiring unknown module "./locale/es". If you are sure the module is there, try restarting the packager.
 stack: 
  Object.ErrorUtils.applyWithGuard  index.ios.bundle:880
  require                           index.ios.bundle:195
  loadLocale                        index.ios.bundle:42796
  chooseLocale                      index.ios.bundle:42774
  locale_locales__getLocale         index.ios.bundle:42867
  Moment.locale                     index.ios.bundle:44506
  eval                              eval at evaluate …:1
 URL: undefined
 line: undefined

It doesn't seems a Moment.js error though.

Locked

Most helpful comment

@alvaromb - just a quick thought from browsing the moment docs, what about:

var moment = require('moment');
var esLocale = require('moment/locale/es');
moment.locale('es', esLocale);

All 10 comments

just do npm install moment
then
var moment = require('moment');

then you can do something like this

var datetime = moment(event.datetime).format('dddd, MMMM Do YYYY');

That’s what I did. I can use moment and I’ve been using it with react-native, but I cannot change the moment locale.

On 15 Jun 2015, at 15:49, Kelvin Jones [email protected] wrote:

just do npm install moment
then
var moment = require('moment');


Reply to this email directly or view it on GitHub https://github.com/facebook/react-native/issues/1629#issuecomment-112077489.

@alvaromb - just a quick thought from browsing the moment docs, what about:

var moment = require('moment');
var esLocale = require('moment/locale/es');
moment.locale('es', esLocale);

Also, see this: https://github.com/moment/moment/issues/2007#issuecomment-99811475

If it doesn't fix the problem, let me know and I'll reopen

Super @brentvatne!! I haven't found that option in the docs. Many thanks.

@brentvatne loading the locale directly i.e require('moment/locale/es'); works.
I'm trying to set the locale dynamically, AKA, I get the locale from user device and then load the specific locale.

const deviceLocale = React.NativeModules.RNI18n.locale;
// fails
require('moment/locale/' + deviceLocale); 

Is there other way to solve it besides:

switch deviceLocale:
case 'es':
  require('moment/locale/es');
case 'he':
  require('moment/locale/he');

It works. Thanks.

This is a bit old now but I'd like to show you how I've done it. I'm using react-native-i18n.

  • Create your language string files. (example ru.js)
  • Do the a locale check at the top of each language string file.

`import I18n from 'react-native-i18n';
if(I18n.locale === 'ru') {
require('moment/locale/ru');
}

export default {
HelloReact: "Здравствуйте, React
}`

Same question here: I would like to load dynamically depending on user/device but I dan't want to write a long serie of 'case'. @ranyefet solution still seem to be the only valid one... @JBerendes seems equivalent but even more work...
Anyone with a better solution? @brentvatne ?

I also have this issue: for some reason it doesn't seem to be possible to do a dynamic include based on the locale.

Was this page helpful?
0 / 5 - 0 ratings