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

2

20

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
Valoniah answered 20/10, 2016 at 21:51 Comment(2)
What stops you from using your preferred structure. I don't see the question here to be honest.Monastery
In the samples I saw only one file and I'm a little bit confuse about how use more filesValoniah
E
40

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;
}
Erlond answered 21/10, 2016 at 5:47 Comment(4)
Nice.. it's simple but I was not getting how to do!! Thanks!Valoniah
if anyone is stuck doing this, please note that the file extension .properties MUST NOT be added, else Spring will not use that file. So, no messageSource.setBasename("classpath:/messages/validation/message.properties"); :(Magnify
@Ali Dehghani: If I follow your second approach and have different MessageSources with different "names". I want to use the <spring:message> tag in a jsp. How is dermined from which MessageSource the property is loaded?Parlour
Using the second approach, how would you access those different messageSources using the JSTL spring message tag? I don't see any way to specify the beanName.Brannon
E
0

you could also use widlcards:

@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource
            = new ReloadableResourceBundleMessageSource();
    messageSource.setBasenames(
            "classpath:/messages/**/*_messages"
    );
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}
Elyseelysee answered 18/4 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.