Reply To: I18n approach: Language in URL

Splash Forums Rewrite Users I18n approach: Language in URL Reply To: I18n approach: Language in URL

#27479

zebhed
Participant

Very true.

After a few refactorings on the way I came to a similar solution.

Following solution has the advantage that browser language/locale is used as an initial value which may be manually overridden by the user (via a form whose action calls “setLanguage()” ):

@ManagedBean
@SessionScoped
public class RequestorLanguage implements Serializable {

    public static String GET_PARAM_LANGUAGE = "language";
    //data
    private String language;
    private Locale locale;

    @PostConstruct
    public void postConstruct() {
        this.locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
        this.language = locale.getLanguage();
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.locale = new Locale(language);
        this.language = locale.getLanguage();

        FacesContext.getCurrentInstance().getViewRoot().setLocale(this.locale); //HINT: this alone is not sufficient and not remembered by JSF! Locale has to be set for every page request via "f:view"!
    }

    public Locale getLocale() {
        return locale;
    }

}