How to override some Resources from a PropertyResourceBundle

匆匆过客 提交于 2019-12-01 07:37:11

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);
}

}

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.

You could write a custom PropertyResolver, and then have that perform the logic of where to pull the property values from.

For example, you could define a bean called messageSource and have that load up application.properties, plus your CMS properties or whatever you have.

Then write a custom PropertyResolver (there's an example of how to do this with Spring's MessageSource here) and link it in to your faces-config.xml using something like this:

<application>  
<property-resolver>  
      com.package.MessageSourcePropertyResolver  
   </property-resolver>  
</application>   
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!