Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle multiple files and messages for internationalization in Spring?

Some articles about Spring internationalization tell how to swap messages passing the locale and etc, but I only found use cases that contains a few messages..

  • How can I organize and use internationalization files per context? (validation, view messages, default messages, business messages)

  • I know that Spring uses the pattern (name of message file defined) + locale. e.g: message_zh_CN. How can I have files per context knowing about this behavior?

What I think it should be:

resources
`-- messages
    |-- validation
    |   |-- message_locale.properties
    |   `-- message_locale2.properties
    |-- business
    |   |-- message_locale.properties
    |   `-- message_locale2.properties
    `-- view
        |-- message_locale.properties
        `-- message_locale2.properties

OR:

resources
`-- messages
    |-- validation
    |   |-- validation_locale.properties
    |   `-- validation_locale2.properties
    |-- business
    |   |-- business_locale.properties
    |   `-- business_locale2.properties
    `-- view
        |-- view_locale.properties
        `-- view_locale2.properties
like image 237
Lucas Avatar asked Oct 20 '16 21:10

Lucas


People also ask

Where can the message properties be configured to simplify the internationalization in spring boot?

By default, a Spring Boot application will look for message files containing internationalization keys and values in the src/main/resources folder. The file for the default locale will have the name messages. properties, and files for each locale will be named messages_XX. properties, where XX is the locale code.


1 Answers

You can either define a global MessageSource for all those different message files. This approach is practical using the setBasenames method:

@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = 
                                               new ReloadableResourceBundleMessageSource();
    messageSource.setBasenames("classpath:/messages/business/message", 
                               "classpath:/messages/validation/message",
                               "classpath:/messages/view/message");

    return messageSource;
}

This approach makes sense if your message keys are unique across all files, e.g. business-12 key only exits in business related message sources. Otherwise, it's better to define one MessageSource per context and inject them according to your context:

@Bean
public MessageSource businessMessageSource() {
    ReloadableResourceBundleMessageSource messageSource = 
                                               new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("classpath:/messages/business/message");

    return messageSource;
}

@Bean
public MessageSource validationMessageSource() {
    ReloadableResourceBundleMessageSource messageSource = 
                                               new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("classpath:/messages/validation/message");

    return messageSource;
}

@Bean
public MessageSource viewMessageSource() {
    ReloadableResourceBundleMessageSource messageSource = 
                                               new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("classpath:/messages/view/message");

    return messageSource;
}
like image 177
Ali Dehghani Avatar answered Nov 15 '22 23:11

Ali Dehghani