2

I have this bean:

@ManagedBean(name="langListing")
@ViewScoped
public class LangListing implements Serializable
{
    private List<SelectItem> languages = new ArrayList<SelectItem>();
    private String language;

    public LangListing() {
       createLangs(); // lazy loading
    }   

    public void createLangs()
    {   
       languages.add(new SelectItem("en", FacesUtil.getResourceBundle().getString("LANG_LABEL_01"))); // English
       languages.add(new SelectItem("fr", FacesUtil.getResourceBundle().getString("LANG_LABEL_02"))); // French
    }

    public List<SelectItem> getLanguages() { 
       return languages;
    }

    public String getLanguage()
    {
       if (FacesContext.getCurrentInstance().getViewRoot().getLocale() != null) {
          language = FacesContext.getCurrentInstance().getViewRoot().getLocale().toString();
       } else {
          language = "en";
       }

       return language;
    }

    public void setLanguages(List<SelectItem> languages) {
       this.languages = languages;
    }    

    public void setLanguage(String language) {
       this.language = language;
    }

    public void changeLocale(AjaxBehaviorEvent event) {
       FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(getLanguage()));
    }
 } 

I'm calling the bean methods from:

<h:selectOneMenu id="selectLang" immediate="true" value="#{langListing.language}">
    <f:ajax listener="#{langListing.changeLocale}" render="@form" />
    <f:selectItems value="#{langListing.languages}" />
</h:selectOneMenu>

The problem is I can't get the codes to change locale from English to French. Can anyone see the problem? Please help.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ChuongPham
  • 4,761
  • 8
  • 43
  • 53

1 Answers1

1

You're setting it with the value of getLanguage() which returns the current UIViewRoot locale or otherwise just en. It does not return the selected language value.

You need to revise the logic in your bean. Here's a rewrite:

@ManagedBean(name="langListing")
@ViewScoped
public class LangListing implements Serializable {
    private List<SelectItem> languages = new ArrayList<SelectItem>();
    private String language;

    public LangListing() {
       languages.add(new SelectItem("en", FacesUtil.getResourceBundle().getString("LANG_LABEL_01"))); // English
       languages.add(new SelectItem("fr", FacesUtil.getResourceBundle().getString("LANG_LABEL_02"))); // French
       language = FacesContext.getCurrentInstance().getViewRoot().getLocale().toString();
    }   

    public List<SelectItem> getLanguages() { 
       return languages;
    }

    public String getLanguage() {
       return language;
    }

    public void setLanguages(List<SelectItem> languages) {
       this.languages = languages;
    }    

    public void setLanguage(String language) {
       this.language = language;
    }

    public void changeLocale(AjaxBehaviorEvent event) {
       FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(language));
    }
 } 

Note that there are more potential flaws/holes with this approach. The bean is view scoped instead of session scoped and you don't seem to be using <f:view> (since that expects a Locale, not String). I'd warmly recommend to get yourself through this answer to align it out.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @BalusC Ah, I see. You're a genius, man!!! I have another question relating to Ajax and is related to the above issue - can I ask it here or do I have to start a new question? – ChuongPham Feb 17 '11 at 17:24
  • You're welcome. For each standalone issue you should ask a new question. – BalusC Feb 17 '11 at 17:24
  • @BalusC I have two questions regarding the link you supplied above: 1) Can you still use Ajax method or it's redundant because of onchange event? 2) The java.util.Locale class only support a handful of locales and, say, if I want to provide Danish support, how would I use java.util.Locale to do it? – ChuongPham Feb 17 '11 at 17:59
  • 1) See your [other question](http://stackoverflow.com/questions/5032455/jsf-2-ajax-form-not-completely-rendered). 2) Just do `new Locale(language)`, exactly as in the linked answer. The absence of a static final constant like `Locale.DANISH` doesn't mean that you cannot use the desired language. Those constants are a leftover of old API and only for top of most used languages in world only. – BalusC Feb 17 '11 at 18:00