从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: