Internationalization

Spring Boot 支持本地化消息,以便你的应用程序可以满足不同语言偏好用户的需求。默认情况下,Spring Boot 在类路径的根目录处查找 messages 资源绑定的存在。

Spring Boot supports localized messages so that your application can cater to users of different language preferences. By default, Spring Boot looks for the presence of a messages resource bundle at the root of the classpath.

自动配置在配置的资源绑定的默认属性文件可用时(默认情况下为 messages.properties)生效。如果你的资源绑定仅包含特定于语言的属性文件,则需要添加默认值。如果没有找到与任何已配置的基本名称匹配的属性文件,则不会有自动配置的 MessageSource

The auto-configuration applies when the default properties file for the configured resource bundle is available (messages.properties by default). If your resource bundle contains only language-specific properties files, you are required to add the default. If no properties file is found that matches any of the configured base names, there will be no auto-configured MessageSource.

资源绑定的基本名称以及其他几个属性都可以使用 spring.messages 命名空间进行配置,如下例所示:

The basename of the resource bundle as well as several other attributes can be configured using the spring.messages namespace, as shown in the following example:

spring:
  messages:
    basename: "messages,config.i18n.messages"
    fallback-to-system-locale: false

spring.messages.basename 支持逗号分隔的位置列表,即包限定符或从类路径根目录解析的资源。

spring.messages.basename supports comma-separated list of locations, either a package qualifier or a resource resolved from the classpath root.

如需了解其他受支持的选项,请参见 {code-spring-boot-autoconfigure-src}/context/MessageSourceProperties.java[@16]。

See {code-spring-boot-autoconfigure-src}/context/MessageSourceProperties.java[MessageSourceProperties] for more supported options.