views:

118

answers:

2

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 :)

+1  A: 

For Php/Js communication, just use json:

echo "var jsArray = " + json_encode($phpArray);
Dykam
Sorry, I don't follow json_encode, is this supposed to create an array in javascript with the same key and values as the `$phpArray`?Thanks, but this doesn't help me with my main problem with languages.
SoLoGHoST
@SoLoGHoST: Yes it does. I agree that the answer is a bit OT...
nico
Thanks nico, will have to look into this. Cheers :)
SoLoGHoST
I missed the point a bit here :/ misunderstood language.
Dykam
+3  A: 

To properly localize numbers, you'll need much more complex logic. A quick example of roughly what you need to do:

function localizeNumber($num, $lang) {
    switch ($lang) {

        case 'eng':
        case ...
            return number_format($num, 0);

        case 'deu':
        case ...
            return number_format($num, 0, ',', '.');

        case 'jpn':
            // TODO: learn about Kanji numerals and implement custom logic
            // Example: 3000 -> 三千, 30000 -> 三万, 321 -> 三百二十一

        ...

    }
}

There's not much to do for most western cultures, just some have the thousand separator and the decimal point reversed. If you want to translate numbers into non-Arabic numerals, learn how the target numeric system works, then implement your own logic (or look around for existing libraries).

Also note that although Japanese for example have "their own numerals" (borrowed from the Chinese), these days they usually prefer Arabic numbers. It depends on the context and what image you want to convey though, sometimes Chinese numerals are the better choice. This goes to say that there's a whole lot of culture involved which you need to understand first.

deceze
Ok, thanks, this approach makes the most sense to me. I'll have to get the translator's to help and hope that `number_format` will be suitable to use for all languages. Cheers :)
SoLoGHoST
Bytheway, the target languages are all of the languages listed here: http://dream-portal.net/index.php?action=contribute;sa=translate
SoLoGHoST
@SoLoGHoST I think you should be fine with Arabic numerals in pretty much all cases there, unless your translators advise you it'd be better to use "local" numerals. In which case, have fun with Hebrew and Chinese... ;)
deceze
hehe, thanks. Using Arabic numerals will have to do... arggg XD
SoLoGHoST