Skip to content Skip to sidebar Skip to footer

Convert English Numbers To Persian/Arabic Only For A Specified Div

I know this question has been replied many times here, but I still haven't got an exact answer.. I need to convert English letters to Persian/Arabic letters by some javascript, but

Solution 1:

I don't believe either of the code samples you provided are JavaScript, the first is close syntactically, but is missing the range() method and new on the array() definition. The second is Java.

To achieve what you require you could convert the text of each of the HTML elements you want to translate to an array and step through them, checking each character via Regex to see if a number was found. If it was, you can do a simple replacement before joining the array back together. Something like this:

var arabicNumbers = ['۰', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
$('.translate').text(function(i, v) {
  var chars = v.split('');
  for (var i = 0; i < chars.length; i++) {
    if (/\d/.test(chars[i])) {
      chars[i] = arabicNumbers[chars[i]];
    }
  }
  return chars.join('');
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="translate">Dummy text with some 123 numbers in 390981 it.</div>
<div class="translate">Dummy text with some 67898 numbers in 109209734.09 it.</div>

Update - 2020-03

Here's a shorter version of the above logic using ES6 syntax. Note that this will work in all modern browsers. The only place it won't work is in any version of IE.

var arabicNumbers = ['۰', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
$('.translate').text((i, v) => v.split('').map(c => parseInt(c) ? arabicNumbers[parseInt(c)] : c).join(''));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="translate">Dummy text with some 123 numbers in 390981 it.</div>
<div class="translate">Dummy text with some 67898 numbers in 109209734.09 it.</div>

Solution 2:

for use in React , use this Component

import React, {Component} from "react";


class PersianNumber extends Component {


   render() {
       let en_number = this.props.number.toString();
       let persianDigits = "۰۱۲۳۴۵۶۷۸۹";
       let persianMap = persianDigits.split("");
       let persian_number = en_number.replace(/\d/g, function (m) {
         return persianMap[parseInt(m)];
       });

    return (
        <span>{persian_number}</span>
    )


  }

}


export default PersianNumber

save the file name as : PersianNumber.jsx and use like this :

<PersianNumber number={12365}/>

Solution 3:

You can use convertToPersianNumber function that I have copied from this link and use it as the following code in jQuery

$('.translate').text(function(i, v) {
   return convertToPersianNumber(v)
})

convertToPersianNumber code

var persianDigits = "۰۱۲۳۴۵۶۷۸۹";
var persianMap = persianDigits.split("");

function convertToEnglishNumber(input){
    return input.replace(/[\u06F0-\u06F90]/g, function(m){
        return persianDigits.indexOf(m);
    });
}
function convertToPersianNumber(input){
    return input.replace(/\d/g,function(m){
        return persianMap[parseInt(m)];
    });
}

// tests
console.log(convertToEnglishNumber("۴۳۵"));
console.log(convertToEnglishNumber("۶۲۷۰۱"));
console.log(convertToEnglishNumber("۳۵۴۳"));
console.log(convertToPersianNumber("216541"));
console.log(convertToPersianNumber("16549"));
console.log(convertToPersianNumber("84621"));

Solution 4:

To convert numeric characters, you just need to add/subtract the difference between two sets of Unicode characters to the original numbers. Here is an example:

// English to Persian/Arabic
console.log(
'Persian now:',
'12345'.replace(/[0-9]/g, c => String.fromCharCode(c.charCodeAt(0) + 1728))
);

// Persian/Arabic to English
console.log(
'English now:',
'۵۶۷۸۹'.replace(/[۰-۹]/g, c => String.fromCharCode(c.charCodeAt(0) - 1728))
);

Solution 5:

I think this could help:

    const arabicNumbers = ['۰', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
    const ThirtyNine = 39;
    const convertToArabic = (number) => {
      return String(number).split('').map(char => arabicNumbers[Number(char)]).join('');
    }
    const inArabic = convertToArabic(ThirtyNine);

Post a Comment for "Convert English Numbers To Persian/Arabic Only For A Specified Div"