JSF国际化

在应用中,可以在faces-config.xml中定义合适的Message Bundle和Resource Bundle,并支持多国语言。我们可以将程序中使用的Message内容定义在Message Bundle中,将Label、ShortDesc等内容定义在Resource Bundle中。Message Bundle中的内容可以使用Java代码进行读取,方便程序逻辑中Message的动态显示;Resource Bundle的内容可以直接使用EL表达式进行绑定。

 

1,Message Bundle和Resource Bundle的定义

1)定义Message的资源文件:UIStrings_en.properties、UIStrings_zh.properties。

UIStrings_en.properties:

database.connect.failure=The database connection could not be established. Please verify that the database is up, a data source jdbc/FODDS exists and that the username is provided as FOD

UIStrings_zh.properties

database.connect.failure=无法正常连接数据库

定义Label等使用的资源文件:StoreFrontUIBundle_en.properties、StoreFrontUIBundle_zh.properties。

StoreFrontUIBundle_en.properties:

global.nav.home=Home

StoreFrontUIBundle_zh.properties:

global.nav.home=主页面

 

为了让每个JSP页面都支持国际化需要在faces-config.xml文件中写入:

 

 

<application>
        <locale-config>
            <default-locale>en</default-locale>
            <supported-locale>en_US</supported-locale>
            <supported-locale>zh_CN</supported-locale>
        </locale-config>
        <resource-bundle>
		<base-name>com.asia.gecapital.cdbs.local.Messages</base-name>--//资源文件名
		<var>msgs</var>---//资源文件要在页面上引用时的缩写形式例如:  
 <h:outputText value="#{msgs.nameText}"/> 
	  </resource-bundle>
        <message-bundle>--//
            com.asia.gecapital.cdbs.jsf.Messages
        </message-bundle>
        <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
    </application>

 

Java中的国际化是由 java.util.Locale 类支持的,中文对应的代码是“zh”;我们要在JSF中使用中文的话,只要在 faces-config.xml 中加入对中文的支持,当然如果需要支持更多的语言的话,多加几个<supported-locale>就可以了。比如:<supported-locale>ja</supported-locale>

3)Locale的规则为:language:[_:country:[_:variant:]],language:[-:country:[-:variant:]]

完整的定义不包含:,例子:ja-JP-SJIS

 

2,使用Java代码读取Message Bundle内容

不同的语言在getBundle方法中会进行区分。

 

String alertMessage = BundleUtils.getStringFromBundle("database.connect.failure");  
  
public static String getStringFromBundle(String key) {  
    ResourceBundle bundle = getBundle();  
    return getStringSafely(bundle, key, null);  
}  
  
private static ResourceBundle getBundle() {  
    FacesContext ctx = getFacesContext();  
    UIViewRoot uiRoot = ctx.getViewRoot();  
    Locale locale = uiRoot.getLocale();  
    ClassLoader ldr = Thread.currentThread().getContextClassLoader();  
    return ResourceBundle.getBundle(ctx.getApplication().getMessageBundle(),   
                                    locale, ldr);  
}  
  
private static String getStringSafely(ResourceBundle bundle, String key,   
                                      String defaultValue) {  
    String resource = null;  
    try {  
        resource = bundle.getString(key);  
    } catch (MissingResourceException mrex) {  
        if (defaultValue != null) {  
            resource = defaultValue;  
        } else {  
            resource = NO_RESOURCE_FOUND + key;  
        }  
    }  
    return resource;  
}  

 

3,使用EL绑定Resource Bundle内容

可以使用#{res['global.nav.home']}或者#{res.global.nav.home}来引用global.nav.home。

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值