简体   繁体   中英

Moment.js: getting translated text for the terms “day”, “hour”, “second” and “minute”

Using moments.js, it is easily possible to set a language and then get various date strings and "calendar style" text translated to the desired language.

However I could not find a way to get a translation to the "date components" themselves. For example, I would want to sue something like:

moment.lang('en'); // set english
moment.dateComponent.hour() // yields "hour"
moment.dateComponent.hours() // yields "hours"
moment.dateComponent.day() // yields "day"
moment.dateComponent.days() // yields "days"
moment.lang('nl'); // set dutch
moment.dateComponent.hour() // yields "uur"
moment.dateComponent.hours() // yields "uur"
moment.dateComponent.day() // yields "dag"
moment.dateComponent.days() // yields "dagen"

Does anyone know of such a way?

Note: I rather avoid getting the translations by doing string splitting on other methods for which the purpose is not getting the date component translated name.

I don't know moments.js so far, but if you have a look at the source code (See http://momentjs.com/downloads/moment-with-langs.js ), you will find you desired information.

So you can access some information directly

moment().lang('en')._lang._months --> ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

quite usefull ... but

moment().lang('en')._lang._relativeTime.hh --> "%d hours"

does not store the information atomar. Since the languages are differently and can not be build this way!

As you can see that this information is not accessible like you want. Use a translation javascript for your "words".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM