Java 对国际化 (i18n) 的支持资源包的调用
中文 | zh |
英语 | en |
日语 | ja |
法语 | fr |
中国大陆 | CN |
中国香港 | HK |
美国 | US |
英国 | EN |
Locale
//带有语言和国际地区信息的 Locale
Locale local1 = new Locale("zh","CN");
//带有语言信息的 Locale
Locale local2 = new Locale("zh");
//等同于 Locale("zh","CN");
Locale locale3 = Locale.CHINA;
//等同于 Locale("zh");
Locale locale4 = Locale.CHINESE;
//获取默认Locale
Locale defaultLocale = Locale.getDefault();
java -Duser.language=zh -Duser.region=CN MainClass
本地化工具类
DateFormat
① 格式化日期
Date date = new Date();
Locale locale = new Locale("en","US");
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM,locale);
String fmtStr = dateFormat.format(date); //fmtStr = "Dec 1, 2017"
② 格式化时间
Date date = new Date();
Locale locale = new Locale("en","US");
DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM,locale);
String fmtStr = dateFormat.format(date); //fmtStr = "4:46:21 PM"
③ 格式化日期时间
Date date = new Date();
Locale locale = new Locale("en","US");
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM,locale6);
String fmtStr = dateFormat.format(date); //fmtStr = "Dec 1, 2017 4:48:06 PM"
- DateFormat.SHORT:如:7/24/98 4:49 PM
- DateFormat.MEDIUM:如 :24-Jul-98,4:49:05 PM
- DateFormat.LONG:如:July 24, 1998 4:49:05 PM EST
- DateFormat.FULL:如:Friday,July 24, 1998 4:49:05 o'clock PM EST
NumberFormat
① 普通数字格式化
double value = 2333.33;
Locale locale = new Locale("en","US");
NumberFormat numberFmt = NumberFormat.getInstance(locale);
String fmtStr = numberFmt.format(value); //fmtStr = "2,333.33"
② 货币格式化
double value = 2333.33;
Locale locale = new Locale("zh","CN");
NumberFormat numberFmt = NumberFormat.getCurrencyInstance(locale);
String fmtStr = numberFmt.format(value); //fmtStr = "¥2,333.33"
③ 本地化数字字符串转换为数字
//将一个本地化的货币字符串转化为double类型;
String currStr = "¥2,333.33";
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.CHINA);
Number num = numberFormat.parse(currStr);
double value = num.doubleValue(); //value=2333.33
MessageFormat
//格式化模板字符串
String pattern1 = "{0},您于 {1,date,short} 支付 {2,number,currency} ";
String pattern2 = "{0} ,you paid {2,number,currency} at {1,date,short} ";
//动态替换格式化字符串的占位符的参数
Object[] params = {"Al-assad",new Date(),1.0E3};
//使用指定本地化对象格式化信息
MessageFormat msgFmt1 = new MessageFormat(pattern1,Locale.CHINA);
MessageFormat msgFmt2 = new MessageFormat(pattern2,Locale.US);
String msg1 = msgFmt1.format(params); //"Al-assad,您于 17-12-1 支付 ¥1,000.00 "
String msg2 = msgFmt2.format(params); //"Al-assad ,you paid $1,000.00 at 12/1/17 "
资源包 和 ResourceBundle
资源包
<资源名>_<语言代码>_<国家/地区代码>.properties
resource.properties //默认资源包,但程序查到不到当地的本地化资源包时,使用该资源包
resource_zh_CN.properties //中文资源包
resource_en_US.properties //英文资源包
......
greeting.common=How are you!
greeting.morning=Good Morning!
greeting.afternoon=Good Afternoon!
greeting.common=\u4F60\u597D!
greeting.morning=\u65E9\u4E0A\u597D!
greeting.afternoon=\u4E0B\u5348\u597D\uFF01
native2ascii [-encoding 编码] [输入文件 [输出文件]]
native2ascii -encoding utf-8 d:\resource_zh_CN_orginal.properties d:\resource_zh_CN.properties
ResouceBundle
获取国际化资源包中的键值
//使用系统用默认Locale获取资源包
Locale locale = Locale.getDefault();
ResourceBundle resourceBundle = ResourceBundle.getBundle("site/assad/i18n/resource"); //指定资源包路径,基于类路径
//获取资源包中的对应键值
String common = resourceBundle.getString("greeting.common"); //"你好!"
String morning = resourceBundle.getString("greeting.morning"); //"早上好!"
String afternoon = resourceBundle.getString("greeting.afternoon"); //"下午好!"
//指定Locale获取资源包
ResourceBundle resourceBundle2 = ResourceBundle.getBundle("site/assad/i18n/resource",Locale.US);
String common2 = resourceBundle2.getString("greeting.common"); //"How are you!"
在资源文件中使用占位符模板字符串
greeting.common=How are you!{0},today is {1,date,short}
greeting.morning=Good morning!{0},now is {1,time,short}
greeting.afternoon=Good afternoon!{0},now is{1,time,short}
//加载本地化资源
ResourceBundle resourceBundle1 = ResourceBundle.getBundle("site/assad/i18n/fmt_resource",Locale.US);
ResourceBundle resourceBundle2 = ResourceBundle.getBundle("site/assad/i18n/fmt_resource",Locale.CHINA);
//动态替换占位符参数
Object[] params = {"Al-assad",new Date()};
//获取本地化键值,并对其进行格式化
String common1 = new MessageFormat(resourceBundle1.getString("greeting.common"),Locale.US).format(params);
String morning1 = new MessageFormat(resourceBundle1.getString("greeting.morning"),Locale.US).format(params);
String afternoon1 = new MessageFormat(resourceBundle1.getString("greeting.afternoon"),Locale.US).format(params);
String common2 = new MessageFormat(resourceBundle2.getString("greeting.common"),Locale.CHINA).format(params);
String morning2 = new MessageFormat(resourceBundle2.getString("greeting.morning"),Locale.CHINA).format(params);
String afternoon2 = new MessageFormat(resourceBundle2.getString("greeting.afternoon"),Locale.CHINA).format(params);
System.out.println(common1+"\n"+morning1+"\n"+afternoon1+"\n"+common2+"\n"+morning2+"\n"+afternoon2);
/*output:
How are you!Al-assad,today is 12/1/17
Good morning!Al-assad,now is 2:22 PM
Good afternoon!Al-assad,now is2:22 PM
你好!Al-assad,今天是 17-12-1
上午好!Al-assad,现在的时间是 下午2:22
下午好!Al-assad,现在的时间是 下午2:22
* */
Java 对国际化 (i18n) 的支持资源包的调用相关推荐
- minecraft_如何使用资源包改变Minecraft的世界
minecraft Some people love Minecraft's 8-bit chunky look, while others can't believe a game would lo ...
- java国际化——资源包
[0]README 1) 本文部分文字描述转自 core java volume 2 , 旨在理解 java国际化--资源包 的基础知识 : 2) 本文源代码idea 转自: (利用propertie ...
- java i18n_Java i18n – Java的国际化
java i18n Internationalization in Java or Java i18n is a very important feature. Java provides inter ...
- java i18n_Java国际化(i18n)
Java国际化(i18n) 最近在做一个网站国际化的功能.用Java做开发,使用spring+velocity. Java提供了对i18n的支持,spring对其做了集成,可以很方便的配置.主要思想就 ...
- android打包名字,使用jenkins为android工程打包,支持多包名,改资源(踩坑指南)...
需求 上一篇文章主要写了打包刚开始的配置和参数化构建.这篇文章主要讲一讲在改包名.改资源的打包实践中,常会碰到的问题以及解决办法.如果看博客的人有更好的解决办法,也可以找我交流,关于页面有我联系方式. ...
- Java中文处理, 资源包后缀详解
作者: 车东 Email: chedongATbigfoot.com/chedongATchedong.com 写于:2002/07 最后更新: 02/22/2006 14:42:55 版权声明:可以 ...
- java i18n实例_Java国际化(i18n)格式化日期
本篇文章帮大家学习java国际化(i18n)格式化日期,包含了Java国际化(i18n)格式化日期使用方法.操作技巧.实例演示和注意事项,有一定的学习价值,大家可以用来参考. DateFormat类提 ...
- 苹果ios超级签名源码包java版带分发页面支持安卓合并部署教程
果超级签名是什么我就不在赘述了,项目由java+vue开发 功能: 1.支持登录注册,拥有共有池,可上传证书等基本操作,支持用户自行上传证书 2.支持修改分发页面轮播图,简介,安卓合并 3.ipa签名 ...
- 苹果ios超级签名源码包java版带分发页面支持安卓合并
苹果超级签名是什么我就不在赘述了,项目由java+vue开发 功能: 1.支持登录注册,拥有共有池,可上传证书等基本操作,支持用户自行上传证书 2.支持修改分发页面轮播图,简介,安卓合并 3.ipa签 ...
最新文章
- Linux那些事儿 之 戏说USB(27)设备的生命线(十)
- 基于分布式的短文本命题实体识别之----人名识别(python实现)
- Work with Alexa :Echo匹配连接到Alexa
- Linux查看文件夹大小的命令
- 【独立游戏】黎明之刃——3D硬核ARPG游戏
- 【数理知识】《矩阵论》方保镕老师-目录及关于符号的含义
- C# SQLite 数据库操作语句与文件视图管理器
- [开发笔记]-winfom ListBox控件选中项上下移动排序
- 清理垃圾代码bat_来买个小内存u盘么咯?盘内带电脑垃圾清理.bat那种丨heibai.org...
- 基础算法 —— 高精度计算 —— 高精度除法
- ModalPopupExtender控件主要有两种使用方式:客户端使用方式和服务器端使用方式
- 杭电 2838 牛叉的树状数组
- VS2005 + VSS2005 的方法
- 2015 2020 r4烧录卡 区别_2020版药典,药用辅料被重视了!
- Windows 下使用 TFTPD32+HTTP PXE引导安装linux
- PyTorch搭建LSTM实现时间序列预测(负荷预测)
- javacore日志分析工具jca
- 信号积分变换的公式推导以及几个例子
- 蓝色经典钢琴-Cinesamples Piano In Blue v2.3b Kontakt
- 树莓派3B+ 编译Qt源码
热门文章
- FMS2016设NVMe主题展区 Memblaze展示PBlaze4及FlashRAID
- ESP32-S3 master分支开发环境搭建
- 【Go Web学习笔记】第一章 搭建Web服务器
- Java顺时针打印矩阵
- 最小生成树的克鲁斯卡尔算法
- 手机射频测试总结(五)——蓝牙部分
- python安装出现modify_Win10系统提示“Python 0x80070643安装时发生严重错误”怎么办?...
- java计算机毕业设计社区团购系统源码+系统+数据库+lw文档
- 想做成功网站需要注意的几个问题
- 网络测试工具 iperf 使用指南