您的位置:首页 > 编程语言 > Java开发

javaweb国际化

2016-07-04 10:57 549 查看
根据数据的类型不同,国际化分为2类:静态数据国际化和动态数据的国际化。

静态数据,包括 “标题”、“用户名”、“密码”这样的文字数据。

动态数据,包括日期、货币等可以动态生成的数据。

国际化涉及到java.util.Locale和java.util.ResourceBundle类。

java.util.Locale

A Locale object represents a specific geographical, political, or cultural region.

Locale对象代表了一定的地理、政治、文化区域。

java.util.ResourceBundle

Resource bundles contain locale-specific objects. When your program needs a locale-specific resource, a String for example, your program can load it from the resource bundle that is appropriate for the current user's locale. In this way, you can write program code that is largely independent of the user's locale isolating most, if not all, of the locale-specific information in resource bundles.

ResouceBundle,由两个单词组成Resouce和Bundle,合在一起就是“资源包”的意思。ResouceBundle是包含不同区域(Locale)资源的集合,只要向ResouceBundle提供一个特定的Locale对象,ResouceBundle就会把相应的资源返回给你。





1、静态数据国际化

静态数据国际化的步骤:

(1).建立资源文件,存储所有国家显示的文本的字符串

a)文件: .properties

b)命名: 基础名_语言简称_国家简称.properties

例如: msg_zh_CN.properties 存储所有中文

msg_en_US.properties 存储所有英文

(2).程序中获取

ResourceBundle类,可以读取国际化的资源文件!

Locale类,代表某一区域,用于决定使用哪一个国际化的资源。

1.1、Locale的API

static Locale getDefault() 得到JVM中默认的Locale对象

String getCountry() 得到国家名称的简写

String getDisplayCountry() 得到国家名称的全称

String getLanguage() 得到当前语言的简写

String getDisplayLanguage() 得到语言的全称

1.2、ResourceBundle的API

static ResourceBundle getBundle(String baseName,Locale locale) 获取ResourceBundle实例

String getString(String key) 根据key获取资源中的值

1.3、示例

(1)建立properties文件:msg_zh_CN.properties和msg_en_US.properties

msg_zh_CN.properties





msg_en_US.properties

(2)代码获取

1.4、关于ResourceBundle的资源文件properties

文件命名:基础名、语言简称

Resource bundles belong to families whose members share a common base name, but whose names also have additional components that identify their locales. For example, the base name of a family of resource bundles might be "MyResources". The family can then provide as many locale-specific members as needed, for example a German one named "MyResources_de".

文件命名:国家简称

If there are different resources for different countries, you can make specializations: for example, "MyResources_de_CH" contains objects for the German language (de) in Switzerland (CH). If you want to only modify some of the resources in the specialization, you can do so.

文件命名:默认的Resource Bundle

The family should have a default resource bundle which simply has the same name as its family - "MyResources" - and will be used as the bundle of last resort if a specific locale is not supported.

文件内容:属于同一个family的resource bundle要包含相同的items内容。

Each resource bundle in a family contains the same items, but the items have been translated for the locale represented by that resource bundle. For example, both "MyResources" and "MyResources_de" may have a String that's used on a button for canceling operations. In "MyResources" the String may contain "Cancel" and in "MyResources_de" it may contain "Abbrechen".

Java代码:获取Resource Bundle

When your program needs a locale-specific object, it loads the ResourceBundle class using the getBundle method:

ResourceBundle myResources = ResourceBundle.getBundle("MyResources", currentLocale);

2、动态数据国际化

动态国际化则主要涉及到数字、货币、百分比和日期

例如:

中文:1987-09-19 ¥1000

英文: Sep/09 1987 $100

3、JSP页面国际化

数值,货币,时间,日期等数据由于可能在程序运行时动态产生,所以无法像文字一样简单地将它们从应用程序中分离出来,而是需要特殊处理,有的Java培训机构讲的不错。Java 中提供了解决这些问题的 API 类(位于 java.util 包和 java.text 包中)

3.1、准备工作:建立properties资源

建立2个properties文件:message_en_US.properties和message_zh_CN.properties。

message_zh_CN.properties





message_en_US.properties

3.2、使用JSP脚本进行国际化

3.3、使用JSTL进行国际化

JSTL标签:

核心标签库

国际化与格式化标签库

数据库标签库(没用)

函数库

<fmt:setLocale value=""/> 设置本地化对象

<fmt:setBundle basename=""/> 设置工具类

<fmt:message></fmt:message> 显示国际化文本

格式化数值:<fmt:formatNumber pattern="#.##" value="100.99"></fmt:formatNumber>

格式化日期:<fmt:formatDate pattern="yyyy-MM-dd" value="${date}"/>

需要注意的一点是:HttpServletRequest有一个方法是getLocale(),可以获取当前request中的Locale信息,在EL表达式中可以使用${pageContext.request.locale}获取


格式化数值和日期

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: