您的位置 首页 java

Java – 如何实现多语言切换

软件的国际化

软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的、符合来访者阅读习惯的页面或数据。

  国际化(internationalization)又称为 i18n(读法为i18n,据说是因为internationalization(国际化)这个单词从i到n之间有18个英文字母,i18n的名字由此而来)。

软件实现国际化,需具备以下两个特征:  

1、对于程序中固定使用的文本元素,例如菜单栏、导航条等中使用的文本元素、或错误提示信息,状态信息等,需要根据来访者的地区和国家,选择不同语言的文本为之服务。  

2、对于程序动态产生的数据,例如(日期,货币等),软件应能根据当前所在的国家或地区的文化习惯进行显示。

Java中如何实现国际化

对于软件中的菜单栏、导航条、错误提示信息,状态信息等这些固定不变的文本信息,可以把它们写在一个properties文件中,并根据不同的国家编写不同的properties文件。这一组properties文件称之为一个资源包。

  • 创建资源包和资源文件

一个资源包中的每个资源文件都必须拥有共同的名字。除了名字,每个资源文件的名称中还必须有标识其本地信息的附加部分。例如:一个资源包的名字为“ application ”,则与中文、英文环境相对应的资源文件名则为: “application_zh.properties” “application_en.properties”

IDEA中创建一个资源包

每个资源包都应有一个默认资源文件,这个文件不带有标识本地信息的附加部分(上图的application.properties)。若ResourceBundle对象在资源包中找不到与用户匹配的资源文件,它将选择该资源包中与用户最相近的资源文件,如果再找不到,使用系统默认Locale对应的资源文件,如果还找不到,则使用默认资源文件。

  • 资源文件的书写格式

资源文件的内容通常采用”关键字=值”的形式,应用程序根据关键字检索值对应的值进行显示。一个资源包中的所有资源文件的关键字必须相同,值则为相应国家的文字。

例如:

application.properties(默认资源文件):

  username  = User Name
password = Password  

application_en.properties:

 username = User Name(EN)
password = Password(EN)  

application_zh.properties:

 username = 用户名
password = 密码  
  • locale

java.util.Locale类表示地区。每一个Locale对象都代表了一个特定的地理、政治和文化地区。它的实现基于规范 。

Locale对象的3种常用创建方式:

1) 获取默认Locale

Locale locale = Locale.getDefault();

 public static Locale getDefault() {
        // do not synchronize this method - see 4071298
        return defaultLocale;
 } 
private static volatile Locale defaultLocale = initDefault();

private static Locale initDefault() {
        String language, region, script,  country , variant;
        Properties props = GetPropertyAction.privilegedGetProperties();
        language = props.getProperty("user.language", "en");
        // for compatibility, check for old user.region property
        region = props.getProperty("user.region");
        if (region != null) {
            // region can be of form country, country_variant, or _variant
            int i = region.indexOf('_');
            if (i >= 0) {
                country = region.substring(0, i);
                variant = region.substring(i + 1);
            } else {
                country = region;
                variant = "";
            }
            script = "";
        } else {
            script = props.getProperty("user.script", "");
            country = props.getProperty("user.country", "");
            variant = props.getProperty("user.variant", "");
        }

        return getInstance(language, script, country, variant,
                getDefaultExtensions(props.getProperty("user.extensions", ""))
                    .orElse(null));
    }  

2) 直接使用Locale的静态对象

Locale locale = Locale.SIMPLIFIED_CHINESE; //中国大陆

3) 通过Locale的构造函数创建Locale对象

Locale local = new Locale(“zh”, “CN”);

查看默认locale相关信息:

     private static void testDefaultLocale() {
        Locale defaultLocale = Locale.getDefault();
        System.out.println("default locale:" + defaultLocale.toString());
        System.out.println("getLanguage:" + defaultLocale.getLanguage());
        System.out.println("getDisplayLanguage:" + defaultLocale.getDisplayLanguage());
        System.out.println("getISO3Language:" + defaultLocale.getISO3Language());
        System.out.println("getCountry:" + defaultLocale.getCountry());
        System.out.println("getISO3Country:" + defaultLocale.getISO3Country());
        System.out.println("getDisplayCountry:" + defaultLocale.getDisplayCountry());
        System.out.println("getDisplayName:" + defaultLocale.getDisplayName());
        System.out.println("getScript:" + defaultLocale.getScript());
        System.out.println("getDisplayScript:" + defaultLocale.getDisplayScript());
        System.out.println("getVariant:" + defaultLocale.getVariant());
        System.out.println("getDisplayVariant:" + defaultLocale.getDisplayVariant());
    }  

输出结果:

 default locale:zh_CN
getLanguage:zh
getDisplayLanguage:中文
getISO3Language:zho
getCountry:CN
getISO3Country:CHN
getDisplayCountry:中国
getDisplayName:中文 (中国)
getScript:
getDisplayScript:
getVariant:
getDisplayVariant:  
  • 通过ResourceBundle类来读取资源

jdk提供了java.util.Locale来代表不同,一个ResourceBundle 类用于描述一个资源包,并且 ResourceBundle类提供了getBundle,这个方法可以根据来访者的国家地区自动获取与之对应的资源文件予以显示。

 ResourceBundle myResources =ResourceBundle.getBundle(basename, new Locale("zh","CN"));  

basename为资源包名字(必须为完整路径)。如果与该locale对象匹配的资源包子类找不到,将会使用系统的默认locale(Locale.getDefault() )来查找资源文件,如果还找不到,则选用默认资源文件。加载资源文件后, 程序就可以调用ResourceBundle 实例对象的 getString 方法获取指定的资源信息名称所对应的值。

 String value =  myResources.getString("key");  

通过指定的Locale来获取资源信息名称对应的值:

 private static void testResourceBundle() {
        //中文Locale
        ResourceBundle chineseBundle = ResourceBundle.getBundle("i18n.application", Locale.CHINESE);
        System.out.println("username:" + chineseBundle.getString("username"));
        System.out.println("password:" + chineseBundle.getString("password"));

        //英文Locale
        ResourceBundle englishBundle = ResourceBundle.getBundle("i18n.application", Locale.ENGLISH);
        System.out.println("username:" + englishBundle.getString("username"));
        System.out.println("password:" + englishBundle.getString("password"));

        //使用系统默认的Locale(Locale.getDefault())
        System.out.println("Default Locale:" + Locale.getDefault());
        ResourceBundle defaultBundle = ResourceBundle.getBundle("i18n.application");
        System.out.println("username:" + defaultBundle.getString("username"));
        System.out.println("password:" + defaultBundle.getString("password"));
    }  

输出结果:

 username:用户名
password:密码
username:User Name(EN)
password:Password(EN)
Default Locale:zh_CN
username:用户名
password:密码  

WEB应用中实现固定文本的国际化

如下所示:

 如下所示:<%@ page language="java"  import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
  <head>
    <title>国际化(i18n)测试</title>
  </head>
  <%
      //加载i18n资源文件,request.getLocale()获取访问用户所在的国家地区
      ResourceBundle myResourcesBundle = ResourceBundle.getBundle("me.gacl.i18n.resource.myproperties",request.getLocale());
  %>
  <body>
        <form action="" method="post">
            <%=myResourcesBundle.getString("username")%>:<input type="text" name="username"/>
            <%=myResourcesBundle.getString("password")%>:<input type="password" name="password"/>
            <input type="submit" value="<%=myResourcesBundle.getString("submit")%>">
        </form>
  </body>
</html>  

运行结果:

浏览器语言是中文环境下的显示效果:

浏览器语言是英文环境下的显示效果:

同样一个页面,在不同语言环境的浏览器下显示出了不同的语言文字效果,这样就实现了固定文本的国际化。

IE浏览器切换使用语言:工具→Internet选项

DateFormat类(日期格式化)

DateFormat 类可以将一个日期/时间对象格式化为表示某个国家地区的日期/时间字符。该类还定义了一些用于描述日期/时间的显示模式的 int 型的常量,包括FULL, LONG , MEDIUM, DEFAULT, SHORT 实例化 DateFormat对象时,可以使用这些常量,控制日期/时间的显示长度。

该类的重要方法:

getDateInstance(int style, Locale aLocale) :以指定的日期显示模式和本地信息来获得DateFormat实例对象,该实例对象不处理时间值部分。  

getTimeInstance(int style, Locale aLocale): 以指定的时间显示模式和本地信息来获得DateFormat实例对象,该实例对象不处理日期值部分。  

getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale): 以单独指定的日期显示模式、时间显示模式和本地信息来获得DateFormat实例对象。

format:将date对象格式化为符合某个本地环境习惯的字符串。  

parse:将字符串解析为日期/时间对象  

注意:DateFormat类不是线程安全的,每个线程都应该创建自己的 DateFormat 实例对象。

如何结合Locale来使用DateFormat类:

 private static void testDateFormat() {
        // 获取当前时间
        Date now = new Date();

        // 输出日期部分
        DateFormat df = DateFormat.getDateInstance(DateFormat.FULL,Locale.US);
        String result = df.format(now);
        System.out.println("英文日期:" + result);
        df = DateFormat.getDateInstance(DateFormat.FULL,Locale.CHINA);
        result = df.format(now);
        System.out.println("中文日期:" +result);

        // 输出时间部分
        df = DateFormat.getTimeInstance(DateFormat.FULL, Locale.US);
        result = df.format(now);
        System.out.println("英文时间:" + result);
        df = DateFormat.getTimeInstance(DateFormat.FULL, Locale.CHINA);
        result = df.format(now);
        System.out.println("中文时间:" + result);

        // 输出日期和时间
        df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG,Locale.US);
        result = df.format(now);
        System.out.println("英文日期时间:" + result);
        df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG,Locale.CHINA);
        result = df.format(now);
        System.out.println("中文日期时间:" + result);

        // 把字符串反向解析成一个date对象
        String s = "2020/8/5 CST 上午12:29:55";
        df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG,Locale.CHINA);
        Date date = null;
        try {
            date = df.parse(s);
            System.out.println("date:" + date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }  

输出结果:

 英文日期:Wednesday, August 5, 2020
中文日期:2020年8月5日星期三
英文时间:12:33:26 AM China Standard Time
中文时间:中国标准时间 上午12:33:26
英文日期时间:8/5/20, 12:33:26 AM CST
中文日期时间:2020/8/5 CST 上午12:33:26
date:Wed Aug 05 00:29:55 CST 2020  

NumberFormat类(数字格式化)

NumberFormat类可以将一个数值格式化为符合某个国家地区习惯的数值字符串,也可以将符合某个国家地区习惯的数值字符串解析为对应的数值

NumberFormat类的方法:

format 方法:将一个数值格式化为符合某个国家地区习惯的数值字符串

parse 方法:将符合某个国家地区习惯的数值字符串解析为对应的数值。

getNumberInstance(Locale locale): 以参数locale对象所标识的本地信息来获得具有多种用途的NumberFormat实例对象

getIntegerInstance(Locale locale): 以参数locale对象所标识的本地信息来获得处理整数的NumberFormat实例对象

getCurrencyInstance(Locale locale): 以参数locale对象所标识的本地信息来获得处理货币的NumberFormat实例对象

getPercentInstance(Locale locale): 以参数locale对象所标识的本地信息来获得处理百分比数值的NumberFormat实例对象

getCompactNumberInstance(Locale locale, NumberFormat.Style formatStyle):

Locale代表着本地语言特性,比如在US locale中,10000可以表示为“10K”,而在China locale中,10000中就变成了“1万”;Style有两种类型,short和long。比如说10000的short表示是“10K”,而它的long表示是“10 thousand”。

使用案例:

 private static void testNumberFormat() {
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
        String result = nf.format(1098);
        System.out.println("China currency:" + result);

        nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
        Number n = null;
        try {
            n = nf.parse("¥1,092.00");
            System.out.println("currency parse:" + n.doubleValue() );
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //使用默认locale[ Locale.getDefault(Locale.Category.FORMAT) ]
        nf = NumberFormat.getPercentInstance();
        System.out.println("percent:" + nf.format(0.65));

        nf = NumberFormat.getNumberInstance(Locale.CHINA);
        //设置小数位的精度
        nf.setMaximumFractionDigits(5);
        System.out.println("number:" + nf.format(100000012.123491234));

        nf = NumberFormat.getIntegerInstance(Locale.US);
        System.out.println("integer:" + nf.format(23100012));

        nf = NumberFormat.getCompactNumberInstance(Locale.CHINA, NumberFormat.Style.LONG);
        System.out.println("china compact long:" + nf.format(120131312321312L)) ;
        nf = NumberFormat.getCompactNumberInstance(Locale.CHINA, NumberFormat.Style.SHORT);
        System.out.println("china compact short:" + nf.format(120131312321312L)) ;

        nf = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.LONG);
        System.out.println("us compact long:" + nf.format(120131312321312L)) ;
        nf = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
        System.out.println("us compact short:" + nf.format(120131312321312.001d)) ;
    }  

输出结果:

 China currency:¥1,098.00
currency parse:1092.0
percent:65%
number:100,000,012.12349
integer:23,100,012
china compact long:120万亿
china compact short:120万亿
us compact long:120 trillion
us compact short:120 trillion  

MessageFormat(文本格式化)

如果一个字符串中包含了多个与国际化相关的数据,可以使用MessageFormat类对这些数据进行批量处理。  

例如:At 12:30 pm on jul 3,1998, a train destroyed 99 houses and caused $1000000 of damage  

以上字符串中包含了时间、数字、货币等多个与国际化相关的数据,对于这种字符串,可以使用MessageFormat类对其国际化相关的数据进行批量处理。  

MessageFormat 类如何进行批量处理呢?    

a. MessageFormat类允许开发人员用占位符替换掉字符串中的敏感数据(即国际化相关的数据)。    

b. MessageFormat类在格式化输出包含占位符的文本时,messageFormat类可以接收一个参数数组,以替换文本中的每一个占位符。

模式字符串与占位符

模式字符串:

  At {0} on {1},a train destroyed {2} houses and caused {3} of damage

  字符串中的{0}、{1}、{2}、{3}就是占位符

占位符的三种书写方式

{argumentIndex}: 0-9 之间的数字,表示要格式化对象数据在参数数组中的索引号;

{argumentIndex,formatType}: 参数的格式化类型;

{argumentIndex,formatType,FormatStyle}: 格式化的样式,它的值必须是与格式化类型相匹配的合法模式、或表示合法模式的字符串。

格式化模式字符串

  1、实例化MessageFormat对象,并装载相应的模式字符串。

  2、使用format(object obj[])格式化输出模式字符串,参数数组中指定占位符相应的替换对象。

使用案例:

     private static void testMessageFormat() {
        //模式字符串
        String pattern = "On {0}, a train destroyed {1} houses and caused {2,number,currency} of damage.";
        //实例化MessageFormat对象,并装载相应的模式字符串
        MessageFormat format = new MessageFormat(pattern, Locale.US);
        //格式化模式字符串,参数数组中指定占位符相应的替换对象
        String result = format.format(new Object[]{new Date(), 99, 100000000});
        System.out.println("US format message:"+ result);

        //实例化MessageFormat对象,并装载相应的模式字符串
        format = new MessageFormat(pattern, Locale.CHINA);
        //格式化模式字符串,参数数组中指定占位符相应的替换对象
        result = format.format(new Object[]{new Date(), 99, 100000000});
        System.out.println("China format message:"+ result);
    }  

输出结果:

 US format message:On 8/5/20, 12:45 PM, a train destroyed 99 houses and caused $100,000,000.00 of damage.
China format message:On 2020/8/5 下午12:45, a train destroyed 99 houses and caused ¥100,000,000.00 of damage.  

参考资料

文章来源:智云一二三科技

文章标题:Java – 如何实现多语言切换

文章地址:https://www.zhihuclub.com/181908.shtml

关于作者: 智云科技

热门文章

网站地图