天天看点

php 阿拉伯语转英文,国际化:对阿拉伯语下简单数字类型的转化

问题源于antd 4.0之后我们使用了day.js代替了moment.js.在设置了阿拉伯语的情况下,后端返回的时间日期是"2020/٠٧/٣١"以及"١٤:٠٦", TimePicker会出错无法修改时间。

在不想重新启用moment.js也不想引入语言包的情况下。我们可以使用toLocaleString()

MDN指路

如果使用moment.js的话 直接moment.locale('ar')就好了

使用toLocaleString()的话代码如下

let values = {

cur_locale: "ar",

cur_time: "١٤:٣٠",

cur_date: "2020/٠٧/٣١",

} //提供数据 供测试

let {locale, cur_time, cur_date} = values

let speciaLocale = ['ar'] // ar 阿拉伯语 特殊语言 返回值非“0123”这种数字 需要转化

if (speciaLocale.includes(locale)) {

let comArr = []

comArr.length = 10

comArr.fill(0) // 快速填充数组

comArr.map((item, i) => {

comArr[i] = i.toLocaleString(locale) // 下标与对应语言的数字对应

})

// comArr 为["٩", "٨", "٧", "٦", "٥", "٤", "٣", "٢", "١", "٠"] 对应从0-9

cur_time = this.transTime(cur_time, comArr)

cur_date = this.transTime(cur_date, comArr)

}

transTime = (timeStr, comArr) => {

let time = ''

timeStr.split('').map(item => {

let index = comArr.indexOf(item)

if (index !== -1) {

time = time + index // index即对应的阿拉伯数字

} else {

time = time + item

}

})

return time

}