Spring — I18N (Internationalization) | Code Factory

Code Factory
2 min readJan 28, 2021

--

Donate : Link

WordPress Blog : Link

Applications… : Link

Spring Tutorial Index Page: Link

  • I18N (Internationalization): We can provide language support by using I18N.
  • L10N (Localization): We should provide business support or validation support.
  • Internationalization enables the application to target any language in the world.
  • Localization is a part of the Internationalization and it enables the application to render in a targeted local language.
  • By using J2EE IOC container ApplicationContext container provide I18N.
  • We have to create bean of ResourceBundleMessageSource class in xml file and id must have “messageSource”.

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Dynamic Web Project
  • Create Spring-I18N project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs (Used 2.X jars)
    - commons-logging-X.X.jar
    - spring-beans-X.X.X.jar
    - spring-context-X.X.X.jar
    - spring-core-X.X.X.jar
  • Also add jar files in WEB-INF/lib folder
  • * You can find dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)

spring.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="com/codeFactory/resources/data" />
</bean>
</beans>

data.properties

message=Hello

data_hi.properties

message=नमस्कार

data_gu.properties

message=નમસ્તે

index.jsp

<%@page import="org.springframework.context.support.ClassPathXmlApplicationContext"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@page import="java.util.Locale"%>
<%
String language = request.getHeader("accept-language");
Locale locale = new Locale(language);
ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");
String message = context.getMessage("message", null, locale);
%>
<form action="./hello">
<%=message %>: <input type="text" name="name" />
<input type="submit" value="Submit">
</form>
  • Change browser language and refresh page.
  • This is just an example, it is not good approach for web application.
  • Right click on Spring-I18N -> Run As -> Run on Server
  • Hit http://localhost:8080/Spring-I18N/

--

--