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
andRULE_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
1 Answer
Reset to default 3Moment 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条)