SoLoGHoST
SoLoGHoST

Reputation: 2689

How to localize numerals?

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

Upvotes: 1

Views: 1051

Answers (3)

Yasen
Yasen

Reputation: 3428

PHP NumberFormatter is good to use http://php.net/manual/en/class.numberformatter.php

Upvotes: 1

deceze
deceze

Reputation: 522042

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.

Upvotes: 4

Dykam
Dykam

Reputation: 10290

For Php/Js communication, just use json:

echo "var jsArray = " + json_encode($phpArray);

Upvotes: 1

Related Questions