如何在Spring中处理多个文件和消息以实现国际化? [英] How to handle multiple files and messages for internationalization in Spring?

查看:126
本文介绍了如何在Spring中处理多个文件和消息以实现国际化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关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)

我知道Spring使用模式(定义的消息文件的名称)+
locale 。 例如:message_zh_CN 。如何知道
关于此行为的每个上下文文件?

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?

我认为它应该是:

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

或者:

resources
`-- messages
    |-- validation
    |   |-- validation_locale.properties
    |   `-- validation_locale2.properties
    |-- business
    |   |-- business_locale.properties
    |   `-- business_locale2.properties
    `-- view
        |-- view_locale.properties
        `-- view_locale2.properties


推荐答案

您可以为所有这些不同的消息文件定义全局 MessageSource 。使用 setBasenames 方法这种方法很实用:

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;
}

如果您的消息密钥在所有文件中都是唯一的,例如 business-12 密钥仅退出与业务相关的消息来源。否则,最好根据您的上下文定义一个 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;
}

这篇关于如何在Spring中处理多个文件和消息以实现国际化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆