Java 国际化编程,Locale 和 ResourceBundle 的使用

版权声明:如果喜欢的话,可以撩我哟,此处没有联系方式,想要就自己找哈。 https://blog.csdn.net/qq_39384184/article/details/85243447

国际化(internationalization,I18N),本地化(localization,L10N)

国际化意味着一个软件可同时支持多种语言,而本地化需要为特定区域或国家的用户提供不同的软件。

Locale 类

Locale 类是用来标识本地化消息的重要工具类,其特定对象表示某一特定的地理、政治或文化区域。

import java.util.Locale;

public class LocaleDemo {

    public static void main(String[] args) {
        Locale[] localeList = Locale.getAvailableLocales();
        for (int i = 0; i < localeList.length; i++) {
            System.out.println(localeList[i].getDisplayCountry()
                    + "=" + localeList[i].getCountry() + " "
                    + localeList[i].getDisplayLanguage()
                    + "=" + localeList[i].getLanguage());
        }
        Locale locale = Locale.getDefault();
        System.out.println("Language        : " + locale.getLanguage());
        System.out.println("Country         : " + locale.getCountry());
        System.out.println("DisplayLanguage : " + locale.getDisplayLanguage());
        System.out.println("DisplayCountry  : " + locale.getDisplayCountry());
        System.out.println("locale : " + locale);
        // 重新设置默认的 Locale 信息
        Locale newLocale = new Locale("en", "US");
        Locale.setDefault(newLocale);
        locale = Locale.getDefault();
        System.out.println("Language        : " + locale.getLanguage());
        System.out.println("Country         : " + locale.getCountry());
        System.out.println("DisplayLanguage : " + locale.getDisplayLanguage());
        System.out.println("DisplayCountry  : " + locale.getDisplayCountry());
        System.out.println("locale : " + locale);
    }

}

ResourceBundle 类

ResourceBundle 类用于加载国家和语言资源包。资源文件的内容是以“键值”对组成,资源文件的命名为 baseName_language_country.properties,其中 language 和 country 必须是 Java 支持的语言和国家,不可随意变化。

Native to Ascii:http://tool.oschina.net/encode?type=3

  • myres_en_US.properties
title=Login
name=Username

  • myres_zh_CN.properties
title=\u767b\u5f55
name=\u7528\u6237\u540d

  • ResourceBundleDemo
import java.util.Locale;
import java.util.ResourceBundle;

public class ResourceBundleDemo {

    public static void main(String[] args) {
//        使用默认的语言和国家
        ResourceBundle resb1 = ResourceBundle.getBundle("myres");
        System.out.println(resb1.getString("title"));
        System.out.println(resb1.getString("name"));
        System.out.println("-----------------------------");

        Locale localeEn = new Locale("en", "US");
        ResourceBundle resb2 = ResourceBundle.getBundle("myres", localeEn);
        System.out.println(resb2.getString("title"));
        System.out.println(resb2.getString("name"));
        System.out.println("-----------------------------");

        Locale localeZh = new Locale("zh", "CN");
        ResourceBundle resb3 = ResourceBundle.getBundle("myres", localeZh);
        System.out.println(resb1.getString("title"));
        System.out.println(resb1.getString("name"));
        System.out.println("-----------------------------");
    }

}

猜你喜欢

转载自blog.csdn.net/qq_39384184/article/details/85243447