Showing posts with label spring internationalization. Show all posts
Showing posts with label spring internationalization. Show all posts

Sunday, 2 December 2018

Spring mvc: Internationalization support part-2

This is continuation to my previous post. In my previous post, I explained how the spring mvc application supports internationalization using browser language. One problem with that approach is, it is browser centric. It displays the information based on browser language.

But there is a case, where my browser preferred language is English, but i want  my webpage to be displayed in German. In this post, I am going to show, how can you achieve that.

It is very simple, just add below lines in the dispatcher servlet of my previous application.


         <bean id="localeResolver"
         class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />

         <mvc:interceptors>
                  <bean
                  class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
                           <property name="paramName" value="siteLanguage" />
                  </bean>
         </mvc:interceptors>

org.springframework.web.servlet.i18n.LocaleChangeInterceptor
Interceptor that allows for changing the current locale on every request, via a configurable request parameter (default parameter name: "locale").

org.springframework.web.servlet.i18n.CookieLocaleResolver
It uses a cookie sent back to the user in case of a custom setting, with a fallback to the specified default locale or the request's accept-header locale.

Find the below working application.

HelloWorldController.java
package com.sample.myApp.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorldController {

 @RequestMapping("/welcome")
 public String getHelloMessage() {
  return "welcome";
 }

}

Create welcome.jsp file under WEB-INF/jsp folder.


welcome.jsp

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World Spring Web MVC</title>
</head>
<body>
 <h1>
  <spring:message code="welcomeMessage" />
 </h1>
</body>
</html>
Create appMessages_en.properties, appMessages_de.properties files under WEB-INF/propertyFiles.

appMessages_en.properties
welcomeMessage=Good Morning......

appMessages_de.properties
welcomeMessage=Guten Morgen......

Create web.xml, HelloWorld-servlet.xml files under WEB-INF folder.


web.xml
<web-app id="WebApp_ID" version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 <display-name>Spring MVC Hello WorldApplication</display-name>

 <servlet>
  <servlet-name>HelloWorld</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>HelloWorld</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

</web-app>

HelloWorld-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

 <mvc:annotation-driven />

 <context:component-scan
  base-package="com.sample.myApp" />

 <bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>


 <bean id="messageSource"
  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  <property name="basename"
   value="WEB-INF/propertyFiles/appMessages" />
 </bean>

 <bean id="localeResolver"
  class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />

 <mvc:interceptors>
  <bean
   class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
   <property name="paramName" value="siteLanguage" />
  </bean>
 </mvc:interceptors>


</beans>


Create index.jsp file under webapp folder.

index.jsp
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Welcome Page</title>
</head>

<body>
 <h2>Click on below button to get welcome page</h2>
 <form method="post" action="/springdemo/welcome?siteLanguage=en"
  id="form1">

  <input type="submit" value="getEnglishMessage"
   style="font-size: 18px;" />
 </form>

 <form method="post" action="/springdemo/welcome?siteLanguage=de"
  id="form2">
  <input type="submit" value="getGermanMessage" style="font-size: 18px;" />
 </form>
</body>
</html>

Total project structure looks like below.

Run the application on server. You can able to see below screen.
If you click on ‘getEnglishMessage’ button, you can able to see below message.


If you click on ‘getGermanMessage’, you can able to see below message.

If you click on ‘getGermanMessage’, you can able to see below message.

Previous                                                 Next                                                 Home

Spring mvc: Internationaliztion, Localization support

Now a days, any typical web application is supporting multiple human languages. For example, when the application is opened from germany, then all the information is displayed in german. If the application is opened from America, the information is displaed in English. User can choose his favorite langauges, based on the language selection we need to display the webpage.

In this post, I am going to explain, how spring supports internationalization.

Step 1: Create properties file in below format.
{PROPERTIES_FILE_NAME}_{LOCALE_CODE}.properties

Ex:
appMessages_en.properties : Specifies the messages in English languages
appMessages_de.properties : Specifies the messages in german language
appMessages.properties : It is the default locale file.

If a key does not exist in a certain requested locale, then the application will fall back to the default locale value.

Step 2:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
         <property name="basename" value="WEB-INF/propertyFiles/appMessages" />
</bean>

Find the below working application.

HelloWorldController.java
package com.sample.myApp.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorldController {

 @RequestMapping("/welcome")
 public String getHelloMessage() {
  return "welcome";
 }

}


Create welcome.jsp file under WEB-INF/jsp folder.

welcome.jsp
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World Spring Web MVC</title>
</head>
<body>
 <h1><spring:message code="welcomeMessage" /></h1>
</body>
</html>


Create appMessages_de.properties, appMessages_en.properties files under WEB-INF/propertyFiles.

appMessages_de.properties
welcomeMessage=Guten Morgen......


appMessages_en.properties
welcomeMessage=Good Morning......

Create web.xml, HelloWorld-servlet.xml files under WEB-INF folder.


web.xml
<web-app id="WebApp_ID" version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 <display-name>Spring MVC Hello WorldApplication</display-name>

 <servlet>
  <servlet-name>HelloWorld</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>HelloWorld</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

</web-app>

HelloWorld-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

 <mvc:annotation-driven />

 <context:component-scan
  base-package="com.sample.myApp" />

 <bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>


 <bean id="messageSource"
  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  <property name="basename"
   value="WEB-INF/propertyFiles/appMessages" />
 </bean>


</beans>

Create index.jsp file under webapp folder.


index.jsp
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Welcome Page</title>
</head>

<body>
 <h2>Click on below button to get welcome page</h2>
 <form method="post" action="/springdemo/welcome" id="f1">
  <input type="submit" name="submit" value="submit"
   style="font-size: 18px;" />
 </form>
</body>
</html>

Project structure looks like below.
Run the application on server.

Click on submit button.

Now let’s change the browser default language to german and re hit /welcome page.

For example, follow below steps to change the language to german in firefox.

1.   Tools -> Options

2.   Search for language in the find box.

3.   Click on Choose… button, it opens below window.




Use the dropdown ‘Select a language to add…’.




Select German and click on Add button.


Click on OK button.

Now, when I rehit the url /welcome, I seen the welcome message in germany.


Previous                                                 Next                                                 Home