In my xhtml page I have the following tag included:
<f:view locale="#{languageBean.locale}">
...
</f:view>
I have implemented a language bean:
@ManagedBean(name = "languageBean")
@SessionScoped
public class LanguageBean implements Serializable {
private static final long serialVersionUID = -4260383661321467037L;
...
private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
public String switchToDE() {
LOGGER.info("switching to german");
Locale german = Locale.GERMAN;
FacesContext.getCurrentInstance().getViewRoot().setLocale(german);
this.locale = german;
return null;
}
public String switchToEN() {
LOGGER.info("switching to english");
Locale english = Locale.ENGLISH;
FacesContext.getCurrentInstance().getViewRoot().setLocale(english);
this.locale = english;
return null;
}
public Locale getLocale() {
return locale;
}
public void setLocale(final Locale locale) {
this.locale = locale;
}
}
Then I want to render commandLinks to switch locale:
<ul>
<li>
<h:form>
<h:commandLink class="" action="#{languageBean.switchToDE}">Deutsch</h:commandLink>
</h:form>
</li>
<li>
<h:form>
<h:commandLink action="#{languageBean.switchToEN}">English</h:commandLink>
</h:form>
</li>
</ul>
On some places in my page I have some outputs where I render messages:
#{msg['message.key.1']
In my faces-config.xml I have configured:
<locale-config>
<default-locale>de</default-locale>
<supported-locale>de</supported-locale>
<supported-locale>en</supported-locale>
</locale-config>
Now my problem: the first click visits the switch locale method but the message bundles are not refreshed in the page. If I click a second time on the same language the message bundle is updated. The message bundle lookup is always one step behind the locale switching. How could I switch locale with h:commandButton tag (I need a link and the style guide disallow to use a or ) so the message bundle lookup is in sync?
I am using GF 2.1 with Mojarra 2.0.9 (JSF 2.0 based on Servlet API 2.5). Any advice is welcome.
EDIT:
Ok, I found out that it was my fault cause we try some "special" things ;-)
I try to explain what we have and what we try:
- we are building our software with maven
- we have separated logically different web apps for different business purposes in the SVN repo
- during build time we aggregate all web apps into one (WAR-overlay)
- therefore we have for each business purposed web app a faces-config-<business-webapp-id>.xml registered
- each web app has it's own resource bundle registered with the name "<business-webapp-id>msg"
- we have a common webapp (framework) which renders navigation items for all registered web apps
- each web has the possibility to register nav-items.
- the framework now tries to aggregate all resource bundles into one big ressource bundles during the phase where the current navigation items will be returned.
I found out that the method "getItems()" on our navigation bean is called before the switchLocale method is triggered and therefore the switching of the locale is too late.
Now I try to ask if there is the possibility to switch the locale with the h:commandLink or maybe with a h:outputLink so that the switching of the locale take place before the backing bean method for "getItems()" is triggered?
I hope that I have explained my "challenge" so that it is understandable.