如何使用真正的多语言字符串做一个应用程序?

Jan*_* S. 6 java multilingual android

什么是为不同语言制作字符串的最佳方法?我有这个问题,我试图显示诸如'月','月','年','年'之类的字符串.目前我正在研究我所熟知的3种语言:西班牙语,英语和波兰语.对于英语和西班牙语,这是直截了当的.但是,例如,在波兰'年'可以成为'lata'(在数字2 - 4之后)或'lat'(在5之后的数字之后).我正在考虑为此添加一个额外的字符串,并在其他语言中将其设置为空.然而,这让我想到了我不知道的其他语言,这可能会有更多的差异.如果我考虑将来添加更多语言,那么在这种情况下哪个应该是最好的方法?

gus*_*afc 3

听起来你想要一个ChoiceFormat,或者至少使用一个到一个MessageFormat

\n\n
public static void main(String... args) {\n    String[] formats = { \n        // NOTE - In a real app, you\'d fetch the format strings from a language,\n        // file, not hard-code them in your program. Obviously.\n        "{0,number} {0,choice,0#years|1#year|1<years}", // english\n        "{0,number} {0,choice,0#a\xc3\xb1os|1#a\xc3\xb1o|1<a\xc3\xb1os}", // spanish\n        "{0,number} {0,choice,1#[fewer than 2]|2#lata|4<lat}", // polish\n        "{0,number} \xc3\xa5r", // swedish - singular and plural forms look the same!\n    };\n    int[] years = {0, 1, 2, 3, 4, 5, 6};\n    for (int year : years) {\n        for (String format : formats) {\n            System.out.println(MessageFormat.format(format, year));\n        }\n        System.out.println();\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

在您的程序中,您当然会format从字符串文件中获取字符串。

\n