Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize numeric values in Moment Arabic localization

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**
  */
});
like image 297
Houssem Badri Avatar asked Mar 21 '18 08:03

Houssem Badri


1 Answers

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>
like image 75
VincenzoC Avatar answered Oct 16 '22 00:10

VincenzoC