Mazzi
Mazzi

Reputation: 949

Farsi/Arabic number to be outputted properly

You have a file that outputs as such:

<div id="first">1</div>
2
<div id="third">
    <? $i = 3; echo(i) ?>
</div>

Which outputs:

1
2
3

Now if I want to have an output as below:

?
?
?

Well, I was hoping I could output the Arabic/Farsi version of 1,2,3 instead of '?'. How can I use HTML/CSS convert to numbers to Arabic/Farsi?

And why does everything change except numbers when I change my language (Windows XP)?

Upvotes: 3

Views: 4927

Answers (5)

ShirazITCo
ShirazITCo

Reputation: 1041

Use the following JavaScript code. This will replace numbers:

function recursiveReplace(node) {
    if (node.nodeType == 3) { // text node
        node.nodeValue = node.nodeValue.replace("1", "۱");
    } else if (node.nodeType == 1) { // element
        $(node).contents().each(function () {
            recursiveReplace(this);
        });
    }
}

recursiveReplace(document.body);

Upvotes: 1

amir22
amir22

Reputation: 423

Simply use CSS:

ol {
    list-style-type: persian
}

Upvotes: 1

wkw
wkw

Reputation: 3863

Expanding on ShirzITCo and Kirk Woll's answer above -- which saved my but when client decided all the content in a WordPress CMS should use Eastern-Arabic numerals instead of the Western Arabic they started with:

function recursiveReplace(node) {
  if (node.nodeType === 3) { // text node
    node.nodeValue = node.nodeValue
    .replace(/0/g,'۰')
    .replace(/1/g,'۱')
    .replace(/2/g,'۲')
    .replace(/3/g,'۳')
    .replace(/4/g,'۴')
    .replace(/5/g,'۵')
    .replace(/6/g,'۶')
    .replace(/7/g,'۷')
    .replace(/8/g,'۸')
    .replace(/9/g,'۹');
  } else if (node.nodeType === 1) { // element
    $(node).contents().each(function () {
      recursiveReplace(this);
    });
  }
}
recursiveReplace(document.body);

Upvotes: 3

Gumbo
Gumbo

Reputation: 655707

If you want to number a list, you could use an ol element and change the list-style-type of it like:

ol {
    list-style: arabic-indic;
}

Upvotes: 4

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799310

  1. There is no automated way to change numbers, or even digits. Set up a mapping between then and convert manually.

  2. Numbers are never localized, because unlike natural language words, Arabic numerals are understood across most of the world.

Upvotes: 1

Related Questions