How To Do Internationalisation In Java

Posted By : Vipul Pandey | 17-May-2018

In this blog I am going to describe how to achieve internationalization in java, internationalization helps in achieving the locale wise messages in our application. Internationalization is the process of designing and developing an application so that it can be adapted to various languages and regions. The term internationalization is abbreviated as i18n, because there are 18 letters between the first "i" and the last "n."

In spring boot application the internationalization is done by following ways :

  1. Create a custom configuration class LocaleConfig which reads the message source and identify the current request locale and depict if there is any change in locale and get the message according to locale.
  2. Create a  message service which gets the message from the message.properties file according to message code and locale.
  3. Creates message && message_XX .properties file according to requirement.

Here is the locale file configuration 

@Configuration
public class LocaleConfig implements WebMvcConfigurer {
	 /**
     * This method is use to get MessageSource.
     * @param Nothing.
	 * @return MessageSource.
     */
    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames("classpath:messages/messages");
        messageSource.setUseCodeAsDefaultMessage(true);
        messageSource.setDefaultEncoding("UTF-8");
        messageSource.setCacheSeconds(3600);
        return messageSource;
    }

    /**
     * This method is use to get LocaleResolver and sets the default locale to US.
     * @param Nothing.
	 * @return LocaleResolver.
     */
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.US);
        return slr;
    }

    /**
     * This method is use to get LocaleChangeInterceptor. The lang parameter is the parameter which is passed as Query params and gets the changes or currently set language. viz if you want to use australian language the the request contains lang=au.
     * @param Nothing.
	 * @return LocaleChangeInterceptor.
     */
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        lci.setParamName("lang");
        return lci;
    }

    /* (non-Javadoc)
     * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addInterceptors(org.springframework.web.servlet.config.annotation.InterceptorRegistry)
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }
}

This is Locale service which is used to get the message according to code and implementation class of locale sevice and @service annotation is used to make the class as service which is scanned by spring.

public interface LocaleService {
	public String getMessage(String code);
}

@Service
public class LocaleServiceImpl implements LocaleService{
	
	@Autowired
    private MessageSource messageSource;

	@Override
	public String getMessage(String code) {
		Locale locale = LocaleContextHolder.getLocale();
        return this.messageSource.getMessage(code, null, locale);
	}
}

Now Under resource folder create a package named messages and 2 files named message.properties and message_au.properties
In file message.properties write
       greetings= Hello

In file message_au.properties write 
     greetings=G'day mate

Now when your request contains the locale US it will sends the greetings as Hello and when the Locale is AU then it will change the greetings text to G'day mate

About Author

Author Image
Vipul Pandey

Vipul Pandey is a good team-player & developing application on MEAN and java spring boot. Vipul loves to keep rotating fingers in his keyboard until he creates somethings inspiring.Hobbies are playing cricket ,swimming & photography.

Request for Proposal

Name is required

Comment is required

Sending message..