javascript - Moment.js - translate local month names to en-gb - Stack Overflow

I have an array with month names in locale (e.g. ianuare, februarie, martie, aprilie, mai, iunie, iulie

I have an array with month names in locale (e.g. ianuare, februarie, martie, aprilie, mai, iunie, iulie...) and I want to translate it to en-gb locale (January, February, March...). This is my code snippet I am using:

    let months = [...momentLocaleObject.months(), ...momentLocaleObject.monthsShort()]
    months.map((n) => {
        dateMap.get(RULE_ENTITIES.MONTHS).set(n, moment().month(n).locale('en-gb').format('MMMM'));
    });

The problem is that it is not working properly and some months are not translated correctly (please see attached picture). Can I ask you about any help?

Edit: JSFiddle is here: /

I have an array with month names in locale (e.g. ianuare, februarie, martie, aprilie, mai, iunie, iulie...) and I want to translate it to en-gb locale (January, February, March...). This is my code snippet I am using:

    let months = [...momentLocaleObject.months(), ...momentLocaleObject.monthsShort()]
    months.map((n) => {
        dateMap.get(RULE_ENTITIES.MONTHS).set(n, moment().month(n).locale('en-gb').format('MMMM'));
    });

The problem is that it is not working properly and some months are not translated correctly (please see attached picture). Can I ask you about any help?

Edit: JSFiddle is here: https://jsfiddle/ef9bmng7/

Share Improve this question edited May 3, 2017 at 21:48 Jan Bouchner asked May 3, 2017 at 19:44 Jan BouchnerJan Bouchner 8971 gold badge14 silver badges38 bronze badges 2
  • What dateMap and RULE_ENTITIES.MONTHS are? Maybe adding a snippet or a fiddle showing your issue would help you getting a useful answer. – VincenzoC Commented May 3, 2017 at 21:17
  • 1 Hi, thanks for your response. I tried to simplify that but the result is the same: jsfiddle/ef9bmng7 – Jan Bouchner Commented May 3, 2017 at 21:48
Add a ment  | 

1 Answer 1

Reset to default 3

Moment by default uses en locale.

In your code n is the name of the month in the given locale, you have to set locale properly to make month() work.

moment().month('ianuarie')

is not valid if current locale is en, while this will work:

// Setting locale locally
moment().locale('ro').month('ianuarie')
// Setting locale globally
moment.locale('ro');
moment().month('ianuarie');

See here more detailed info on setting locale in moment.

Here a working version of your fiddle, as exaplained, I've just added locale(key)before using month(n):

const countriesMap = new Map();

const roMap = new Map();
const roMapSpecials = new Map();
const roMapDate = new Map();
const roMapWeekdays = new Map();
const roMapMonths = new Map();

countriesMap.set('ro', roMap);

//roMap.set('Specials', roMapSpecials);
//roMapDate.set('Weekdays', roMapWeekdays);
roMapDate.set('Months', roMapMonths);
roMap.set('Date', roMapDate);

for (let pair of countriesMap) {
	let [key, value] = pair;
	let momentLocaleObject = moment.localeData(key);
	let dateMap = value.get('Date');
  
let months = [...momentLocaleObject.months(), ...momentLocaleObject.monthsShort()]
months.map((n) => {
	dateMap.get('Months').set(n, moment().locale(key).month(n).locale('en-gb').format('MMMM'));
});
}

console.log(countriesMap.get('ro').get('Date').get('Months'))
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742339748a4425380.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信