最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set locale on momentJS won't work - Stack Overflow

programmeradmin0浏览0评论

I've installed momentJS on ionic/angular using bower - and it works fine. Except for changing locale to 'fr' or 'da'. The files are available in the locales-folder, but the following code still echoes in english :(

moment.locale('fr');
var NowMoment = moment().format("dddd, MMMM Do");
console.log(NowMoment);

It's written in my directive/link function. Should I do anything different ?

Thanx Ask

I've installed momentJS on ionic/angular using bower - and it works fine. Except for changing locale to 'fr' or 'da'. The files are available in the locales-folder, but the following code still echoes in english :(

moment.locale('fr');
var NowMoment = moment().format("dddd, MMMM Do");
console.log(NowMoment);

It's written in my directive/link function. Should I do anything different ?

Thanx Ask

Share Improve this question asked Jan 2, 2016 at 22:05 AskbarAskbar 8991 gold badge7 silver badges11 bronze badges 2
  • 1 Did you load/include the lang file for fr? – t.niese Commented Jan 2, 2016 at 22:09
  • 1 of course - including the language file helps ;) thanx - feel free to post as answer – Askbar Commented Jan 3, 2016 at 8:24
Add a ment  | 

2 Answers 2

Reset to default 5

moment.js itself does not load any language files. So as long as you do not use moment+locales.js (which would contain all locals) you will either need to include the additional language data with your moment.js or you need to load it separate.

So either something like this:

<script src="/js/moment+fr.js"></script>

Or this that way:

<script src="/js/moment.js"></script>
<script src="/js/locale/fr.js"></script>

If you only use fr then you would not need to call moment.locale('fr'); in this particular case, because the last loaded local will be the active one.

When working with TypeScript

import moment from 'moment/moment';
import 'moment/locale/es';

moment.locale('es');

locale function wouldn't change the language You will need to explicitly call the js library

import moment from 'moment/moment.js';

and this will do the magic.

发布评论

评论列表(0)

  1. 暂无评论