如何覆盖 PropertyResourceBundle 中的某些资源
我有一个现有的 Java/JSF 网站,屏幕上的所有文本都来自属性文件,通过
对于这些的运行时可配置子集,我想从其他地方(通过 Web 服务的 CMS)提取字符串。 查看 ResourceBundle 类,它似乎有一个与此类似的基础设施,并委托给父 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
}
}
我已经尝试过这个并且父级为空,我认为这更多地用于默认的情况 -> zh -> en_GB。
我正在考虑一个不太吸引人的选项,即让属性文件与自定义 ResourceBundle 具有不同的名称,然后从 CustomResourceBundle.handleGetObject(key) 中委托整个 ResourceBundle.getBundle(PROPERTY_FILE_NAME).getString(key) 堆栈。
还有更好的想法吗?
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.
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.
I am wanting somethis like this
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
}
}
I have tried this and parent is null, I assume this is more used for the case of default -> en -> en_GB.
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).
Any better ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终通过检查我们是否有覆盖值来解决这个问题,如果我们确实返回它,否则委托给标准资源包
注意名称类中的细微差别是 UILabels,这是所有客户端将使用的文件是 UILabelsFile 所以 ResourceBundle加载程序不会递归。
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
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.
您可以编写一个自定义 PropertyResolver,然后让它执行从何处提取属性值的逻辑。
例如,您可以定义一个名为
messageSource
的 bean,并加载 application.properties,以及您的 CMS 属性或任何您拥有的属性。然后编写一个自定义
PropertyResolver
(有一个示例说明如何使用 Spring 的 MessageSource 此处)并使用如下内容将其链接到您的faces-config.xml
: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 yourfaces-config.xml
using something like this: