本地化字符串列表的正确方法是什么?我知道分隔符可以本地化为逗号或分号,但是连词是否本地化?如果是这样,任意长度列表的格式字符串会是什么样子?
示例
“蝙蝠、猫和狗”。我可以根据区域设置使用分隔符并构建列表,如下所示:
LIST := UNIT
LISTMID := UNIT SEPARATOR UNIT
LISTMID := LISTMID SEPARATOR UNIT
LIST := UNIT CONJUNCTION UNIT
LIST := LISTMID CONJUNCTION UNIT
我是否必须针对每种语言制定此规则?有什么图书馆可以帮助解决这个问题吗?
我来这里寻找同一问题的答案,最后进行了更多谷歌搜索,发现了这个:http://icu-project.org/apiref/icu4j/com/ibm/icu/text/ListFormatter.html
该类采用参数
two
、start
、middle
和 end
:
所以,对于英语来说,那就是:
- TWO := "{0} and {1}"
- START := "{0}, {1}"
- MIDDLE := "{0}, {1}"
- END := "{0} and {1}"
我写了一个快速的 Lua 演示来展示我想象的它是如何工作的:
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))
将其适用于其他语言应该很简单。
Intl 有一个列表格式,在 MDN 上记录了 JavaScript:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat