You tagged your question with Spring, so I recommend that you use Spring MessageSource. Spring MessageSource can aggregate many property files even hierarchically. This gives you many advantages over the old java ResourceBundle .
You can define Spring MessageSource in spring-config.xml as follows:
<bean id="messageSource" name="resourceBundle" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" p:fallbackToSystemLocale="false" p:cacheSeconds="0"> <property name="basenames"> <list> <value>/messages/Messages</value> </list> </property> </bean>
How can you define your Class , which extends the ResourceBundle as follows (some cleaning and refactoring is required):
public class SpringResourceBundle extends ResourceBundle { private MessageSource messages; private FacesContext fc; private Locale locale = null; public SpringResourceBundle() { fc = FacesContext.getCurrentInstance(); WebApplicationContext webAppCtx = (WebApplicationContext) fc.getExternalContext().getApplicationMap().get( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); messages = (MessageSource) webAppCtx.getBean("messageSource"); } @Override public Locale getLocale() { Locale loc = fc.getELContext().getLocale(); if (fc.getExternalContext() != null) { loc = fc.getExternalContext().getRequestLocale(); } try { UIViewRoot viewRoot = fc.getViewRoot(); if (viewRoot != null) { loc = viewRoot.getLocale(); } if (loc == null) { loc = fc.getApplication().getDefaultLocale(); } } catch (Throwable th) { System.out.println(th.getMessage()); loc = locale; } locale = loc; return loc; } @Override protected Object handleGetObject(String key) { try { return messages.getMessage(key, null, getLocale()); } catch (NoSuchMessageException e) { return "???" + key + "???"; } } @Override public Enumeration<String> getKeys() { return Collections.enumeration(Collections.EMPTY_LIST); } }
Finnaly in faces-config.xml declare your resource package with the class above. Something like that:
<application> <locale-config> <default-locale>en</default-locale> <supported-locale>cs</supported-locale> <supported-locale>de</supported-locale> <supported-locale>en</supported-locale> </locale-config> <message-bundle>your.package.SpringResourceBundle</message-bundle> </application>
Here you go Spring MessageSource in JSF. Hope this is understandable.
source share