开发者

newbie: help using mod_rewrite for localised website

Newbie: help switching locale with JSF

Hi, I hope someone can help, I am having problems switching between locales in particular en_GB to en_US and en_US to en_GB for my website, however all other two character locales are switching fine.

faces_config.xml snippet

<default-locale>en_gb</default-locale>
<supported-locale>en_us</supported-locale>
<supported-locale>en_gb</supported-locale>
<supported-locale>cy</supported-locale>
<supported-locale>es</supported-locale>
<supported-locale>fr</supported-locale>

web page snippet

  <f:view locale="#{localeBean.locale}">
     <h:body>
       <h:form>
         <h:selectOneMenu value="#{localeBean.language}" onchange="submit()">
           <f:selectItem itemValue="en_GB" itemLabel="English (British)" />
           <f:selectItem itemValue="en_US" itemLabel="English (American)" />
           <f:selectItem itemValue="cy" itemLabel="Cymraeg (British)" />
           <f:selectItem itemValue="es" itemLabel="Español (España)" />
           <f:selectItem itemValue="fr" itemLabel="Français (France)" />
         </h:selectOneMenu>
      </h:form>
    </h:body
  </f:view>

Java bean

public class LocaleBean {

private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();

public Locale getLocale() {
    return locale;
}

public String getLanguage() {
    return locale.getLanguage();
}

public void setLanguage(String language) {

    if ( language.equals("en_GB") ) {
        locale = new Locale("en","GB");
    }
    else if ( language.equals("en_US") ) {
        locale = new Locale("en","US");
    }
    else {
        locale = new Locale(language);
    }

 FacesContext.getCurrentInstance开发者_开发百科().getViewRoot().setLocale(locale);
}

}

My problem is when I change the locale to en_US or en_GB the selectItem defaults to en_GB itemLablel, so unless I select either fr, es or cy first I am unable to select either en_US or en_GB locales.

Any help appreciated


The reason why it happens should be obvious:

public String getLanguage() {
    return locale.getLanguage();
}

This will always return language-code only. But your English identifiers are "en_GB" and "en_US" respectively, so it always selects first item.

I believe now, the fix is obvious, isn't it? The modification I recommend is:

public String getLanguage() {
    return locale.toString();
}

That is, unless you are using just the language code somewhere else...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜