从db 获取的messages.properties
可能重复:
有关多语言支持的Java EE实体的设计问题
我正在研究JSF应用程序的i18n。 我需要从数据库中获取通常位于messages.properties中的所有标准jsf消息。 有一些简单的方法吗?
谢谢。
我想我找到了答案:
public class DBMessagesBundle extends ResourceBundle { @Override protected String handleGetObject(String key){ ... } @Override public Enumeration getKeys() { ... } }
在FacesConfig.xml中
... mypackage.DBMessagesBundle
谢谢你的帮助。
首先,您需要自己的MessageSource。 看一下AbstractMessageSource并扩展它:
public class CustomResourceBundleMessageSource extends AbstractMessageSource { @Autowired LocalizationStore localizationStore; @Override protected MessageFormat resolveCode(String code, Locale locale){ MessageFormat messageFormat = null; ResourceBundle bundle = localizationStore.getBundle(locale); try { messageFormat = getMessageFormat(bundle, code, locale); } catch (MissingResourceException | NullPointerException ex) { //Return just the code in case this is used for logging, or throw, your choice return createMessageFormat(code, locale); } return messageFormat; } protected MessageFormat getMessageFormat(ResourceBundle bundle, String code, Locale locale) throws MissingResourceException { String msg = bundle.getString(code); return createMessageFormat(msg, locale); } }
您的商店必须返回ResourceBundle:
这很大程度上取决于您的数据库模型。 我建议在getBundle()方法上使用@Cachable,因为您的本地化不太可能经常更改,并且根据您的数据库模型,它可能很昂贵。 返回的对象只需要为ResourceBundle实现以下方法:
public class DBResourceBundle extends ResourceBundle { @Override protected String handleGetObject(String key){ ... } @Override public Enumeration getKeys() { ... } }
最后,您需要在配置中注册MessageSource bean:
- 在try / catch中捕获exception后继续执行循环
- Java:openGL:JOGL:当我调用display()方法时,幕后会发生什么?
- 为什么autoboxed整数和.getClass()值== – 相等,不仅.equals() – 相等?
- 为什么getClass返回类的名称+ $ 1(或$ *)
- 如何使用SolrJ获取solr服务器中所有核心的列表
- Tomcat 6堆大小 – 这是正确的吗?
- iText如何从可填写的模板创建多页文档
- 同意Xcode / iOS许可证需要管理员权限,请以root用户身份重新运行sudo无法在操作系统更新后运行git
- DocumentFilter的正则表达式匹配所有十进制数,但最后只有一个小数