vis.js的官网中这部分写的不是很清楚,只提到了timeliness的本地化是基于moment.js
,如果需要自定义,需要引入moment.js
,或者使用moment.js
包装好的本地化文件moment-with-locales.min.js
,原文如下:
Timeline can be localized. For localization, Timeline depends largely on the localization of moment.js. Locales are not included in vis.js by default. To enable localization, moment.js must be loaded with locales. Moment.js offers a bundle named “moment-with-locales.min.js” for this and there are various alternative ways to load locales.
…
Create a new localeTo load a locale (that is not supported by default) into the Timeline, one can add a new locale to the option
locales
:var options = { locales: { // create a new locale (text strings should be replaced with localized strings) mylocale: { current: 'current', time: 'time', } }, // use the new locale locale: 'mylocale' };
但是文档中没有提到应该如何使用moment.js
,在moment.js
的官网上也没有看到使用说明,所以我大概地看了一下vis.js这部分的代码实现,逻辑是这样:vis会先检测moment.js
是否已经引入,否的话就通过commonjs引入;
module.exports = typeof window !== 'undefined' && window['moment'] || __webpack_require__(3);
在引用了moment-with-locales.min.js
文件后,’moment’会被注册,此时在timeline的初始化中调用moment-with-locales.min.js
文件中实现的本地化对象来初始化locale属性即可(中文对象名为:’zh-cn’):
var option = {
...
locale: moment.locale('zh-cn'),
...
}
需要注意的是,因为vis会先检测moment
对象是否存在,如果不存在则会通过commonJS引入moment.js
,但是引入的moment.js
没有中文本地化对象,所以需要将moment-with-locales.min.js
引入放在vis.js
引入之前