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

locale changes does'nt work #2962

Closed
titimoby opened this issue Feb 15, 2016 · 26 comments
Closed

locale changes does'nt work #2962

titimoby opened this issue Feb 15, 2016 · 26 comments

Comments

@titimoby
Copy link

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"
@elwynelwyn
Copy link

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

@titimoby
Copy link
Author

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 :

en
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

@mattjohnsonpint
Copy link
Contributor

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

@titimoby
Copy link
Author

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.)

@mattjohnsonpint
Copy link
Contributor

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.

@mattjohnsonpint
Copy link
Contributor

See also "Loading locales in the browser".

@titimoby
Copy link
Author

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

@titimoby
Copy link
Author

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.

@sbaechler
Copy link

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

@mattjohnsonpint
Copy link
Contributor

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

@younss
Copy link

younss commented Oct 24, 2016

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

@vanjiminster
Copy link

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

@dialnco
Copy link

dialnco commented Oct 11, 2017

@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!

@sgj0
Copy link

sgj0 commented Oct 12, 2017

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.

@Vitegor
Copy link

Vitegor commented Dec 29, 2017

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

@javiersm79
Copy link

javiersm79 commented Apr 20, 2018

This Solution Worked for me.. Here

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

@Hypenate
Copy link

Hypenate commented Sep 3, 2018

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

@JeongtaekLim
Copy link

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

It works for me thanks!

@ligne13
Copy link

ligne13 commented Jun 5, 2020

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.

@socketpair
Copy link
Contributor

socketpair commented Jun 17, 2020

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

@pbrissaud
Copy link

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

@YousefMohsen
Copy link

YousefMohsen commented Sep 3, 2020

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

@PandukaNandara
Copy link

Form 2022 onward you have to import as follows.

import 'moment/dist/locale/si';

@Arturexe
Copy link

Arturexe commented May 10, 2022

This is the shortest version that works for me:

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

moment.locale('de');

Working with Vue

@uanish
Copy link

uanish commented Apr 2, 2023

using import 'moment/dist/locale/de', I get an error when reloading the page(export default hooks;SyntaxError: Unexpected token 'export'). How can I fix this

@Dilshan97
Copy link

import 'moment/locale/fr';

this is work for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests