Walter
Walter

Reputation: 235

JSF 2.0 - switching locale with h:commandLink - message bundle not in synch

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:

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.

Upvotes: 3

Views: 2756

Answers (1)

BalusC
BalusC

Reputation: 1108852

I'm not sure how and why this problem is caused. You can try the following in order to force JSF to recreate the view. Instead of returning null on action methods, return an empty string or force a redirect to the same view.

public String switchToDE() {
    // ...
    return "";
}

or

public String switchToDE() {
    // ...
    return FacesContext.getCurrentInstance().getViewRoot().getViewId() + "?faces-redirect=true";
}

Unrelated to the concrete question, if your environment supports EL 2.2, it would be more DRY if you use a single action method for this. E.g.

public String switchLanguage(String language) {
    // ...
}

with

<h:commandLink action="#{languageBean.switchLanguage('EN')}">English</h:commandLink>

Upvotes: 2

Related Questions