Moment: locale changes does'nt work

Created on 15 Feb 2016  ·  22Comments  ·  Source: moment/moment

When I try to change the locale value, nothing happens, not even error.

      this.$log.log(moment.locale()); // returns 'en'
      this.$log.log(moment.locale('fr')); // set to french but returns 'en'
      this.$log.log(moment.locale()); // of course, still returns 'en' instead of 'fr'

I also tried from the Chrome developer tools console:

> moment.locale('fr')
-> "en"
Troubleshooting

Most helpful comment

I added:
import 'moment/locale/fr';

and using:
moment.locale('fr')

now sets properly the locale to french.
It is not dynamic but it will do the job for now.

I can close it I think and thanks for all people who answered.
Hope this issue will help people using TypeScript/Angular/Jspm.

All 22 comments

Are you using a module loader that may be giving you a different instance of moment?

import m from 'moment';

this.$log.log(m.locale());
this.$log.log(m.locale('fr'));
this.$log.log(m.locale());

this.$log.log(window.moment.locale());
this.$log.log(window.moment.locale('is'));
this.$log.log(window.moment.locale());

I'm in an angular project, modules are handles with JSPM.
I don't understand what you mean by " that may be giving you a different instance of moment"

The 3 lines are real code I tried, using the same moment in the same method.
I have the same result if I use a specific variable.

import * as moment from 'moment';

export function registerDateFormatService(appModule: angular.IModule) {
   appModule.service('dateFormatService', DateFormatService);
}

export class DateFormatService {
   constructor(private $log: ng.ILogService) {
   }

   public format(value: moment.Moment, displayFormat: string): string {
      this.$log.log(value.locale());
      this.$log.log(value.locale('fr'));
      this.$log.log(value.locale());
      return value.format(displayFormat);
   }
}

results are :

angular.js:13239 Moment {_isAMomentObject: true, _i: "2010-10-20 4:30", _f: "YYYY-MM-DD", _isUTC: false, _pf: Object…}_d: Wed Oct 20 2010 00:00:00 GMT+0200 (CEST)_f: "YYYY-MM-DD"_i: "2010-10-20 4:30"_isAMomentObject: true_isUTC: false_isValid: true_locale: Locale_abbr: "en"_ordinalParse: /\d{1,2}(th|st|nd|rd)/_ordinalParseLenient: /\d{1,2}(th|st|nd|rd)|\d{1,2}/ordinal: (number)__proto__: Locale_pf: Object__proto__: Moment
angular.js:13239 en

Are you importing the locale data? Which .js file(s) are you using?

Do you mean adding another import after the line:
import * as moment from 'moment';

or import in the module sens, here with JSPM.
(I have moment.js and a lot of what looks like localexx.js like fr.js in my case in the jspm modules folder.)

Read through the "Where to use it" section of the docs and see if any of those scenarios apply to you. In general, you either need to be using moment-with-locales.js, or have loaded the locale data separately. Most loaders will not automatically pick up the language file, so you may need to import them separately.

Basically, if you try to switch to a locale that's not loaded, moment will revert to the en locale.

I'll experiment what I can whith this.
I'm using a simple line added to package.json with is then used by jspm through npm install
"moment": "github:moment/moment@^2.11.2"

I then use it with a single import:
import * as moment from 'moment';

I wrote it in this comment to keep a track for future users who may have the same usage.
I'll post the result of my experiments.
Thanks

I added:
import 'moment/locale/fr';

and using:
moment.locale('fr')

now sets properly the locale to french.
It is not dynamic but it will do the job for now.

I can close it I think and thanks for all people who answered.
Hope this issue will help people using TypeScript/Angular/Jspm.

Maybe add this to the documentation. I had the same issue and more and more people start using jspm instead of Bower.

Glad you got it working! I added an issue to the docs repo. Feel free to send a PR there.

import like that
import 'moment/src/locale/fr-ca';
import 'moment/src/locale/en-ca';

Thanks @younss, your method worked for me. I'm using Ionic 2 RC 2.

@younss you are my savior! it's 10/10/2017 and the docs don't specify this change! updated from moment 2.18 to 2.19.0 and locales imports completely crashed :(
Using React.
Thanks again!

I had the same problem when I updaded to the 2.19.1
Maybe this could help:

Remove :

  • node_modules folder
  • package-lock.json

Re-launch:
npm install

I keep import 'moment' normally and use locale in my sources. So no need to import as @younss did.
Using VueJS.
This worked for me.

Maybe the pb is in the package-lock.json file which do not update the moment node data properly. By removing it and re-launching npm install it create a new package-lock.json file with all data updated.

@younss, thanks , works for me, i am using parcel-bundler

This Solution Worked for me.. Here

I added:
import 'moment/locale/fr';

Did this change again?
I installed:

npm i moment-with-locales-es6
npm i moment

I created a class with one function:

import * as moment from 'moment';
import 'moment/locale/pt-br';
import 'moment/locale/fr-ca';

export class DatetimeFormatPipe
    transform(date: string): string {
        moment.locale('pt-BR');
        console.log(moment.locale()); 
        console.log('pt-BR', moment(date).toLocaleString());

        moment.locale('fr-CA');
        console.log(moment.locale());
        console.log('fr-CA', moment(date).toLocaleString());

        return "";
    }
}

The logs are as follows:

pt-br
pt-BR Tue Dec 20 2016 10:45:12 GMT+0100
fr-ca
fr-CA Tue Dec 20 2016 10:45:12 GMT+0100

import like that
import 'moment/src/locale/fr-ca';
import 'moment/src/locale/en-ca';

It works for me thanks!

I use:

import moment from 'moment/dist/moment';
import fr from 'moment/dist/locale/fr';

moment.locale('fr', fr);

and it works.
Other solutions did not work for me.

import moment from 'moment/dist/moment';
import 'moment/dist/locale/ru';

moment.locale('ru');

The only works for me. @ligne13 Thanks for the hint!

moment 2.25.2

Hi,

When using 'import 'moment/locale/fr';' in my Node/Express App, I got this error : "SyntaxError: Cannot use import statement outside a module".

I googled this and I try to add --experimental-modules --es-module-specifier-resolution=node flags but now my app is a mess.

If I can't add this import, is there another way to change the locale ?

Thanks

Paul


Specs :

  • OS : Ubuntu 20.04 through WSL2
  • Node : 12.18.3
  • NPM : 6.14.6
  • Express : 4.16.1
  • MomentJS : 2.27

Hi,

When using 'import 'moment/locale/fr';' in my Node/Express App, I got this error : "SyntaxError: Cannot use import statement outside a module".

I googled this and I try to add --experimental-modules --es-module-specifier-resolution=node flags but now my app is a mess.

If I can't add this import, is there another way to change the locale ?

Thanks

Paul

Specs :

  • OS : Ubuntu 20.04 through WSL2
  • Node : 12.18.3
  • NPM : 6.14.6
  • Express : 4.16.1
  • MomentJS : 2.27

@pbrissaud

You can't use 'import' in node.js yet. I haven't tried it myself, but I reckon that this would work:

require('moment/locale/fr')
moment.locale('fr');
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Delgan picture Delgan  ·  3Comments

slavafomin picture slavafomin  ·  3Comments

RobinvanderVliet picture RobinvanderVliet  ·  3Comments

Shoroh picture Shoroh  ·  3Comments

ninigix picture ninigix  ·  3Comments