JSF 中的本地化





5.00/5 (2投票s)
在本文中,我将介绍文本、日期和货币的本地化。
引言
这是一个在 JSF 应用程序中实现的本地化的简单演示。本地化是将应用程序调整为特定区域的语言、文化和格式的过程,该区域被称为区域设置。在本文中,我将介绍文本、日期和货币的本地化。
背景
java.util
包中的 Locale
类用于指定地理位置,以便用于实现本地化。Locale
类具有以下构造函数:
Locale(String language)
Locale(String language, String country)
Locale(String language,String country,String variant)
例如,您可以使用以下代码指定在法国使用的法语:
Locale locale=new Locale("fr","FR");
java.util
包中的 ResourceBundle
类用于加载资源包文件。资源包是一个 Java .properties 文件,其中包含特定区域设置的字符串。它由每个属性的键值对组成。资源包文件的命名方式为 BaseFileName_LanguageCode_CountryCode.properties。例如,在法国使用的法语的资源包文件将是 MessageBundle_fr_FR.properties。
以下代码显示了如何使用 ResourceBundle
类加载法语的资源包:
ResourceBundle bundle=ResourceBundle.getBundle("MessageBundle",locale);
相应的 MessageBundle_fr_FR.properties 文件包含以下代码:
# To change this license header, choose License Headers in Project Properties. # To change this template file, choose Tools | Templates # and open the template in the editor. message=Bonjour language=French imageUrl=france.png
java.text
包中的 DateFormat
类用于本地化日期。以下是如何本地化当前日期的示例:
DateFormat df=DateFormat.getDateInstance(DateFormat.SHORT, locale); currDate=df.format(new Date());
java.text
包中的 NumberFormat
类用于本地化数字。以下是如何将金额本地化为货币的示例:
NumberFormat nf=NumberFormat.getCurrencyInstance(locale); amount=nf.format(12345.67);
使用代码
由于本文基于 JSF,我创建了一个资源作用域的托管 Bean 类,名为 HelloBean
,以执行实际的本地化并将本地化的数据返回到 JSF 页面。托管 Bean 包含以下属性:
private String language; private String message; private String currDate; private String amount; private String imageUrl;
它包含上述属性的 getter 方法以及将这些属性在不同语言和国家/地区进行本地化的方法。以下代码显示了英语和法语中的本地化:
public void showEnglishMessage() { Locale locale=new Locale("en","US"); ResourceBundle bundle=ResourceBundle.getBundle("MessageBundle",locale); localize(bundle, locale); } public void showFrenchMessage() { Locale locale=new Locale("fr","FR"); ResourceBundle bundle=ResourceBundle.getBundle("MessageBundle",locale); localize(bundle, locale); } public void localize(ResourceBundle bundle,Locale locale) { message=bundle.getString("message"); language=bundle.getString("language"); imageUrl=bundle.getString("imageUrl"); DateFormat df=DateFormat.getDateInstance(DateFormat.SHORT, locale); currDate=df.format(new Date()); NumberFormat nf=NumberFormat.getCurrencyInstance(locale); amount=nf.format(12345.67); }
在上面的代码中,showEnglishMessage()
和 showFrenchMessage()
用户定义的方法调用另一个用户定义的方法,名为 localize()
,它使用 Locale 和 ResourceBundle 参数执行本地化。
以下是英语和法语的资源包文件:
# To change this license header, choose License Headers in Project Properties. # To change this template file, choose Tools | Templates # and open the template in the editor. message=Hello language=English imageUrl=us.png
# To change this license header, choose License Headers in Project Properties. # To change this template file, choose Tools | Templates # and open the template in the editor. message=Bonjour language=French imageUrl=france.png
托管 Bean 的属性绑定到 index.xhtml JSF 页面上的 JSF 控件,如下所示:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Localization Demo</title>
</h:head>
<h:body>
<h:form>
<div align="center">
<h1>Localization Demo</h1>
<h:panelGrid columns="2" border="5">
<h:outputLabel for="txtLanguage" value="Language: "/>
<h:outputText id="txtLanguage" value="#{helloBean.language}"/>
<h:outputLabel for="txtMessage" value="Message: "/>
<h:outputText id="txtMessage" value="#{helloBean.message}"/>
<h:outputLabel for="txtDate" value="Date: "/>
<h:outputText id="txtDate" value="#{helloBean.currDate}"/>
<h:outputLabel for="txtAmount" value="Amount(Currency): "/>
<h:outputText id="txtAmount" value="#{helloBean.amount}"/>
<h:outputLabel for="imgFlag" value="Flag: "/>
<h:graphicImage id="imgFlag" url="#{helloBean.imageUrl}"
width="100" height="100" />
<h:commandButton id="cmdEnglish" value="English Hello"
action="#{helloBean.showEnglishMessage}"
style="width: 170px" />
<h:commandButton id="cmdFrench" value="French Hello"
action="#{helloBean.showFrenchMessage}"
style="width: 170px" />
<h:commandButton id="cmdGerman" value="German Hello"
action="#{helloBean.showGermanMessage()}"
style="width: 170px" />
<h:commandButton id="cmdSpanish" value="Spanish Hello"
action="#{helloBean.showSpanishMessage()}"
style="width: 170px" />
<h:commandButton id="cmdItalian" value="Italian Hello"
action="#{helloBean.showItalianMessage()}"
style="width: 170px" />
<h:commandButton id="cmdTurkish" value="Turkish Hello"
action="#{helloBean.showTurkishMessage()}"
style="width: 170px" />
</h:panelGrid>
</div>
</h:form>
</h:body>
</html>
关注点
正如本文所示,Java 提供了一种非常简单的方法来本地化数据。可以使用相同的方法在任何类型的 Java 环境中使用。
本文中使用的国旗图像是从以下网站下载的: