Localization 名单本地化
本地化字符串列表的正确方法是什么?我知道分隔符可以本地化为逗号或分号,但连词是否可以本地化?如果是这样,我的任意长度列表的格式字符串是什么样的 范例 “蝙蝠、猫和狗”。我可以根据区域设置使用分隔符,并按照以下方式构建列表:Localization 名单本地化,localization,internationalization,Localization,Internationalization,本地化字符串列表的正确方法是什么?我知道分隔符可以本地化为逗号或分号,但连词是否可以本地化?如果是这样,我的任意长度列表的格式字符串是什么样的 范例 “蝙蝠、猫和狗”。我可以根据区域设置使用分隔符,并按照以下方式构建列表: LIST := UNIT LISTMID := UNIT SEPARATOR UNIT LISTMID := LISTMID SEPARATOR UNIT LIST := UNIT CONJUNCTION UNIT LIST := LISTMID CONJUNCTION UN
LIST := UNIT
LISTMID := UNIT SEPARATOR UNIT
LISTMID := LISTMID SEPARATOR UNIT
LIST := UNIT CONJUNCTION UNIT
LIST := LISTMID CONJUNCTION UNIT
我需要为每种语言制定这条规则吗?有没有图书馆可以帮上忙?我来这里是为了找到同一个问题的答案,最后在谷歌上搜索了更多的内容,结果发现: 该类采用参数
two
,start
,middle
,以及end
:
- two-两个项目的字符串,第一个项目包含{0},第二个项目包含{1}
- start-列表项开头的字符串,第一个项包含{0},其余项包含{1}
- 中间-列表项开头的字符串,列表的第一部分包含{0},其余部分包含{1}
- end—列表项结尾的字符串,列表的第一部分包含{0},最后一项包含{1}
- 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))
将其应用于其他语言应该很简单。虽然这适用于英语,但它是否适用于其他语言,尤其是从右到左的语言?我没有RTL语言的经验,但我在研究时发现的“奇怪”示例(韩语、意大利语、匈牙利语、汉语)都将得到支持: