清单的本地化 [英] Localization of lists
问题描述
对字符串列表进行本地化的正确方法是什么?我知道分隔符可以被本地化为逗号或分号,但是连词是否被本地化了?如果是这样,我的任意长度列表的格式字符串将是什么样?
What is the right way to localize a list of strings? I know that the separator can be localized to a comma or a semi-colon but does the conjunction get localized? If so, what would my format string for an arbitrary length list look like?
示例
蝙蝠,猫和狗".我可以按区域设置使用分隔符,并按以下方式构造LIST:
"Bat, Cat and Dog". I could use the separator as per locale and construct the LIST as the following:
LIST := UNIT
LISTMID := UNIT SEPARATOR UNIT
LISTMID := LISTMID SEPARATOR UNIT
LIST := UNIT CONJUNCTION UNIT
LIST := LISTMID CONJUNCTION UNIT
我是否必须根据每种语言制定此规则?有没有可用的库来帮助解决这个问题?
Would I have to craft this rule per language? Any libraries available to help with this?
推荐答案
我来到这里寻找相同问题的答案,并最终进行了更多的谷歌搜索,结果发现:
I came here looking for an answer to the same question, and ended up doing more googling, which found this: http://icu-project.org/apiref/icu4j/com/ibm/icu/text/ListFormatter.html
该类采用参数two
,start
,middle
和end
:
- two-两个项目的字符串,第一个包含{0},第二个包含{1}.
- start-列表项开始的字符串,第一个包含{0},其余包含{1}.
- middle-列表项开头的字符串,其中,列表的第一部分包含{0},列表的其余部分包含{1}.
- end-列表项末尾的字符串,其中,列表的第一部分为{0},最后一项为{1}.
因此,对于英语来说,应该是:
So, for English, that would be:
- TWO := "{0} and {1}"
- START := "{0}, {1}"
- MIDDLE := "{0}, {1}"
- END := "{0} and {1}"
我写了一个简短的Lua演示来说明我的工作原理:
I wrote a quick Lua demonstration for how I imagine this works:
function list_format(words, templates)
local length = #words
if length == 1 then return words[1] end
if length == 2 then
return replace(replace(templates['two'], '{0}', words[1]),
'{1}', words[2])
end
local result = replace(templates['end'], '{1}', words[length])
while length > 3 do
length = length - 1
local mid = replace(templates['middle'], '{1}', words[length])
result = replace(result, '{0}', mid)
end
result = replace(result, '{0}', words[2])
result = replace(templates['start'], '{1}', result)
result = replace(result, '{0}', words[1])
return result
end
function replace(template, index, text)
str, _ = string.gsub(template, index, text)
return str
end
local english = {
["two"] = "{0} and {1}",
["start"] = "{0}, {1}",
["middle"] = "{0}, {1}",
["end"] = "{0} and {1}"
}
print(list_format({"banana"}, english))
print(list_format({"banana", "apple"}, english))
print(list_format({"banana", "apple", "mango"}, english))
print(list_format({"banana", "apple", "mango", "pineapple"}, english))
要使其适应其他语言应该是微不足道的.
It should be trivial to adapt this for other languages.
这篇关于清单的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!