Localization

  • 异常消息可本地化,但开发者消息和日志记录消息则不会。

  • messages.properties 文件包含默认语言消息。

  • 复制并重命名 messages.properties 以支持其他语言。

  • 使用 org.springframework.context.support.ReloadableResourceBundleMessageSource 来注册本地化文件。

  • 设置 org.springframework.context.i18n.LocaleContextHolder 以包含区域设置,例如使用 DispatcherServlet 或 RequestContextFilter。 Spring Security supports localization of exception messages that end users are likely to see.

如果您需要支持其他区域设置,本节包含您需要知道的所有内容。

If you need to support other locales, this section contains everything you need to know.

所有异常消息都可以本地化,包括与身份验证失败和访问被拒绝相关的消息(授权失败)。专注于开发者或系统部署者的异常和日志记录消息(包括不正确的属性、接口合约违规、使用不正确的构造器、启动时间验证、调试级日志记录)不会本地化,而是以硬编码形式包含在 Spring Security 代码中。

All exception messages, including messages related to authentication failures and access being denied (authorization failures), can be localized. Exceptions and logging messages that are focused on developers or system deployers (including incorrect attributes, interface contract violations, using incorrect constructors, startup time validation, debug-level logging) are not localized and instead are hard-coded in English within Spring Security’s code.

spring-security-core-xx.jar 中,您会找到 org.springframework.security 包,该包反过来包含 messages.properties 文件以及一些常见语言的本地化版本。您的 ApplicationContext 应引用此包,因为 Spring Security 类实现 Spring 的 MessageSourceAware 接口,并期望在应用程序上下文启动时依赖注入消息解析器。通常,您只需在应用程序上下文中注册一个 bean 即可引用这些消息。以下列表显示了一个示例:

In the spring-security-core-xx.jar, you find an org.springframework.security package that, in turn, contains a messages.properties file as well as localized versions for some common languages. Your ApplicationContext should refer to this, as Spring Security classes implement Spring’s MessageSourceAware interface and expect the message resolver to be dependency injected at application context startup time. Usually, all you need to do is register a bean inside your application context to refer to the messages. The following listing shows an example:

<bean id="messageSource"
	class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:org/springframework/security/messages"/>
</bean>

messages.properties 的名称符合标准资源包,并表示 Spring Security 消息支持的默认语言。此默认文件采用英语。

The messages.properties is named in accordance with standard resource bundles and represents the default language supported by Spring Security messages. This default file is in English.

要自定义 messages.properties 文件或支持其他语言,您应复制该文件、对其进行相应的重命名,然后将其注册到前面这个 bean 中。此文件中没有很多消息键,因此本地化不应被视为一项主要工作。如果您确实对该文件进行了本地化,请考虑通过记录 JIRA 任务并附上适当命名的 messages.properties 本地化版本与社区分享您的工作成果。

To customize the messages.properties file or support other languages, you should copy the file, rename it accordingly, and register it inside the preceding bean definition. There are not a large number of message keys inside this file, so localization should not be considered a major initiative. If you do perform localization of this file, consider sharing your work with the community by logging a JIRA task and attaching your appropriately-named localized version of messages.properties.

Spring Security 依靠 Spring 的本地化支持来实际查找适当的消息。为此,您必须确保传入请求中的区域设置存储在 Spring 的 org.springframework.context.i18n.LocaleContextHolder 中。Spring MVC 的 DispatcherServlet 会自动为您的应用程序执行此操作。但是,由于 Spring Security 的 filter 在此之前被调用,因此需要在调用 filter 之前设置 LocaleContextHolder 以包含正确的 Locale。您可以在 filter 中自行执行此操作(该 filter 必须放在 web.xml 中的 Spring Security filter 之前),或者可以使用 Spring 的 RequestContextFilter。请参阅 Spring Framework 文档以进一步了解在 Spring 中使用本地化。

Spring Security relies on Spring’s localization support in order to actually look up the appropriate message. For this to work, you have to make sure that the locale from the incoming request is stored in Spring’s org.springframework.context.i18n.LocaleContextHolder. Spring MVC’s DispatcherServlet does this for your application automatically. However, since Spring Security’s filters are invoked before this, the LocaleContextHolder needs to be set up to contain the correct Locale before the filters are called. You can either do this in a filter yourself (which must come before the Spring Security filters in web.xml) or you can use Spring’s RequestContextFilter. See the Spring Framework documentation for further details on using localization with Spring.

contacts 示例应用程序已设置为使用本地化消息。

The contacts sample application is set up to use localized messages.