Monday, August 6, 2012

Spring Resolving Text Messages (Internationalization)


resource.properties:
country = India

resource_en_US.properties:
country = United States

resource_en_GB.properties:
country = United Kingdom

resource_de_DE.properties:
country = Germany

resources.xml:
<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-3.0.xsd">
      <bean id="messageSource"
            class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename">
                  <value>resource</value>
            </property>
      </bean>
</beans>

Test.java:
package com.lnn.internationalization;

import java.util.Locale;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
      private static final String COUNTRY = "country";
      public static void main(String[] args) {
            Locale.setDefault(Locale.ROOT);
            ApplicationContext context = new ClassPathXmlApplicationContext("resources.xml");
            System.out.println(context.getMessage(COUNTRY, new Object[0], Locale.US));
            System.out.println(context.getMessage(COUNTRY, new Object[0], Locale.GERMANY));
            System.out.println(context.getMessage(COUNTRY, new Object[0], Locale.UK));
            System.out.println(context.getMessage(COUNTRY, new Object[0], Locale.CANADA));
      }
}

No comments:

Post a Comment