为不同语言制作字符串的最佳方法应该是什么?我遇到了这个问题,我正在尝试显示诸如“月”、“月”、“年”、“年”之类的字符串。目前我正在研究我知道的 3 种语言:西类牙语、英语和波兰语。对于英语和西类牙语,这很简单。但例如,在波兰语中,“years”可以变成“lata”(在数字 2 - 4 之后)或“lat”(在数字 5 之后)。我正在考虑为此添加一个额外的字符串,并在其他语言中将其设为空。然而,这让我想到了我不知道的其他语言,它们可能有更多差异。在这种情况下,如果我考虑在未来添加更多语言,哪种方法最好?
最佳答案
听起来你想要一个 ChoiceFormat
,或者至少通过 MessageFormat
使用一个:
public static void main(String... args) {
String[] formats = {
// NOTE - In a real app, you'd fetch the format strings from a language,
// file, not hard-code them in your program. Obviously.
"{0,number} {0,choice,0#years|1#year|1<years}", // english
"{0,number} {0,choice,0#años|1#año|1<años}", // spanish
"{0,number} {0,choice,1#[fewer than 2]|2#lata|4<lat}", // polish
"{0,number} år", // swedish - singular and plural forms look the same!
};
int[] years = {0, 1, 2, 3, 4, 5, 6};
for (int year : years) {
for (String format : formats) {
System.out.println(MessageFormat.format(format, year));
}
System.out.println();
}
}
在您的程序中,您当然会从字符串文件中获取 format
字符串。
关于java - 如何用真正的多语言字符串做一个应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3136288/