Localization

如果您需要支持其他语言环境,本节包含了您需要知道的所有内容。

If you need to support other locales, everything you need to know is contained in this section.

所有异常消息都可以本地化,包括与身份验证失败和拒绝访问(授权失败)相关的消息。针对开发人员或系统部署人员的异常和日志消息(包括不正确的属性、接口契约违规、使用不正确的构造函数、启动时验证、调试级日志记录)不会被本地化,而是以英语硬编码到 Spring Security 的代码中。

All exception messages can be localized, including messages related to authentication failures and access being denied (authorization failures). Exceptions and logging messages that are focused on developers or system deplopers (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,以引用消息即可。示例如下所示:

Shipping in the spring-security-core-xx.jar you will find an org.springframework.security package that in turn contains a messages.properties file, as well as localized versions for some common languages. This should be referred to by your ApplicationContext, 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. An example is shown below:

<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 正确名称的本地化版本,与社区分享您的成果。

If you wish to customize the messages.properties file, or support other languages, you should copy the file, rename it accordingly, and register it inside the above 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, please 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 的过滤器在执行此操作之前调用,因此 LocaleContextHolder 必须在调用过滤器之前设置为包含正确的 Locale。您可以在 web.xml 中自己执行此操作(过滤器必须在前 Spring Security 过滤器之前),或者您可以使用 Spring 的 RequestContextFilter。请参阅 Spring Framework 文档,以了解有关与 Spring 一起使用本地化的更多详细信息。

Spring Security relies on Spring’s localization support in order to actually lookup the appropriate message. In order 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, but 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. Please refer to the Spring Framework documentation for further details on using localization with Spring.

“联系人”示例应用程序已设置为使用本地化消息。

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