Make writing a habit together! This is the third day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details. For an internationalization-enabled (I18N) application, it needs to be able to parse text messages for different locales. Spring’s application context is able to parse text messages in the target locale by key. Typically, messages for a locale should be stored in a separate properties file. This property file is called a resource bundle.

MessageSource is an interface that defines several methods for parsing messages. The ApplicationContext interface extends this interface so that all application contexts can parse text messages.

The application context delegates message resolution to a bean with the exact name messageSource. ResourceBundleMessageSource is the most common MessageSource implementation, it parses the message from resources in different areas of the package

A, use ResourceBundleMessageSource parse the message

For example, you can create the following resource bundle, messages _ en _ us. The English language in America. The resource bundle is loaded from the root of the classpath.

1.1 Creating resource Packages

Create a filename messages-en-us.properties in the classpath of your Spring application.

messages_en_US.properties
msg.text=Welcome to mine.com
Copy the code

1.2 configuration ResourceBundleMessageSource bean

Will now ResourceBundleMessageSource class configuration for the bean name “messageSource”. In addition, you must specify the resource bundle for ResourceBundleMessageSource base name.

applicationContext.xml <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">  <property name="basename"> <value>messages</value> </property> </bean>Copy the code

2. Search sequence of resource packages

  1. For this messageSource definition, the resource bundle message is EN _ US if you are looking for text messages in the US locale where the preferred language is English. Language and country matching attributes will be given priority.
  2. If there is no such resource bundle or the message cannot be found, then a message _ en. Only attributes that match the language will be considered.
  3. If the resource bundle is still not found, the default messages.properties for all locale Settings will eventually be selected.

3. Demo- How to use MessageSource

3.1 Obtaining messages directly

Now to check if the message can be loaded, run the following code:

TestSpringContext.java public class TestSpringContext { @SuppressWarnings("resource") public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); String message = context.getMessage("msg.text", null, Locale.US); System.out.println(message); }}Copy the code

Output:

// Console outputs Welcome to mine.comCopy the code

3.2 Implement MessageSourceAware interface in bean

Good, so we can parse the message. However, here we access the ApplicationContext directly, so it looks easy. If we want to access messages in a bean instance, we need to implement either the ApplicationContextAware interface or the MessageSourceAware interface.

The code looks something like this:

EmployeeController.java
public class EmployeeController implements MessageSourceAware {
 
    private MessageSource messageSource;
 
    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }
 
    //Other code
}
Copy the code

You can now use this messageSource instance to retrieve/parse text messages defined in the resource file.