我正在尝试更改moment.js设置的日期的语言。默认是英语,但我想设置德语。以下是我的尝试:
var now = moment().format("LLL").lang("de");
它会产生NaN。
var now = moment("de").format("LLL");
这甚至不是反应。
var now = moment().format("LLL", "de");
没有变化:这仍然是用英语生成的结果。
这怎么可能呢?
我正在尝试更改moment.js设置的日期的语言。默认是英语,但我想设置德语。以下是我的尝试:
var now = moment().format("LLL").lang("de");
它会产生NaN。
var now = moment("de").format("LLL");
这甚至不是反应。
var now = moment().format("LLL", "de");
没有变化:这仍然是用英语生成的结果。
这怎么可能呢?
当前回答
对于那些在异步环境中工作的人来说,在按需加载区域设置时,moment的行为会出乎意料。
而不是
await import('moment/locale/en-ca');
moment.locale('en-ca');
颠倒顺序
moment.locale('en-ca');
await import('moment/locale/en-ca');
似乎locale被加载到当前选定的locale中,覆盖了之前设置的任何locale信息。所以先切换语言环境,再加载语言环境信息不会导致这个问题。
其他回答
使用momentjs 2.8+,执行以下操作:
moment.locale("de").format('LLL');
http://momentjs.com/docs/#/i18n/
您需要在脚本中添加moment.lang(navigator.language)。
并且必须添加你想要显示的每个国家的语言环境:例如GB或FR,你需要在moment.js库中添加该语言环境格式。在momentjs文档中可以找到这种格式的示例。如果你不在moment.js中添加这种格式,那么它总是会选择US locale,因为这是我目前看到的唯一一个。
流星用户:
Moment locale没有在meteor中默认安装,您只能在默认安装中获得'en' locale。
所以你使用的代码显示正确的其他答案:
moment.locale('it').format('LLL');
但在安装所需的区域设置之前,它将保持英文。
有一种很好的、干净的方法可以为meteor中的moment添加单独的locale(由rzymek提供)。
以通常的流星方式安装moment包:
meteor add rzymek:moment
然后添加你需要的语言环境,例如意大利语:
meteor add rzymek:moment-locale-it
或者如果你真的想添加所有可用的地区(添加大约30k到你的页面):
meteor add rzymek:moment-locales
当我与gulp和朋友一起使用webpack时(这个生成器为我设置了一切),我不得不对凉亭进行更改。json文件。我必须重写当前包的默认导入,并选择所有语言附带的文件:
"overrides": {
"moment": {
"main": [
"min/moment-with-locales.min.js"
]
}
}
这是我的凉亭。json文件:
{
"name": "html5",
"version": "0.0.0",
"dependencies": {
"angular-animate": "~1.4.2",
"angular-cookies": "~1.4.2",
"angular-touch": "~1.4.2",
"angular-sanitize": "~1.4.2",
"angular-messages": "~1.4.2",
"angular-ui-router": "~0.2.15",
"bootstrap-sass": "~3.3.5",
"angular-bootstrap": "~0.13.4",
"malarkey": "yuanqing/malarkey#~1.3.1",
"angular-toastr": "~1.5.0",
"moment": "~2.10.6",
"animate.css": "~3.4.0",
"angular": "~1.4.2",
"lodash": "^4.13.1",
"angular-moment": "^0.10.3",
"angularLocalStorage": "ngStorage#^0.3.2",
"ngstorage": "^0.3.10"
},
"devDependencies": {
"angular-mocks": "~1.4.2"
},
"overrides": {
"bootstrap-sass": {
"main": [
"assets/stylesheets/_bootstrap.scss",
"assets/fonts/bootstrap/glyphicons-halflings-regular.eot",
"assets/fonts/bootstrap/glyphicons-halflings-regular.svg",
"assets/fonts/bootstrap/glyphicons-halflings-regular.ttf",
"assets/fonts/bootstrap/glyphicons-halflings-regular.woff",
"assets/fonts/bootstrap/glyphicons-halflings-regular.woff2"
]
},
"moment": {
"main": [
"min/moment-with-locales.min.js"
]
}
},
"resolutions": {
"angular": "~1.4.2"
}
}
你需要时间。lang(警告:lang()自2.8.0时刻起已弃用,请使用locale()代替):
moment.lang("de").format('LLL');
http://momentjs.com/docs/#/i18n/
从v2.8.1开始,moment.locale('de')设置本地化,但不返回时刻。一些例子:
var march = moment('2017-03')
console.log(march.format('MMMM')) // 'March'
moment.locale('de') // returns the new locale, in this case 'de'
console.log(march.format('MMMM')) // 'March' still, since the instance was before the locale was set
var deMarch = moment('2017-03')
console.log(deMarch.format('MMMM')) // 'März'
// You can, however, change just the locale of a specific moment
march.locale('es')
console.log(march.format('MMMM')) // 'Marzo'
总而言之,在全局moment上调用locale将为所有未来moment实例设置locale,但不会返回moment实例。在实例上调用locale,为该实例设置它并返回该实例。
此外,正如Shiv在评论中所说,确保你使用的是“moment-with-locale .min.js”而不是“moment.min.js”,否则它将无法工作。