Java 国际化简单使用

Java国际化使用

Locale

是表示语言和国际的类,是国际化应用的基础
语言完整版:http://www.loc.gov/standards/iso639-2/php/English_list.php
国家完整版:https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes(科学上网)
https://blog.csdn.net/qq_16393511/article/details/92794742(网友)

语言代码
中文zh
英语en
国家代码
中国大陆CN
中国台湾TW
中国香港HK
英国EN
美国US
加南大CA
		//语言+地区 = 中国大陆 中文
        Locale dl = new Locale("zh", "CN");
        //中国台湾 中文
        Locale tw = new Locale("zh", "TW");
        //等于zh CN
        Locale china = Locale.CHINA;
        //只有语言
        new Locale("zh");
        //等于 new Locale("zh")
        Locale chinese = Locale.CHINESE;
        //获得本地系统本地化
        Locale aDefault = Locale.getDefault();

可以根据jvm参数修改本地的环境
-Duser.language=en -Duser.region=Us
在这里插入图片描述

本地化工具类

NumberFormat,DateFormat,MessageFormat

        //MessageFormat 给不同地区的钱加上货币符号
        Locale china = Locale.CHINA;
        Locale us = Locale.US;
        NumberFormat currencyInstance = NumberFormat.getCurrencyInstance(china);
        NumberFormat currencyInstance1 = NumberFormat.getCurrencyInstance(us);
        double amt = 10.999d;
        System.out.println(currencyInstance.format(amt)); //¥11.00
        System.out.println(currencyInstance1.format(amt)); //$11.00

        //根据不同地区显示 显示不同的时间格式
        DateFormat dateInstance = DateFormat.getDateInstance(DateFormat.MEDIUM, china);
        DateFormat dateInstance1 = DateFormat.getDateInstance(DateFormat.MEDIUM, us);
        System.out.println(dateInstance.format(new Date()));//2023年1月7日
        System.out.println(dateInstance1.format(new Date()));//Jan 7, 2023

        //根据不同的地区,可以格式化字符串
        String patter = "{0},{1,time,long},{2,number,currency}";
        Object[] para = {"cyz",new Date(),1.0E3};
        MessageFormat messageFormat = new MessageFormat(patter,china);
        MessageFormat messageFormat1 = new MessageFormat(patter,us);
        System.out.println(messageFormat.format(para));//cyz,CST 上午12:18:55,¥1,000.00
        System.out.println(messageFormat1.format(para));//cyz,12:18:55 AM CST,$1,000.00

配置文件本地化 ResourceBundle

配置文件命名规范
[资源名] _ [语言代码] _ [国家/地区代码].properties
默认资源名
[资源名] .properties

定义了一个资源名为resource的多语言配置
在这里插入图片描述
简单的使用,配置文件内容
resource_en_US.properties

greeting.common=How are you
greeting.morning= good moring {0},give me {1,number,currency}

resource_zh_CN.properties

greeting.common=你好
greeting.morning= 早上好啊 {0},给我 {1,number,currency}

resource_cyz_CYZ.properties

greeting.common=#¥%!@#
greeting.morning= #¥%!@ {0},#¥%!@ {1,number,currency}

使用代码

		Locale china = Locale.CHINA;
        Locale us = Locale.US;
        //乱设置用来测试
        Locale cyz = new Locale("cyz", "CYZ");

        //第一个参数为相对路径 +/资源名 就是文件的前缀
        //如果resource123_en_US.properties 下面的就要写resource123
        ResourceBundle bundleChina = ResourceBundle.getBundle("resource", china);
        ResourceBundle bundleUs = ResourceBundle.getBundle("resource", us);
        ResourceBundle bundleCyz = ResourceBundle.getBundle("resource", cyz);

        Object[] para = {"cxk",10.11d};

        String formatChina = new MessageFormat(bundleChina.getString("greeting.morning"), china).format(para);
        String formatUs = new MessageFormat(bundleUs.getString("greeting.morning"), us).format(para);
        String formatCyz = new MessageFormat(bundleCyz.getString("greeting.morning"), cyz).format(para);

        System.out.println(formatChina);//早上好啊 cxk,给我 ¥10.11
        System.out.println(formatUs);//good moring cxk,give me $10.11
        System.out.println(formatCyz);//#¥%!@ cxk,#¥%!@ ¤ 10.11 没有实际的国家找不到货币符号

在java9之前中文不能直接明文写在配置文件,写完后要用java指令native2ascii进行转码才能使用

如果对应的语言+地区找不到配置文件,就会默认寻找其他的配置文件,最后会找[资源名] .properties,如果还找不到抛出异常

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值