views:

1002

answers:

4

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?

+2  A: 

You will need to use JavaScript, but the procedure is quite straightforward. Assuming that the number you wish to convert is already in a string, then something like the following snippet of code will work:

var newValue="";
var digits="1234"; // this is your input string
for (var i=0;i<digits.length;i++)
{
  var ch=digits.charCodeAt(i);
  if (ch>=48 && ch<=57)
  {
    // european digit range
    var newChar=ch+1584;
    newValue=newValue+String.fromCharCode(newChar);
  }
  else
    newValue=newValue+String.fromCharCode(ch);
}

The code isn't very pretty and can probably be written more efficiently, but essentially what it's doing is converting any char from "0" to "9" by adding an offset value to make the character value now be in the unicode range for the Indic digits. The Indic digits range from \u0660 to \u0669 hence the conversion from European to Indic digits is just simple maths.

ferdley
A: 

The "ASCII equivalents" you are referring to are not actually that at all.

First of all, ASCII is a 7-bit character encoding in which characters like Arabic-Indic Digit Two don't exist.

Secondly, what you are seeing are actually HTML Entities. To programmatically make a conversion from Latin numerals to these entities would require the exertion of a backend language like PHP, Perl, C#, etc.

Thirdly, the numeric value represented in the entities is their Unicode Code Point in decimal form. So ٢ is the Unicode character at code point 1634 (decimal) or 0662 (hex), which is the more standard notation.

Lastly, I like ferdley's approach, but the tricky part will figuring out how to use his algorithm to replace only the numbers you want, and not numbers that otherwise appear in the HTML source, such as the pixel-width of an image.

Peter Bailey
A: 

To explain this comment:

Like in this link almasry-alyoum.com when I view the source of this page, I find that Indian letters are put in their ascii representation (i.e. &#1634;&#1635;&#1639;)

These are HTML character entities. The values are Unicode codepoints as defined by the documentation.

0660 ARABIC-INDIC DIGIT ZERO
0661 ARABIC-INDIC DIGIT ONE
0662 ARABIC-INDIC DIGIT TWO
0663 ARABIC-INDIC DIGIT THREE
0664 ARABIC-INDIC DIGIT FOUR
0665 ARABIC-INDIC DIGIT FIVE
0666 ARABIC-INDIC DIGIT SIX
0667 ARABIC-INDIC DIGIT SEVEN
0668 ARABIC-INDIC DIGIT EIGHT
0669 ARABIC-INDIC DIGIT NINE

So, ٠ ١ ٢ ٣ ٤ ٥ ٦ ٧ ٨ ٩ can be encoded as &#x0660; &#x0661; &#x0662; &#x0663; &#x0664; &#x0665; &#x0666; &#x0667; &#x0668; &#x0669; in a web page.

Note: &# for decimal values; &#x for hex.

McDowell
Related to this topic: http://stackoverflow.com/questions/1676460/why-in-the-arabic-unicode-representation-of-arabic-numbers-there-is-2-representat
McDowell
+2  A: 

How about a straight replace function?

String.prototype.toIndiaDigits= function(){
 var id= ['०','१','२','३','४','५','६','७','८','९'];
 return this.replace(/[0-9]/g, function(w){
  return id[+w]
 });
}

// test

var S='The year 2009 has only 365 days'; alert(S.toIndiaDigits());

/* returned value: (String) The year २००९ is has only ३६५ days */

kennebec
This will work provided the HTML and/or JavaScript source files are encoded as UTF-8 or some other Unicode-compatible encoding.
Peter Bailey