Moment: 将阿拉伯数字转换为拉丁数字,我该怎么做?

创建于 2016-12-27  ·  3评论  ·  资料来源: moment/moment

我曾经通过执行以下操作将阿拉伯数字转换为拉丁数字

      moment.updateLocale('ar', <MomentLanguage>{
        preparse: (str) => {
            return str.replace(/\u200f/g, '');
        },
        postformat: (str) => {
            return str;
        }
      });

但是现在,MomentLanguage 不是即时模块,我该怎么办?
PS:我使用的是 2.15.2 版本

最有用的评论

@IbraheemAlSaady这应该可以

const symbolMap = {
        '1': '1',
        '2': '2',
        '3': '3',
        '4': '4',
        '5': '5',
        '6': '6',
        '7': '7',
        '8': '8',
        '9': '9',
        '0': '0'
    };
    const numberMap = {
        '١': '1',
        '٢': '2',
        '٣': '3',
        '٤': '4',
        '٥': '5',
        '٦': '6',
        '٧': '7',
        '٨': '8',
        '٩': '9',
        '٠': '0'
    }
  moment.updateLocale('ar', {
      preparse: function (string) {
          return string.replace(/\u200f/g, '').replace(/[١٢٣٤٥٦٧٨٩٠]/g, function (match) {
              return numberMap[match];
          }).replace(/،/g, ',');
      },
      postformat: function(string) {
        return string.replace(/\d/g, function(match) {
          return symbolMap[match];
        }).replace(/,/g, '،');
      },
  });

所有3条评论

你的代码是用 Typescript 写的,对吧? 我认为您正在使用的界面现在称为Locale

由于自卢卡斯发表评论以来我们没有收到回复,因此关闭。 如果您仍然需要帮助,请重新打开。

@IbraheemAlSaady这应该可以

const symbolMap = {
        '1': '1',
        '2': '2',
        '3': '3',
        '4': '4',
        '5': '5',
        '6': '6',
        '7': '7',
        '8': '8',
        '9': '9',
        '0': '0'
    };
    const numberMap = {
        '١': '1',
        '٢': '2',
        '٣': '3',
        '٤': '4',
        '٥': '5',
        '٦': '6',
        '٧': '7',
        '٨': '8',
        '٩': '9',
        '٠': '0'
    }
  moment.updateLocale('ar', {
      preparse: function (string) {
          return string.replace(/\u200f/g, '').replace(/[١٢٣٤٥٦٧٨٩٠]/g, function (match) {
              return numberMap[match];
          }).replace(/،/g, ',');
      },
      postformat: function(string) {
        return string.replace(/\d/g, function(match) {
          return symbolMap[match];
        }).replace(/,/g, '،');
      },
  });
此页面是否有帮助?
0 / 5 - 0 等级