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

[Packager] Requiring momentjs locale fails #1629

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

[Packager] Requiring momentjs locale fails #1629

alvaromb opened this issue Jun 15, 2015 · 10 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@alvaromb
Copy link
Contributor

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
Copy link

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
Copy link
Contributor Author

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
Copy link
Collaborator

@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
Copy link
Collaborator

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

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

@alvaromb
Copy link
Contributor Author

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

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

@john916zhang
Copy link

It works. Thanks.

@JBerendes
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
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
Copy link
Contributor

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
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
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

9 participants