Convert English Numbers to Persian

Convert English Numbers to Persian in a webpage

Sometimes you need to serve everything in persian in a web page, but the code page of persian (and arabic) numbers is deferent, so you have to change all numbers to persian code page in your content  (html static pages or dynamic pages or database).
There is a big point here, html uses numbers in structure (styles, color codes, size and ...), so you have to change just numbers in contents not inside html tags, if you change all numbers (in content and tags) then you corrupt your page compeletly. This script tries to find numbers in content and skip numbers inside html tags.

You should load this script just before body ending tag . For example:

1.<script src="[JS PATH]/ConvertNumbersEn2Pe.js" type="text/javascript">< /script>
2.< /body>

Script Code of ConvertNumbersEn2Pe.js Dwonload Here

01.String.prototype.toPersianDigit = function (a) {
02.    return this.replace(/\d+/g, function (digit) {
03.        var enDigitArr = [], peDigitArr = [];
04.        for (var i = 0; i < digit.length; i++) {
05.            enDigitArr.push(digit.charCodeAt(i));
06.        }
07.        for (var j = 0; j < enDigitArr.length; j++) {
08.            peDigitArr.push(String.fromCharCode(enDigitArr[j] + ((!!a && a == true) ? 1584 : 1728)));
09.        }
10.        return peDigitArr.join('');
11.    });
12.};
13.  
14.function TraceNodes(Node) {
15.    if (Node.nodeType == 3)  //TextNode
16.        Node.nodeValue = Node.nodeValue.toPersianDigit();
17.    else
18.        for (var i = 0; i < Node.childNodes.length; i++)
19.            TraceNodes(Node.childNodes[i]);
20.}
21.  
22.TraceNodes(document);