Java学习路线-21:国际化Locale、ResourceBundle、MessageFormat

第11 章 : 国际化程序实现

42 国际化程序实现原理

统一程序代码,根据不同国家实现不同语言描述
需要解决问题:
1、定义保存文字信息
2、根据不同区域语言编码读取文件信息

43 Locale类

Locale类:专门描述区域和语言编码的类

构造方法

public Locale(String language)
public Locale(String language, String country)

国家语言代码
中文:zh_CN
美国:en_US

使用示例

import java.util.Locale;

class Demo {

    public static void main(String[] args){

        Locale loc = new Locale("zh", "CN");
        System.out.println(loc);
        // zh_CN

    }
}

读取本地默认环境

Locale loc = Locale.getDefault();
System.out.println(loc);
// zh_CN

使用常量

Locale loc = Locale.CHINA;
System.out.println(loc);
// zh_CN

44 ResourceBundle读取资源文件

public static final ResourceBundle getBundle(String baseName)
// baseName 没有后缀的文件名

资源文件 message.properties

info="这是消息"

读取实例

import java.io.UnsupportedEncodingException;
import java.util.ResourceBundle;

class Demo {

    public static void main(String[] args) throws UnsupportedEncodingException {

        ResourceBundle bundle = ResourceBundle.getBundle("message");

        // 解决中文乱码问题
        String message = new String(bundle.getString("info").getBytes("ISO-8859-1"), "utf-8");

        System.out.println(message);
        // "这是消息"

    }
}

45 实现国际化程序开发

CLASSPATH 下建立文件

cat Message.properties
info=默认资源 

cat Message_zh_CN.properties
info=中文资源 

cat Message_en_US.properties
info=英文资源

执行程序会读取中文资源

import java.io.UnsupportedEncodingException;
import java.util.ResourceBundle;

class Demo {

    public static void main(String[] args) throws UnsupportedEncodingException {

        ResourceBundle bundle = ResourceBundle.getBundle("message");

        // 解决中文乱码问题
        String message = new String(bundle.getString("info").getBytes("ISO-8859-1"), "utf-8");

        System.out.println(message);
        // 中文资源
    }
}

getBundle方法默认加载Locale.getDefault()

public static final ResourceBundle getBundle(String baseName)
    {
        return getBundleImpl(baseName, Locale.getDefault(),
                             getLoader(Reflection.getCallerClass()),
                             getDefaultControl(baseName));
    }

使用重载函数,读取英文资源

Locale locale = Locale.US;
ResourceBundle bundle = ResourceBundle.getBundle("message", locale);

// 解决中文乱码问题
String message = new String(bundle.getString("info").getBytes("ISO-8859-1"), "utf-8");

System.out.println(message);
// 英文资源

如果没有对应区域编码的资源文件,读取默认资源
读取流程:

指定区域的资源文件 > 默认的本地资源文件 > 公共的资源文件

46 格式化文本显示

格式化方法

public class MessageFormat extends Format {
    public static String format(String pattern, Object ... arguments)
}

代码示例


import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

class Demo {

    public static void main(String[] args) {
        // 占位符{}
        String message = "hello {0} date: {1}";
        System.out.println(MessageFormat.format(message, "admin", new SimpleDateFormat("yyyy-MM-dd").format(new Date())));
        // hello admin date: 2019-11-18
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值