国际化:Java平台下的Locale类

Locale类有如下几个构造器:

Locale(String language)

Locale(String language, String country)

Locale(String language, String country, String variant)

通过构造方法实例化Locale对象:

public class LocaleTest {
    public static void main(String[] args) {

        //创建一个通用英语的locale.
        Locale locale11 = new Locale("en");
        //创建一个加拿大英语的locale.
        Locale locale12 = new Locale("en", "CA");
        //创建一个美式英语的locale,硅谷的英语
        Locale locale13 = new Locale("en", "US", "SiliconValley");

    }
}

Locale类有一些预定义的Locale对象实例:

Locale Name Locale
Locale.CHINA zh_CN
Locale.CHINESE zh
Locale.SIMPLIFIED_CHINESE zh_CN
Locale.TRADITIONAL_CHINESE zh_TW
Locale.PRC zh_CN
Locale.TAIWAN zh_TW
Locale.ENGLISH en
Locale.UK en_GB
Locale.US en_US
Locale.FRANCE fr_FR
Locale.FRENCH fr

以下例子是德国locale实例以英语、德语和法语提供了信息:

public class LocaleTest {

    public static void main(String[] args) {
        Locale deLocale = new Locale("de", "DE");
        Locale frLocale = new Locale("fr", "FR");
        System.out.println("Default language name (default): "
                + deLocale.getDisplayLanguage());
        System.out.println("German language name (German): "
                + deLocale.getDisplayLanguage(deLocale));
        System.out.println("German language name (French): "
                + deLocale.getDisplayLanguage(frLocale));
    }

}

输出结果为:

Default language name (default): German
German language name (German): Deutsch
German language name (French): allemand

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/14/java-internationalization-locale/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
国际化:Java平台下的Locale类
Locale类有如下几个构造器: Locale(String language) Locale(String language, String country) Locale(String language, String country, String variant)……
<<上一篇
下一篇>>
文章目录
关闭
目 录