ResourceBundle 使用

Java ResourceBundle 理解
1. 基础
  • java.util 包下的工具类

  • ResourceBundle 是用来处理properties文件的类

  • ResourceBundle 是用来做国际化的,配合多个properties文件

2. 配置

1539182191512

  • 建立properties文件 (自定义名字_地区字符.properties文件)

    • 比如:

      mySource_en_US.properties
      mySource_zh_CN.properties

      mySource.properties (默认)

      mySource_en_US.properties
      title=welcome
      
      mySource_zh_CN.properties
      title=欢迎
      
      mySource_en_US.properties
      title=欢迎你
      
3. 使用

public class TestResourceBundle {
    public static void main(String[] args) {
        //设置地区美国
        Locale.setDefault(Locale.US);
        System.out.println("US " + TestResourceBundle.getValue());
        //设置地区中国
        Locale.setDefault(Locale.SIMPLIFIED_CHINESE);
        System.out.println("CN " + TestResourceBundle.getValue());
        //没有配置的地区
        Locale.setDefault(Locale.GERMAN);
        System.out.println("Other " + TestResourceBundle.getValue());
    }

    private static String getValue() {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("sourceBundle.mySource");
        String value = resourceBundle.getString("title");
        value = new String(value.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        return value;
    }
}

输出结果:

US welcome
CN 欢迎
Other 欢迎你

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值