Now I will guide you how to performing Localization in Spring using Java Configuration, this is our goal :

Localization Message (English)
as default page will showing simple text hello world in English, and when you click “In” at the top-right, these sentence will translate into Bahasa Indonesia.

Localization Message (Indonesia)
Before we start to write code, firstly I would like to show you the project structure, because we don’t use xml configuration, so we write all configuration in Java.

Project Structure
Okay and now it’s time to coding … First create Class ConfigManager.java to make configuration is more easily, please see picture above for the detail package.
ConfigManager.java
package com.hidupbersahaja.rnd.sys.config;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
public class ConfigManager {
private static PropertiesConfiguration config = null;
static {
try {
config = new PropertiesConfiguration();
config.setBasePath("config");
config.setFileName("config/config.properties");
config.load();
} catch (ConfigurationException e) {
throw new RuntimeException("Please configure config.properties properly", e);
}
}
private ConfigManager(){
}
public static Configuration getConfiguration(){
return config;
}
}
then write some code at your Web Application Config Class :
WebAppConfig.java
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.hidupbersahaja.rnd" })
public class WebAppConfig extends WebMvcConfigurerAdapter{
private org.apache.commons.configuration.Configuration _config = ConfigManager.getConfiguration();
@Bean
public ReloadableResourceBundleMessageSource messageSource(){
ReloadableResourceBundleMessageSource bundleSource = new ReloadableResourceBundleMessageSource();
bundleSource.setDefaultEncoding("UTF-8");
bundleSource.setBasename(_config.getString("locale.language.basename"));
return bundleSource;
}
@Bean
public CookieLocaleResolver localeResolver(){
CookieLocaleResolver localeResolver = new CookieLocaleResolver();
localeResolver.setDefaultLocale(new Locale(_config.getString("locale.language.default")));
localeResolver.setCookieName("localCookie");
localeResolver.setCookieMaxAge(3600);
return localeResolver;
}
}
at the config.poperties, you can set some configuration such as default language, language param, etc
config.properties
##################################################################################### # SYSTEM UTILITY ##################################################################################### locale.language.default=en locale.language.basename=classpath:language/messages locale.language.param=lang
create 2 files properties that represent message for each language, such as messages_en.properties for English and messages_in.properties for Bahasa Indonesia, please see project structure for the detail package
messages_en.properties
hello.world=Hello World from INDONESIA !
messages_in.properties
hello.world=Hallo Dunia dari INDONESIA !
at the index.jsp
<%@ include file="../jsp/common/taglibs.jsp"%> <spring:message code="hello.world" />
And the last for the button chooser EN / IN you can add parameter language (currentURL?lang=en)
<ul class="nav navbar-nav navbar-right">
<%
String currentUrl = request.getAttribute("javax.servlet.forward.servlet_path").toString();
%>
<li class="active">
<a href="${currentUrl }?lang=en">En</a>
</li>
<li>
<a href="${currentUrl }?lang=in">In</a>
</li>
</ul>
for the detail list country you can check at :
http://www.java2s.com/Code/Java/I18N/iso6392languagecodecsv.htm
That’s all, Have a nice Weekend !
