Changing Locale with AJAX example.

Lets see!

I already posted an example here. However, clicking a button to change the Locale is not very useful is it? Here is my new example:

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>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.1.v20140609</version>
                <configuration>
                    <scanIntervalSeconds>2</scanIntervalSeconds>
                    <webApp>
                        <contextPath>/</contextPath>
                    </webApp>
                </configuration>
            </plugin>
        </plugins>
    </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>

LocaleBean.java
package biz.tugay;
 
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
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 String selectedLanguage;
 
    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();
        selectedLanguage = locale.getLanguage();
    }
 
    public Locale getLocale() {
        return locale;
    }
 
    public String getSelectedLanguage() {
        return selectedLanguage;
    }
 
    public void setSelectedLanguage(String selectedLanguage) {
        this.selectedLanguage = selectedLanguage;
 
    }
 
    public void changeLocale() {
        if (selectedLanguage.equals("en")) {
            locale = localeEn;
        } else {
            locale = localeTr;
        }
    }
}

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>
        <h:form id="changeLocaleForm"
                prependId="false"
                style="float: right">
            <h:selectOneMenu id="changeLocalListBox"
                             value="#{localeBean.selectedLanguage}">
                <f:selectItem id="localeTrSelectItem"
                              itemValue="tr"
                              itemLabel="TR"/>
                <f:selectItem id="localeEnSelectItem"
                              itemValue="en"
                              itemLabel="EN"/>
                <f:ajax event="valueChange"
                        execute="@form"
                        listener="#{localeBean.changeLocale}"
                        render="@all"/>
            </h:selectOneMenu>
        </h:form>
        <h1>#{msgs.applicationTitle}</h1>
        <br/>
        <h:link id="otherpagelink"
                value="#{msgs.takeMeToTheOtherPage}"
                outcome="otherpage"/>
        <br/>
    </h:body>
</f:view>
</html>

otherpage.xml
<?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>

And as usual, in action: