JSF example where user changes Locale!

Lets see!

Directory layout:

And lets show the source code..

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
    <groupId>biz.tugay</groupId>
 
    <artifactId>darbe</artifactId>
    <packaging>war</packaging>
 
    <version>1.0-SNAPSHOT</version>
    <name>darbe</name>
 
    <url>http://maven.apache.org</url>
 
    <dependencies>
        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>javax.faces-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.2.13</version>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>3.0.0</version>
            <scope>provided</scope>
        </dependency>
 
    </dependencies>
 
    <build>
        <finalName>darbe</finalName>
    </build>
 
</project>

web.xml
<web-app 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-app_3_1.xsd"
         version="3.1">
 
    <servlet>
        <servlet-name>FacesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>FacesServlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
 
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
 
</web-app>

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 wide configurations -->
    <application>
 
        <!-- Resolving resource bundles -->
        <resource-bundle>
            <base-name>biz.tugay.msg</base-name>
            <var>msgs</var>
        </resource-bundle>
 
        <!-- Localisation configuration -->
        <locale-config>
            <default-locale>en</default-locale>
            <supported-locale>tr</supported-locale>
        </locale-config>
 
    </application>
 
</faces-config>

msg.propertites
applicationTitle=Registiration Application
changeLocale=Change Locale
takeMeToTheOtherPage=Take me to the other page!

msg_tr.properties
applicationTitle=Kay\u0131t Olma Uygulamas\u0131
changeLocale=Lokali de\u011Fi\u015Ftir
takeMeToTheOtherPage=Beni di\u011Fer sayfaya g\u00F6t\u00FCr!

LocaleBean.java
package biz.tugay;
 
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import java.util.Locale;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 7/17/2016
 * Time: 5:53 PM
 */
 
@ManagedBean
@SessionScoped
public class LocaleBean {
 
    private Locale locale;
 
    private static final Locale localeTr = new Locale("tr");
    private static final Locale localeEn = new Locale("en");
 
    @PostConstruct
    public void init() {
        locale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
    }
 
    public Locale getLocale() {
        return locale;
    }
 
    public String changeLocale() {
        final UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
        final String language = viewRoot.getLocale().getLanguage();
        if (language.equals("tr")) {
            locale = localeEn;
        } else {
            locale = localeTr;
        }
        return "index.xhtml?faces-redirect=true";
    }
}

index.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!--
    User: Koray Tugay (koray@tugay.biz)
    Date: 7/17/2016
    Time: 12:26 PM
-->
 
<!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="#{localeBean.locale}">
    <h:head>
        <title>#{msgs.applicationTitle}</title>
    </h:head>
    <h:body>
        <h1>#{msgs.applicationTitle}</h1>
        <h:form id="changeLocaleForm"
                prependId="false">
            <h:commandButton id="changeLocaleButton"
                             value="#{msgs.changeLocale}"
                             action="#{localeBean.changeLocale()}"/>
        </h:form>
        <br/>
        <h:link id="otherpagelink"
                value="#{msgs.takeMeToTheOtherPage}"
                outcome="otherpage"/>
    </h:body>
</f:view>
</html>

otherpage.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!--
    User: Koray Tugay (koray@tugay.biz)
    Date: 7/17/2016
    Time: 6:11 PM
-->
 
<!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="#{localeBean.locale}">
    <h:head>
        <title>#{msgs.applicationTitle}</title>
    </h:head>
    <h:body>
        <h1>#{msgs.applicationTitle}</h1>
        <br/>
        <h:link id="otherpagelink"
                value="#{msgs.takeMeToTheOtherPage}"
                outcome="index"/>
    </h:body>
</f:view>
</html>

Finally, lets see how our application works.