JavaWeb 国际化

来源:互联网 发布:程序员简历怎么写 编辑:程序博客网 时间:2024/05/17 22:33

原作者:尚硅谷-佟刚


src 目录下资源文件

这里写图片描述

中文国际化文件(date=日期,salary=工资)
这里写图片描述

英文国际化文件
这里写图片描述


测试代码

package com.atweihai.nationalization;import java.text.DateFormat;import java.text.MessageFormat;import java.text.NumberFormat;import java.util.Date;import java.util.Locale;import java.util.ResourceBundle;import org.junit.Test;public class NationalizationTest {    //国际化    @Test    public void testNationalization(){        Locale locale=Locale.US;        //获取描述资源文件的ResourceBundle对象        ResourceBundle resourceBundle=ResourceBundle.getBundle("i18n", locale);        String salaryLabel=resourceBundle.getString("salary");        String dateLabel=resourceBundle.getString("date");        double salary=12345.678;        //获取格式化货币的NumberFormat 对象        NumberFormat numberFormat=NumberFormat.getCurrencyInstance(locale);        String salaryStr=numberFormat.format(salary);        Date date=new Date();        //获取格式化日期DateFormat对象        DateFormat dateFormat=DateFormat.getDateInstance(DateFormat.MEDIUM, locale);        String dateStr=dateFormat.format(date);        //设置模式字符串message        String message="{0}:{1} ,{2}:{3}";        //用MessageFormat 的静态format 方法,获取 参数替换模式字符串中占位符后的字符串        String messageFormat=MessageFormat.format(message,salaryLabel,salaryStr,dateLabel,dateStr);        System.out.println(messageFormat);    }}

控制台打印

这里写图片描述

这里写图片描述

原创粉丝点击