Blog Image

Example On Internationalization  

Here we are going to explore about spring provided Internationalization to know how we can work with Internationalization in our projects.

Lets see the example:
Technologies used :-
1) java (1.8).
2) maven (3.5.2).
3) Spring (3.0.0).

Project Structure

Execute below command for creating maven project.
mvn archetype:generate -DgroupId=com.sjg
-DartifactId=example-on-internationalization
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false

Below is the maven project structure with packages and java src files.

Classes
Test.java
package com.sjg.test;

import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("com/sjg/common/application-context.xml");
        String message = context.getMessage("field.incorrect", new Object[] { "firstName" }, "No Message Found",
                Locale.CANADA_FRENCH);
        System.out.println(message);
    }
}
application-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans";
    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.xsd">;

    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list value-type="java.lang.String">
                <value>messages</value>
                <value>errors</value>
            </list>
        </property>
    </bean>

</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">;
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.sjg</groupId>
  <artifactId>example-on-internationalization</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>example-on-internationalization</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.srimanjavagroup.com</url>;

  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.0.0.RELEASE</version>
        </dependency>
    </dependencies>
</project>
errors.properties
field.incorrect={0} should be alphanumeric

errors_fr_CA.properties
field.incorrect={0} should be alphanumeric (Arabic)
messages_fr_CA.properties
index.title=Welcome to Air Ticketing System (Arabic)!
messages.properties
index.title=Welcome to Air Ticketing System!

Please find attached the example_on_internationalization.zip as an maven project as a reference. Feel free to pass your comments in the blog.

About author

User Image
Sriman

A software developer on JDK and JEE platform. I am passionate about Java technology and always an explorer and learner in new technologies in Java. I have experience on Open Source technologies like Struts, Spring, Jsf etc.. and even strong knowledge on Integration/Distributed world like Ejb, Web Services and Restful Services.

0

-Comments

Be the first person to write a comment for this Blog
Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.