# 国际化
Moment.js has robust support for internationalization.You can load multiple locales and easily switch between them.In addition to assigning a global locale, you can assign a locale to a specific moment.
# 设置语言_全局
// From 2.8.1 onward
moment.locale(String);
moment.locale(String[]);
moment.locale(String, Object);
// Deprecated in 2.8.1
moment.lang(String);
moment.lang(String[]);
moment.lang(String, Object);
2
3
4
5
6
7
8
9
By default, Moment.js comes with English locale strings. If you need other locales, you can load them into Moment.js for later use.
To load a locale, pass the key and the string values to moment.locale.
More details on each of the parts of the locale bundle can be found in the customization section.
moment.locale('fr', {
months : "janvier_février_mars_avril_mai_juin_juillet_août_septembre_octobre_novembre_décembre".split("_"),
monthsShort : "janv._févr._mars_avr._mai_juin_juil._août_sept._oct._nov._déc.".split("_"),
weekdays : "dimanche_lundi_mardi_mercredi_jeudi_vendredi_samedi".split("_"),
weekdaysShort : "dim._lun._mar._mer._jeu._ven._sam.".split("_"),
weekdaysMin : "Di_Lu_Ma_Me_Je_Ve_Sa".split("_"),
longDateFormat : {
LT : "HH:mm",
LTS : "HH:mm:ss",
L : "DD/MM/YYYY",
LL : "D MMMM YYYY",
LLL : "D MMMM YYYY LT",
LLLL : "dddd D MMMM YYYY LT"
},
calendar : {
sameDay: "[Aujourd'hui à] LT",
nextDay: '[Demain à] LT',
nextWeek: 'dddd [à] LT',
lastDay: '[Hier à] LT',
lastWeek: 'dddd [dernier à] LT',
sameElse: 'L'
},
relativeTime : {
future : "dans %s",
past : "il y a %s",
s : "quelques secondes",
m : "une minute",
mm : "%d minutes",
h : "une heure",
hh : "%d heures",
d : "un jour",
dd : "%d jours",
M : "un mois",
MM : "%d mois",
y : "une année",
yy : "%d années"
},
ordinalParse : /\d{1,2}(er|ème)/,
ordinal : function (number) {
return number + (number === 1 ? 'er' : 'ème');
},
meridiemParse: /PD|MD/,
isPM: function (input) {
return input.charAt(0) === 'M';
},
// in case the meridiem units are not separated around 12, then implement
// this function (look at locale/id.js for an example)
// meridiemHour : function (hour, meridiem) {
// return /* 0-23 hour, given meridiem token and hour 1-12 */
// },
meridiem : function (hours, minutes, isLower) {
return hours < 12 ? 'PD' : 'MD';
},
week : {
dow : 1, // Monday is the first day of the week.
doy : 4 // The week that contains Jan 4th is the first week of the year.
}
});
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Once you load a locale, it becomes the active locale. To change active locales, simply call moment.locale with the key of a loaded locale.
moment.locale('fr');
moment(1316116057189).fromNow() // il y a une heure
moment.locale('en');
moment(1316116057189).fromNow() // an hour ago
2
3
4
- As of 2.8.0, changing the global locale doesn't affect existing instances.
moment.locale('fr');
var m = moment(1316116057189);
m.fromNow(); // il y a une heure
moment.locale('en');
m.fromNow(); // il y a une heure
moment(1316116057189).fromNow(); // an hour ago
2
3
4
5
6
7
moment.locale returns the locale used. This is useful because Moment won't change locales if it doesn't know the one you specify.
moment.locale('fr'); // 'fr'
moment.locale('tq'); // 'fr'
2
You may also specify a list of locales, and Moment will use the first one it has localizations for.
moment.locale(['tq', 'fr']); // 'fr'
Moment will also try locale specifier substrings from most-specific to least-specific until it finds a locale it knows. This is useful when supplying Moment with a locale string pulled from the user's environment, such as window.navigator.language.
moment.locale('en-NZ'); // 'en'
Finally, Moment will search intelligently through an array of locales and their substrings.
moment.locale('en-NZ', 'en-AU'); // 'en-au', not 'en'
# 设置语言_局部
// From version 2.8.1 onward
moment().locale(String);
// Deprecated version 2.8.1
moment().lang(String);
2
3
4
5
A global locale configuration can be problematic when passing around moments that may need to be formatted into different locale.
- In 1.7.0 we added instance specific locale configurations.
moment.locale('en'); // default the locale to English
var localLocale = moment();
localLocale.locale('fr'); // set this instance to use French
localLocale.format('LLLL'); // dimanche 15 juillet 2012 11:01
moment().format('LLLL'); // Sunday, July 15 2012 11:01 AM
moment.locale('es'); // change the global locale to Spanish
localLocale.format('LLLL'); // dimanche 15 juillet 2012 11:01
moment().format('LLLL'); // Domingo 15 Julio 2012 11:01
localLocale.locale(false); // reset the instance locale
localLocale.format('LLLL'); // Domingo 15 Julio 2012 11:01
moment().format('LLLL'); // Domingo 15 Julio 2012 11:01
2
3
4
5
6
7
8
9
10
11
12
13
14
If you call moment#locale with no parameters, you get back the locale configuration that would be used for that moment.
var fr = moment().locale('fr');
fr.localeData().months(moment([2012, 0])) // "janvier"
fr.locale('en');
fr.localeData().months(moment([2012, 0])) // "January"
2
3
4
If you need to access the locale data for a moment, this is the preferred way to do so.
- As of 2.3.0, you can also specify an array of locale identifiers. It works the same was it does in the global locale configuration.
# 加载语言_NodeJS
moment.locale(String);
Loading locales in NodeJS is super easy. If there is a locale file in moment-root/locale/ named after that key, the first call to moment.locale will load it.
var moment = require('moment');
moment.locale('fr');
moment(1316116057189).fromNow(); // il y a une heure
2
3
If you want your locale supported, create a pull request to the develop branch with the required locale and unit test files.
# 加载语言_浏览器
// From 2.8.1 onward
moment.locale(String, Object);
// Deprecated in 2.8.1
moment.lang(String, Object);
2
3
4
5
Loading locales in the browser just requires you to include the locale files.
<script src="moment.js"></script>
<script src="locale/fr.js"></script>
<script src="locale/pt.js"></script>
<script>
moment.locale('fr'); // Set the default/global locale
// ...
</script>
There are minified versions of all locales together:
<script src="moment.js"></script>
<script src="min/locales.js"></script>
2
3
4
5
6
7
8
9
10
11
To minimize http requests, use our Grunt task to compile Moment with a custom list of locales:
grunt transpile:fr,it
<script src="min/moment-with-locales.custom.js"></script>
- Note: Locale files are defined in UMD style, so they should work seamlessly in all environments.
# 新增语言
To add your locale to Moment.js, submit a pull request with both a locale file and a test file. You can find examples in
moment/locale/fr.js and moment/test/locale/fr.js.To run the tests in Node.js, do
npm install, thengrunt.If all the tests pass, submit a pull request, and thank you for contributing!
# 获取当前语言
// From version 2.8.1 onward
moment.locale();
// Deprecated in version 2.8.1
moment.lang();
2
3
4
5
If you are changing locales frequently, you may want to know what locale is currently being used. This is as simple as
calling moment.locale without any parameters.
moment.locale('en'); // set to english
moment.locale(); // returns 'en'
moment.locale('fr'); // set to french
moment.locale(); // returns 'fr'
2
3
4
# 列出月份和星期
moment.months()
moment.monthsShort()
moment.weekdays()
moment.weekdaysShort()
moment.weekdaysMin()
2
3
4
5
It is sometimes useful to get the list of months or weekdays in a locale, for example when populating a dropdown menu.
moment.months();
Returns the list of months in the current locale.
[ 'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December' ]
```
> Similarly, `moment.monthsShort` returns abbreviated month names, and `moment.weekdays, moment.weekdaysShort, moment.weekdaysMin` return lists of weekdays.You can pass an integer into each of those functions to get a specific month or weekday.
```js
moment.weekdays(3); // 'Wednesday'
```
- Note: Currently, `weekdays always have Sunday as index 0`, regardless of the local first day of the week.
> Some locales make special considerations into account when formatting month names. For example, Dutch formats month abbreviations without a trailing period, but only if it's formatting the month between dashes. The months method supports passing a format in so that the months will be listed in the proper context.
```js
moment.locale('nl');
moment.monthsShort(); // ['jan.', 'feb.', 'mrt.', ...]
moment.monthsShort('-MMM-'); // [ 'jan', 'feb', 'mrt', ...]
```
> And finally, you can combine both the format option and the integer option.
```js
moment.monthsShort('-MMM-', 3); // 'apr'
```
## 获取语言数据
[back](#国际化)
```js
localeData = moment.localeData()
localeData.months()
localeData.monthsShort()
localeData.monthsParse()
localeData.weekdays()
localeData.weekdaysShort()
localeData.weekdaysMin()
localeData.weekdaysParse()
localeData.longDateFormat()
localeData.isPM()
localeData.meridiem()
localeData.calendar()
localeData.relativeTime()
localeData.pastFuture()
localeData.ordinal()
localeData.preparse()
localeData.postformat()
localeData.weeks()
localeData.invalidDate()
localeData.firstDayOfWeek()
localeData.firstDayOfYear()
```
> You can access the properties of the currently loaded locale through the moment.localeData(key) function. It returns the current locale or a locale with the given key:
```js
// get current locale
var currentLocaleData = moment.localeData();
var frLocaleData = moment.localeData('fr');
```
The returned object has the following methods:
```js
localeData.months(aMoment); // full month name of aMoment
localeData.monthsShort(aMoment); // short month name of aMoment
localeData.monthsParse(longOrShortMonthString); // returns month id (0 to 11) of input
localeData.weekdays(aMoment); // full weekday name of aMoment
localeData.weekdaysShort(aMoment); // short weekday name of aMoment
localeData.weekdaysMin(aMoment); // min weekday name of aMoment
localeData.weekdaysParse(minShortOrLongWeekdayString); // returns weekday id (0 to 6) of input
localeData.longDateFormat(dateFormat); // returns the full format of abbreviated date-time formats LT, L, LL and so on
localeData.isPM(amPmString); // returns true iff amPmString represents PM
localeData.meridiem(hours, minutes, isLower); // returns am/pm string for particular time-of-day in upper/lower case
localeData.calendar(key, aMoment); // returns a format that would be used for calendar representation. Key is one of 'sameDay', 'nextDay', 'lastDay', 'nextWeek', 'prevWeek', 'sameElse'
localeData.relativeTime(number, withoutSuffix, key, isFuture); // returns relative time string, key is on of 's', 'm', 'mm', 'h', 'hh', 'd', 'dd', 'M', 'MM', 'y', 'yy'. Single letter when number is 1.
localeData.pastFuture(diff, relTime); // convert relTime string to past or future string depending on diff
localeData.ordinal(number); // convert number to ordinal string 1 -> 1st
localeData.preparse(str); // called before parsing on every input string
localeData.postformat(str); // called after formatting on every string
localeData.week(aMoment); // returns week-of-year of aMoment
localeData.invalidDate(); // returns a translation of 'Invalid date'
localeData.firstDayOfWeek(); // 0-6 (Sunday to Saturday)
localeData.firstDayOfYear(); // 0-15 this and the first day of week are used
// to determine which is the first week of the
// year. dow == 1 and doy == 4 means week starts
// Monday and first week that has Thursday is the
// first week of the year (but doy is NOT simply
// Thursday).
```
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101