【问题标题】:Matching Locale from supported languages从支持的语言匹配区域设置
【发布时间】:2016-02-19 23:38:46
【问题描述】:

我无法支持多种语言。我在服务器上有一个多种语言的字符串列表,我正在运行时下载它们。对于每个字符串,都有一个支持的语言列表,格式为 "en-US", "nl-NL" 等。

我希望我的应用为默认语言环境选择最匹配的翻译。如果没有最好的,应该选择en-US

iOS 的一个例子是here

您知道可以帮助我的解决方案或库吗?

【问题讨论】:

    标签: android ios xml localization


    【解决方案1】:

    您可以使用内置的 Locale 类:

    Locale l = new Locale("", "CH");
    System.out.println(l.getDisplayCountry());
    

    例如打印“Switzerland”。请注意,我没有提供语言。

    因此,您可以为反向查找做的是从可用国家/地区构建地图:

    public static void main(String[] args) throws InterruptedException {
    Map<String, String> countries = new HashMap<>();
    for (String iso : Locale.getISOCountries()) {
    Locale l = new Locale("", iso);
    countries.put(l.getDisplayCountry(), iso);
      }
    
    
     System.out.println(countries.get("Switzerland"));
     System.out.println(countries.get("Andorra"));
     System.out.println(countries.get("Japan"));
     }
    

    在这里你可以看到更多。here

    【讨论】:

    • 这如何为我提供最匹配的语言环境?如果我的语言环境是 nl-BE 并且没有 nl-BE 它应该返回 nl-NL,因为那是最好的匹配。国家代码我无法将 BE 与 NL 匹配
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 2012-12-18
    • 2021-04-27
    相关资源
    最近更新 更多