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

[Packager] Requiring momentjs locale fails #1629

Closed
alvaromb opened this Issue Jun 15, 2015 · 10 comments

Comments

Projects
None yet
9 participants
@alvaromb
Copy link
Contributor

alvaromb commented Jun 15, 2015

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.

@cyprusglobe

This comment has been minimized.

Copy link

cyprusglobe commented Jun 15, 2015

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');

@alvaromb

This comment has been minimized.

Copy link
Contributor

alvaromb commented Jun 15, 2015

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 notifications@github.com wrote:

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


Reply to this email directly or view it on GitHub #1629 (comment).

@brentvatne

This comment has been minimized.

Copy link
Collaborator

brentvatne commented Jun 15, 2015

@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);

@brentvatne brentvatne changed the title Requiring momentjs locale fails [Packager] Requiring momentjs locale fails Jun 15, 2015

@brentvatne

This comment has been minimized.

Copy link
Collaborator

brentvatne commented Jun 15, 2015

Also, see this: moment/moment#2007 (comment)

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

@brentvatne brentvatne closed this Jun 15, 2015

@alvaromb

This comment has been minimized.

Copy link
Contributor

alvaromb commented Jun 16, 2015

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

@ranyefet

This comment has been minimized.

Copy link

ranyefet commented Dec 8, 2015

@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');
@anderson916

This comment has been minimized.

Copy link

anderson916 commented Feb 4, 2016

It works. Thanks.

@JBerendes

This comment has been minimized.

Copy link

JBerendes commented Sep 13, 2016

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
}`

@bourgois

This comment has been minimized.

Copy link

bourgois commented Oct 3, 2016

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 ?

@jdmunro

This comment has been minimized.

Copy link

jdmunro commented Nov 6, 2016

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

weihanglo added a commit to weihanglo/pycontw-mobile that referenced this issue Jun 5, 2017

fix: modify import logic of moment locale
It seems that this is and old and unsolved issue. The only solution is
import all locale explicitly.

Check out facebook/react-native#1629

@facebook facebook locked as resolved and limited conversation to collaborators Jun 15, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.