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

由网友(不许回头)分享简介:应该是什么使不同语言的字符串,最好的方法?我有这个问题,我想显示诸如月,月,年,年的字符串。目前,我正在3种语言,我知道:西班牙语,英语和波兰语。对于英语和西班牙语这是直线前进。但是,例如,在抛光年可以成为阔'(号码2后 - 4)或纬度(后从5号)。我想就加入了这个额外的字符串,使得它在其他语言为空。然而,这让我想到...

应该是什么使不同语言的字符串,最好的方法?我有这个问题,我想显示诸如月,月,年,年的字符串。目前,我正在3种语言,我知道:西班牙语,英语和波兰语。对于英语和西班牙语这是直线前进。但是,例如,在抛光年可以成为阔'(号码2后 - 4)或纬度(后从5号)。我想就加入了这个额外的字符串,使得它在其他语言为空。然而,这让我想到了其他语言,我不知道,可能有更多的差异。这应该是在这种情况下,最好的办法,如果我正在考虑增加更多语言的未来?

what should be the best approach to make strings for different languages? I have this problem, I am trying to display strings such as 'month', 'months', 'year', 'years'. Currently I am working on 3 languages I know: spanish, english and polish. For english and spanish this is straight forward. But for instance, in polish 'years' can become 'lata' (after numbers 2 - 4) or 'lat' (after numbers from 5). I was thinking on adding an extra string for this, and making it empty in the other languages. However this made me think about the other languages I don't know, which might have even more differences. Which should be the best approach in this case, if I am considering adding more languages in the future?

推荐答案

听起来像是你想要的 ChoiceFormat ,或至少使用一个通过的 的MessageFormat

Sounds like you want a ChoiceFormat, or at least using one through a 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();
    }
}

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

In your program, you would of course get the format string from your string file.

阅读全文

相关推荐

最新文章