Convert from English Digits to Arabic ones in html page

Viewed 29025

I need to convert all English numbers that appear in a given HTML page to Arabic ones (to be independent from the user browser encoding). I prefer to use javascript or it will be great if this can be handled using CSS.

I found some pages doing this but I found that the Arabic letters are added with their ASCII representation in the source code. Does it mean that they are applying some sort of a java script function?

Any clue how can I do something like this?

9 Answers

Convert English <> Arabic <> Persian

    //English to Persian digits.
    String.prototype.EntoFa= function() {
      return this.replace(/\d/g, d => '۰۱۲۳۴۵۶۷۸۹'[d])
    }
    
    //English to Arabic digits.
    String.prototype.EntoAr= function() {
      return this.replace(/\d/g, d =>  '٠١٢٣٤٥٦٧٨٩'[d])
    }
    
    //Arabic to English digits.
    String.prototype.ArtoEn= function() {
      return this.replace(/[\u0660-\u0669]/g, 
        d => d.charCodeAt() - 1632)
    }
    
    //Persian to English digits.
    String.prototype.PetoEn= function() {
      return this.replace(/[\u06F0-\u06F9]/g, 
        d => d.charCodeAt() - 1776)
    }
    
    //Persian to Arabic digits.
    String.prototype.PetoAr= function() {
      return this.replace(/[\u06F0-\u06F9]/g, 
        d => '٠١٢٣٤٥٦٧٨٩'[d.charCodeAt() - 1776])
    }
    
    //Arabic to Persian digits.
    String.prototype.ArtoPe= function() {
      return this.replace(/[\u0660-\u0669]/g, 
        d => '۰۱۲۳۴۵۶۷۸۹'[d.charCodeAt() - 1632])
    }
    
    //Both Persian and Arabic to English digits.
    String.prototype.IntoEn= function() {
      return this.replace(/[\u06F0-\u06F9\u0660-\u0669]/g, 
        d => ((c=d.charCodeAt()) > 1775 ? c - 1776 : c - 1632))
    }
    
    //English to either Persian or Arabic digits.
    String.prototype.EntoIn= function(e) {
      return this.replace(/\d/g, 
        d => e ? '٠١٢٣٤٥٦٧٨٩'[d] : '۰۱۲۳۴۵۶۷۸۹'[d])
    }
    
    //English to Persian digits using unicode.
    String.prototype.EntoFaUni= function() {
      return this.replace(/\d/g, d => String.fromCharCode('0x06F'+d))
    }
    
    //English to Arabic digits using unicode.
    String.prototype.EntoArUni= function() {
      return this.replace(/\d/g, d => String.fromCharCode('0x066'+d))
    }
    
    //English to either Persian or Arabic digits.
    String.prototype.EntoInUni= function(e) {
      return this.replace(/\d/g, d => String.fromCharCode('0x06'+(e ? '6':'F')+d))
    }
    
    //examples
    let En = 'It is 30/08/2018 at 8:24 AM'
    let Pe = 'It is ۳۰/۰۸/۲۰۱۹ at ۸:۲۴ AM'
    let Ar = 'It is ٣٠/٠٨/٢٠١٩ at ٨:٢٤ AM'
    
    let PeAr = 'It is ۳۰/۰۸/۲۰۱۹ at ۸:۲۴ | AM It is ٣٠/٠٨/٢٠١٩ at ٨:٢٤ AM'
    
    //Persian <> Araibc <> English
    
    console.log(Ar.ArtoEn())
    console.log(Pe.PetoEn())
    console.log(Pe.PetoAr())
    console.log(Ar.ArtoPe())
    console.log(PeAr.IntoEn())

    //using array
    console.log(En.EntoFa())
    console.log(En.EntoAr())
    console.log(En.EntoIn(0))
    console.log(En.EntoIn(1))
    
    //using unicode
    console.log(En.EntoFaUni())
    console.log(En.EntoArUni())
    console.log(En.EntoInUni(0))
    console.log(En.EntoInUni(1)) 

jsfiddle

I know this is a very old post, but for other people coming here from google search that have same problem, there is a relatively new method called toLocaleString which converts Number types to your preferred number system glyphs:

(2500000).toLocaleString('ar-EG');
//outputs: "٢٬٥٠٠٬٠٠٠"

Thanks for the answers. No one has discussed handling decimal and thousand markers. See Wikipedia for example. According to this page, these are the correct unicode characters:

  • U+066B - Arabic Decimal Separator
  • U+066C - Arabic Thousands Separator

Edit for the first answer, convert English numbers to Arabic numbers:

String.prototype.toArabicDigits = function(){
var id = ['٠','١','٢','٣','٤','٥','٦','٧','٨','٩'];
return this.replace(/[0-9]/g, function(w){
  return id[+w];
 });
};

Give this JavaScript function a string number and it will help you in converting english to arabic

function GetArabicNumber(number) {
            var charIndex = 0;
            var NumericArabic = "";

            while (charIndex < number.length) {
                switch (number[(charIndex)]) {
                    case '.':
                        NumericArabic += ".";
                        break;

                    case '0':
                        NumericArabic += "٠";
                        break;

                    case '1':
                        NumericArabic += "١";
                        break;

                    case '2':
                        NumericArabic += "٢";
                        break;

                    case '3':
                        NumericArabic += "٣";
                        break;

                    case '4':
                        NumericArabic += "٤";
                        break;

                    case '5':
                        NumericArabic += "٥";
                        break;

                    case '6':
                        NumericArabic += "٦";
                        break;

                    case '7':
                        NumericArabic += "٧";
                        break;

                    case '8':
                        NumericArabic += "٨";
                        break;

                    case '9':
                        NumericArabic += "٩";
                        break;

                    default:
                        NumericArabic += number[(charIndex)];
                        break;
                }

                charIndex++;
            }

            return NumericArabic;
        }
Related