Internationalization in Spring

In this post under Spring Core, I will show with example how to achieve Internationalization using Spring framework.

Internationalization in Spring is achieved using “MessageSource” interface and its default implementations.

Whenever an “ApplicationContext” instance is created, Spring searches for bean definition with name “messageSource”, when found associate it with “ApplicationContext” instance.

“ApplicationContext” interface implements “MessageSource” interface.

“MessageSource” interface has 3 overloaded “getMessaage” methods.

So “ApplicationContext” inherits those overloaded “getMessaage” methods.

Whenever a call is made to overloaded “getMessaage” methods using “ApplicationContext” interface, the call is delegated to actual “MessageSource” bean created during Spring application context initialization.

For our example we use the “ResourceBundleMessageSource” implementation of “MessageSource” interface.

“ResourceBundleMessageSource” internally uses JDK “ResourceBundle” implementation. So it works exactly like JDK ResourceBundle.

We also need two properties files named “messages.properties” and “messages_fr.properties” with below contents

messages.properties

greeting=Hello!
welcome=Welcome to our application.

messages_fr.properties

greeting=Bonjour !
welcome=Bienvenue dans notre application.

Below is the complete main class for your reference

Main class

package core.package40;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;

import java.util.Locale;

@Configuration
@ComponentScan(basePackages = "core.package40")
public class Example40 {
    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Example40.class);

        System.out.println("English Locale: " + applicationContext.getMessage("welcome", new Object[0], Locale.ENGLISH));
        System.out.println("French Locale: " + applicationContext.getMessage("welcome", new Object[0], Locale.FRENCH));
    }
}

In the above code, “messageSource” method, I create an instance of “ResourceBundleMessageSource” class, set the basename and the default encoding.

In the main method, I create an instance of “AnnotationConfigApplicationContext”.

While creating the instance of “AnnotationConfigApplicationContext”, Spring searches for bean named “messageSource” and it gets the instance that we created and associate it with “AnnotationConfigApplicationContext” instance.

At line 28, we call “getMessage” with three arguments
1) the message key name
2) arguments
3) the locale

At line 29, we call “getMessage” again for french locale

In this way we can achieve internationalization in Spring.

Below is the output

Output

English Locale: Welcome to our application.
French Locale: Bienvenue dans notre application.

Leave a comment