0

I am trying to practice the internationalization flowwork with JSF framework but I am facing problem with changing the language in the browser when entering en in the InputText field so that no language changing is being taken by the browser.

How can I change the browser language when entering en in the inputText?

index.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core">

<f:view locale="#{sessionBean.locale}">
    <head>
        <meta charset="ISO-8859-1" />
        <title>Insert title here</title>
    </head>
    <body>


        <H1>#{locale['welcome']}</H1>
        <BR />
        <BR />
        <H2>#{locale['login.or.register']}</H2> 

        <h:form>
          <h:inputText value="#{sessionBean.lang}" required="true"/>
          <br/>
          <h:commandButton value="Change" action="#{sessionBean.set()}"/>
          <h:commandLink value="Log out" action="#{sessionBean.logout()}"/>

        </h:form>
    </body>
</f:view>
</html>

SessionBean

 package com.beans;

import java.io.Serializable;
import java.util.Locale;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

@ManagedBean
@SessionScoped
public class SessionBean implements Serializable {

    private String lang;
    private Locale locale;

    @PostConstruct
    public void init() {
        locale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
        String initLang = locale.getLanguage();
        System.out.println("SessionBean - init() - initLang: " + initLang);

    }

    public String set() {
        locale = new Locale(lang);
        String language1 = locale.getLanguage();
        FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
        String language2 = FacesContext.getCurrentInstance().getViewRoot().getLocale().getLanguage();

//      Locale locale2 =FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
//      String initLang2 = locale2.getLanguage();

        System.out.println("SessionBean - set(): " + "language1: " + language1 + " ,language2: " + language2);
        return "/index?faces-redirect=true";

    }

    public Locale getLocale() {
        System.out.println("SessionBean - getLocaleBean()");
        return locale;
    }

    public String logout() {
        HttpSession ses = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        ses.invalidate();
        System.out.println("SessionBean - logout()");
        return "/index?faces-redirect=true";
    }

    public String getLang() {
        System.out.println("SessionBean - getLang()");
        return lang;
    }

    public void setLang(String lang) {
        this.lang = lang;
        System.out.println("SessionBean - setLang()");
    }

}

output of the SessionBean class

2016-07-14T22:43:18.054+0200|Information: SessionBean - init() - initLang: de
2016-07-14T22:43:18.054+0200|Information: SessionBean - getLocaleBean()
2016-07-14T22:43:18.057+0200|Information: SessionBean - getLocaleBean()
2016-07-14T22:43:18.071+0200|Information: SessionBean - getLang()
2016-07-14T22:43:18.073+0200|Information: SessionBean - setLang()
2016-07-14T22:44:05.105+0200|Information: SessionBean - set(): language1: en ,language2: en
2016-07-14T22:44:43.181+0200|Information: SessionBean - init() - initLang: de
2016-07-14T22:44:43.181+0200|Information: SessionBean - getLocaleBean()
2016-07-14T22:44:43.184+0200|Information: SessionBean - getLocaleBean()
2016-07-14T22:44:43.191+0200|Information: SessionBean - getLang()

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">

    <application>
       <locale-config>
        <default-locale>en</default-locale>
        <supported-locale>de</supported-locale>     
      </locale-config>

      <resource-bundle>
       <base-name>
         locales.locale
       </base-name>      
       <var>
         locale
       </var>
      </resource-bundle>

    </application>

</faces-config>

locale_en.properties

welcome = Welcome to MyApp.com
login.or.register = Please login or register

locale_de.properties

welcome = Herzlich Willkommen zu MyApp.com
login.or.register = Bitte Einlogen oder Registerieren
Cœur
  • 37,241
  • 25
  • 195
  • 267
Mr Asker
  • 2,300
  • 11
  • 31
  • 56
  • First I have changed `LocalBean - init()` to `SessionBean - init()`. Yes it is being called twice. I added some more outputs of the methods in SessionBean class. Somehow the locale `en` is not being set correctly since when calling the `init()` again I am getting `de`. I just want to mention that the output of the outcommented `initLang2` in the set() is ´de´ . – Mr Asker Jul 14 '16 at 21:00
  • @BalusC: And how can I managed it with the SessionBean? Since I tried it with RequestBean and it does not work? – Mr Asker Jul 14 '16 at 21:42

0 Answers0