如何覆盖PropertyResourceBundle中的一些资源 [英] How to override some Resources from a PropertyResourceBundle

查看:170
本文介绍了如何覆盖PropertyResourceBundle中的一些资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的Java / JSF网站,屏幕上的所有文字都来自属性文件,通过< f:loadBundle basename =org.example.applicaltionvar =applicaltion/>从applicaltion.properties中提取文本。

I have an exsisting Java/JSF website all the text on the screen is coming from property files via <f:loadBundle basename="org.example.applicaltion" var="applicaltion" /> which pulls the text from applicaltion.properties.

对于这些运行时可配置的子集,我想从其他位置(CMS通过Web服务)拉取字符串。看看ResourceBundle类它有一个基础设施用于接近这个的基础设施,并且委托给父ResourceBundle。

For a runtime configurable subset of these I am wanting to pull the string from else where (CMS via web services). Looking at the ResourceBundle class it appares to have an infrastructer for something close to this with delegation to a parent ResourceBundle.

我想要像这样的一些

public class Applicaltion extends ResourceBundle{
    @Override
protected Object handleGetObject(String key) {
    if(overridenKey(key)){
        return overridedValue(key);
    }
    return null; // ResourceBundle.getObject will delegate to parent 
             // if we return null
}
}

我试过这个并且parent为null,我认为这更多用于默认情况 - > en - > en_GB。

I have tried this and parent is null, I assume this is more used for the case of default -> en -> en_GB.

我正在考虑一个不太吸引人的选项,即让属性文件与自定义resourceBundle具有不同的名称,然后从CustomResourceBundle.handleGetObject(key)中委托整个ResourceBundle.getBundle(PROPERTY_FILE_NAME).getString(key)堆栈。

I am considering the not very appealing option of have the property file a different name from the custom resourceBundle and then delegating through the full stack of ResourceBundle.getBundle(PROPERTY_FILE_NAME).getString(key) from within CustomResourceBundle.handleGetObject(key).

任何更好的想法?

推荐答案

我最终通过检查是否解决了这个问题我们有一个覆盖值,如果我们返回那个,否则委托给标准资源包

I ended up solving this by checking if we had an override value, if we did returning that, else delegating to the standard resource bundle

public class UILabels extends ResourceBundle {


private ResourceBundle getFileResources(){
    return ResourceBundle.getBundle("com.example.web.UILabelsFile", this.getLocale());
}

public Enumeration<String> getKeys() {
    return getFileResources().getKeys();
}

protected Object handleGetObject(String key) {
    if(overrideValue(key)){
        return getOverridenValue(key);
    }
    return getFileResources().getObject(key);
}

}

注意名称等级的细微差别UILabels是所有客户端将使用该文件的是UILabelsFile,因此ResourceBundle加载器不会递归。

Note the slight difference in name class is UILabels which is what all clients will use the file is UILabelsFile so the ResourceBundle loader does not go recursive.

这篇关于如何覆盖PropertyResourceBundle中的一些资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆