Hello World的国际化静态版本

来源:互联网 发布:linux添加永久默认路由 编辑:程序博客网 时间:2024/06/17 22:27

1 资源文件的三种形式

  • baseName_language_country.properties
  • baseName_language.properties
  • baseName.properties

2 为hello world国际化版本准备资源文件

mess.properties的文件内容

hello=你好!

mess_en_US.properties的文件内容

hello=Welcome you!

mess_zh_CN.properties的文件内容

hello=\u4f60\u597d\uff01

该文件是通过下面方法得到,native2ascii工具在JDK的安装目录下,同java.exe在同一目录。

E:\test\Java\First2>native2ascii mess.properties mess_zh_CN.properties

 

3 应用举例

代码示例

import java.util.*;public class Hello{public static void main(String[] args){// 取得系统默认的国家/语言环境Locale myLocale = Locale.getDefault(Locale.Category.FORMAT);// 根据指定国家/语言环境加载资源文件ResourceBundle bundle = ResourceBundle.getBundle("mess" , myLocale);// 打印从资源文件中取得的消息System.out.println(bundle.getString("hello"));}}

运行结果

你好!

代码说明

Java程序国际化关键类是ResourceBundle和Locale,ResourceBundle根据不同的Locale加载语言资源文件,再根据指定的key取得已加载语言资源文件的字符串。

注意:

要将制作好的mess_zh_CN.properties文件和Hello.class放在同一目录下。

0 0
原创粉丝点击