问题描述
我想在 moment.js 中创建自己的语言环境,它的父级应该是阿拉伯语本地环境,但我只想更改为数字格式以显示 0-9
,而不是默认显示.
I want to create my own locale in moment.js, its parent should be the Arabic local but I want only to change to numeric format to display 0-9
, not the default display.
根据文档,我可以从这个开始:
As per documentation, I can start with that:
moment.defineLocale('ar-sa-mine', {
parentLocale: 'ar-sa',
/*
here I need to specify the numeric: change **this ٢٩ to 29**
*/
});
推荐答案
如果您查看 locale/ar-sa.js
代码你会注意到,moment 使用了 preparse
和 postformat
将数字字符转换为阿拉伯语.
If you look at locale/ar-sa.js
code you will note that moment uses preparse
and postformat
to convert from numeric characters to arabic.
您可以简单地通过重置preparse
和postformat
来恢复默认行为(例如查看moment 代码:function preParsePostFormat (string) { return string; }
)
You can simply restore default behaviour resetting preparse
and postformat
(e.g. see moment code: function preParsePostFormat (string) { return string; }
)
这是一个现场示例:
console.log( moment().format() );
console.log( moment().format('dddd DD MMMM YYYY') );
moment.defineLocale('ar-sa-mine', {
parentLocale: 'ar-sa',
preparse: function (string) {
return string;
},
postformat: function (string) {
return string;
}
});
console.log( moment().format() );
console.log( moment().format('dddd DD MMMM YYYY') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/locale/ar-sa.js"></script>
这篇关于在 Moment 阿拉伯语本地化中自定义数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!