如何定位数字? [英] How to localize numerals?

查看:204
本文介绍了如何定位数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在使用数学方程来输出数字,但是,我需要它与所有语言兼容.当前,所有语言字符串都在一个名为$txt的php数组中,并且该数组的每个键都被该语言调用.我正在输出以下内容:Column 1Column 2Column 3等,以及Row 1Row 2Row 3等.计算是通过php和javascript完成的,所以我想知道关于仅支持数字的所有语言的最佳方法.

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.

我不做翻译,其他人做,但是我需要能够指出它,或者是定义语言的php变量$ txt,或者,因为计算也是通过javascript完成的,我需要以某种方式将其存储在其中.我正在考虑存储这样的东西:

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>';

与javascript函数相比,它可以为每个数字抓取正确的字符串,如下所示:

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?

谢谢:)

推荐答案

要正确定位数字,您将需要更复杂的逻辑.大致说明您需要做什么的简单示例:

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.

这篇关于如何定位数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆