JSF locale: browser defined language seems to be ignored
Currently my team is developing a web 2.0 application in java and we are using JSF for the presentation layer. We are beginning with the presentation layer so we have yet to add the business and data layer.
Technologies we use: - JSF (Mojarra 2.0.3) - Primefaces 2.1 (JSF library of components) - Spring Security 3.0.5 - jBoss 5.1
I was asked to add locale support to the app. So I configured the faces-config.xml file as follows:
<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>pt</supported-locale>
<supported-locale>en</supported-locale>
<supported-locale>es</supported-locale>
</locale-config>
<message-bundle>
com.<company>.<product>.i18n.MessageBundle
</message-bundle>
</application>
I created the MessageBundle suffixed files and deployed the app. I read that there are 3 criteria that JSF uses to choose the locale: 1) match between request locales (Accept-Language request header) and the supported locales 2) application default locale defined in the faces config file 3) JVM default locale
When I try to change the language in my browser options the app does not change the locale. Even if I only configure one language other than the default one. Am I missing something here? Is this what to expect or in fact it should change contents when I change the browser language (provided it i开发者_开发百科s a supported language)?
I would very much appreciate any tips or at least a pointer as to where I could find the answer seen as I have googled already for it with no luck.
Thanks in advance.
You need to refresh the page in the browser after you change the browser definied locale. You also need to realize that the <message-bundle>
is for localized conversion/validation messages, not for localized content. There you should use <resource-bundle>
in faces-config.xml
and/or <f:loadBundle>
in XHTML for.
In addition to the other answers, I can add the following remark: you need to create as many language files as there are defined languages. If you have a com.<company>.<product>.i18n.MessageBundle.properties
without _xx
extension, the messages inside will be default values for existing files, but not for non existing files.
Example:
messages.properties (containing English)
messages_fr.properties (containing French)
If you declare an "en" language, no English message will be displayed ! You must create :
messages.properties (containing default values not found in other files)
messages_en.properties (containing English)
messages_fr.properties (containing French)
I eventually found out what the problem was. Somewhere inside the code there was a locale declaration, for that reason, no matter which language I chose in my browser, the presented language would always be the same. Once I removed that declaration everything started working as supposed to.
I'm sorry for not posting this sooner.
Also I'd like to thank you all for your tips. Even though the answer was not in them, I learned a lot along the way.
Are you sure you have separate message bundles named:
com.<company>.<product>.i18n.MessageBundle_es and com.<company>.<product>.i18n.MessageBundle_pt?
精彩评论