How to localize numerals?

Posted by SoLoGHoST on Stack Overflow See other posts from Stack Overflow or by SoLoGHoST
Published on 2010-05-20T05:23:49Z Indexed on 2010/05/20 6:10 UTC
Read the original article Hit count: 186

Filed under:
|
|
|

Ok, I'm using mathematical equations to output numbers, though, I need this to be compatible for all languages. Currently, all language strings are within a php array called $txt, and each key of the array gets called for that language. I'm outputting the following: Column 1, Column 2, Column 3, and so on, as well as Row 1, Row 2, Row 3, and so on. The calculations are done via php and javascript, so I'm wondering on the best approach for how to support all languages for the numbers only.

I don't do the translations, someone else does, but I need to be able to point it to, either the php variable $txt of where the language is defined, or, since the calculations are done via javascript also, I need to somehow store this in there. I'm thinking of storing something like this:

// This part goes in the php language file.
$txt['0'] = '0';
$txt['1'] = '1';
$txt['2'] = '2';
$txt['2'] = '3';
$txt['4'] = '4';
$txt['5'] = '5';
$txt['6'] = '6';
$txt['7'] = '7';
$txt['8'] = '8';
$txt['9'] = '9';

// This part goes in the php file that needs to call the numbers.
echo '<script>
    var numtxts = new Array();
    numtxts[0] = \'', $txt['0'], '\';
    numtxts[1] = \'', $txt['1'], '\';
    numtxts[2] = \'', $txt['2'], '\';
    numtxts[3] = \'', $txt['3'], '\';
    numtxts[4] = \'', $txt['4'], '\';
    numtxts[5] = \'', $txt['5'], '\';
    numtxts[6] = \'', $txt['6'], '\';
    numtxts[7] = \'', $txt['7'], '\';
    numtxts[8] = \'', $txt['8'], '\';
    numtxts[9] = \'', $txt['9'], '\';
</script>';

And than in the javascript function it could grab the correct string for each number like so:

// Example Number String below.
var numString = "10";
var transNum = "";

for(x=0;x<numString.length;x++)
{
    var numChar = numString.charAt(x);
    transNum += numtxts[parseInt(numChar)];
}

return transNum;

The problem with this bit of code is that it groups the numbers, not sure if all languages do that, like the english language does...?

Perhaps there's a better approach for this? Can anyone help please?

Thanks :)

© Stack Overflow or respective owner

Related posts about php

Related posts about JavaScript