Spring Security(三十一):9.6 Localization(本地化)

Spring Security supports localization of exception messages that end users are likely to see. If your application is designed for English-speaking users, you don’t need to do anything as by default all Security messages are in English. If you need to support other locales, everything you need to know is contained in this section.java

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 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的代碼中用英語進行硬編碼。
 
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:
在spring-security-core-xx.jar中發送,你會發現一個org.springframework.security包,它又包含一個messages.properties文件,以及一些經常使用語言的本地化版本。這應該由您的ApplicationContext引用,由於Spring Security類實現了Spring的MessageSourceAware接口,並指望消息解析器在應用程序上下文啓動時被依賴注入。一般,您須要作的就是在應用程序上下文中註冊bean以引用消息。一個例子以下所示:
 
<bean id="messageSource"
	class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:org/springframework/security/messages"/>
</bean>

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.web

messages.properties根據標準資源包命名,表示Spring Security消息支持的默認語言。此默認文件爲英文。
 
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.
若是您但願自定義messages.properties文件或支持其餘語言,您應該複製該文件,相應地重命名,並在上面的bean定義中註冊它。此文件中沒有大量的消息密鑰,所以本地化不該被視爲主要的主動。若是您確實執行了此文件的本地化,請考慮經過記錄JIRA任務並附加適當命名的本地化版本的messages.properties來與社區共享您的工做。
 
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.
Spring Security依賴於Spring的本地化支持,以便實際查找相應的消息。爲了使其工做,您必須確保傳入請求中的區域設置存儲在Spring的org.springframework.context.i18n.LocaleContextHolder中。 Spring MVC的DispatcherServlet會自動爲您的應用程序執行此操做,但因爲在此以前調用了Spring Security的過濾器,所以須要將LocaleContextHolder設置爲在調用過濾器以前包含正確的Locale。您能夠本身在過濾器中執行此操做(必須在web.xml中的Spring Security過濾器以前),或者您可使用Spring的RequestContextFilter。有關在Spring中使用本地化的更多詳細信息,請參閱Spring Framework文檔。
 
The "contacts" sample application is set up to use localized messages.
「contacts」示例應用程序設置爲使用本地化消息。
相關文章
相關標籤/搜索