Spring — I18N (Internationalization) | Code Factory

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)
<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>
message=Hello
message=नमस्कार
message=નમસ્તે
<%@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/

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Get a public URL to your Localhost — The easy way

Investment Series: PagerDuty, Disqus, and MemSQL

Sketch of Artificial Lake in the Middle Neighbourhood

Spring MVC — Multi Action Controller | Code Factory

Imperium Empires AMA #2 — Technical Deep-Dive with Cliff Yung, CEO

Learn How to Set Up Docker For Windows

Scrum Master, what are you doing with the team?

How to use wsl Ubuntu Terminal with Ruby, RUBY ON RAILS, MYSQL AND POSTGRESQL DATABASES ,GIT .

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Explore Prototype Design Pattern

Visitor Design Pattern in Java

Circuit Breaker Pattern With Netflix-Hystrix: Java

Exception handling In Java.