Customize numeric values in Moment Arabic localization

一曲冷凌霜 提交于 2021-01-02 08:21:21

问题


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**
  */
});

回答1:


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.

You can simply restore default behaviour resetting preparse and postformat (e.g. see moment code: function preParsePostFormat (string) { return string; })

Here a live sample:

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>


来源:https://stackoverflow.com/questions/49401205/customize-numeric-values-in-moment-arabic-localization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!