ADF中设计支持国际化

在ADF应用中,可以在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=主页面

2)在faces-config.xml中描述资源文件的使用:

    <message-bundle>oracle.fodemo.storefront.resources.UIStrings</message-bundle>
    <locale-config>
      <default-locale>en</default-locale>
      <supported-locale>zh</supported-locale>
    </locale-config>
   <resource-bundle>
      <base-name>oracle.fodemo.storefront.StoreFrontUIBundle</base-name>
      <var>res</var>
    </resource-bundle>

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

更多内容可以参考:http://java.chinaitlab.com/base/38294.html


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。


卢玉双 2011/12/31 @上海


参考:

http://technology.amis.nl/blog/4076/context-sensitive-resource-bundle-entries-in-javaserver-faces-applications-going-beyond-plain-language-region-variant-locales

http://java.chinaitlab.com/base/38294.html

http://dev.yesky.com/369/2285369.shtml


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值