使可变长度列表可翻译 [英] Making a variable length list translatable
问题描述
我想显示一个数组中的值列表,并使其可翻译.我选择的翻译方法是gettext.我要尝试执行的一个示例是将可变长度数组变成:
I want to display a list of values from an array, and have it be translatable. My translation method of choice is gettext. An example of what I am trying to do is to turn a variable-length array like:
$array = ('apple', 'plum', 'watermelon', [...]);
放入一个字符串,例如 apple,李子和西瓜
.
into a string like apple, plum and watermelon
.
仅用英语进行操作不会有问题,但是使其可翻译是一个问题,因为其他语言不一定会使用逗号来分隔值和/或在倒数第二个之间可能没有逗号而没有单词"and"和最后的值.如果我知道该数组始终有3个值,那么我可以轻松地做到这一点:
Doing it only in English would not be a problem, but making it translatable is, because other languages may not necessarily use a comma to separate values and/or may not have the word "and" without a comma between the second last and last values. If I knew that the array always had 3 values, I could easily just do:
sprintf(gettext("%s, %s and %s"), $array[0], $array[1], $array[2]);
问题在于数组的长度可以变化.如何从可变长度数组制作可翻译列表?
The problem is that the array can vary in length. How can I make a translatable list from a variable-length array?
修改:
也许我没有说清楚,但是其他语言可能会以不同的方式处理列表.例如,一种语言可能会显示诸如 1、2、3、4
之类的列表,另一种语言可能会显示诸如 1、2、3、4
之类的列表,另一种可能具有额外的内容列表之前和之后,等等.
Perhaps I didn't make myself clear, but other languages may handle lists differently. For example a language might display the list like 1 and 2, 3, 4
, another one might display it like 1, 2, 3, 4
, another might have extra things before and after the list, etc.
推荐答案
我想到的一种方式是这样的:
One way I thought of doing this would be like this:
function array_to_str($array) {
$two_values = _("%s and %s");
$three_values = _("%s, %s and %s");
$separator = _(", ");
if (count($array) == 1) {
return $array[0];
}
if (count($array) == 2) {
return sprintf($two_values, $array[0], $array[1]);
}
$prev = '';
for ($i = 0; $i < count($array) - 2; $i++) {
$prev .= $array[$i];
if ($i < count($array) - 3) {
$prev .= $separator;
}
}
return sprintf(
$three_values,
$prev,
$array[count($array) - 2],
$array[count($array) - 1]
);
}
$arrays[] = array('apple');
$arrays[] = array('apple', 'plum');
$arrays[] = array('apple', 'plum', 'watermelon');
$arrays[] = array('apple', 'plum', 'watermelon', 'lemon');
foreach ($arrays as $array) {
echo array_to_str($array) . PHP_EOL;
}
这将输出:
apple
apple and plum
apple, plum and watermelon
apple, plum, watermelon and lemon
如果该语言使用列表格式 1和2、3、4、5、6
,则翻译人员可以通过翻译来克服这一问题
If the language uses the list format 1 and 2, 3, 4, 5, 6
, the translator could then overcome this by translating
$three_values = "%3$s and %2$s, %1$s";
不幸的是,这样会导致列表混乱.该字符串将输出 6和5、1、2、3、4
.
Unfortunately doing it like this, the list will be out of order. The string would output 6 and 5, 1, 2, 3, 4
.
但是,通过Google翻译当前定义的语言,没有没有将和"的位置更改为倒数第二个位置.从右到左的语言在从左到右阅读时将其放在第一个元素之后,但是由于它是从右到左,因此实际上处于正确(倒数第二个)位置.
However, going through the languages currently defined in Google Translate, there are none that change the position of the "and" to be anything but the second last position. The right-to-left languages put it after the first element when reading left to right, but since it is right to left, it is actually in the correct (penultimate) position.
使用此方法还可以使您克服以非标准方式显示其列表的语言:
Using this method would also allow you to overcome languages that display their lists in a non standard way:
(Korean) 1, 2, 3, 4 <-- only commas are used
(Italian) 1, 2, 3 e 4, <-- extra comma at the end
(Chinese) 1,2,3和4的 <-- extra 的 at the end
(Hungarian) Az 1., 2., 3. és 4. <-- extra A at the beginning for 2 numbers, Az at the beginning for 3 or more numbers
使用上面的函数可以完成这些操作中的最后一个:
The last of these could be done using the above function like this:
$two_values = "A %s és %s";
$three_values = "Az %s, %s és %s";
$separator = ", ";
假设您的电话号码格式正确,则会输出:
Assuming your numbers are formatted correctly, this will output:
A 1. és 2.
Az 1., 2. és 3.
Az 1., 2., 3. és 4.
这篇关于使可变长度列表可翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!