SpringBoot 國際化配置,SpringBoot Locale 國際化

SpringBoot 國際化配置,SpringBoot Locale 國際化

 

================================

©Copyright 蕃薯耀 2018年3月27日

http://www.cnblogs.com/fanshuyao/

 

附件下載(源碼下載)見:http://fanshuyao.iteye.com/blog/2414640

 

一、效果所下:



 



 



 

二、SpringBoot 國際化配置

1、創建國際化配置文件(3個):

mess.properties

Java代碼  收藏代碼
  1. mess.user.name=用戶名  
  2. mess.user.password=密碼  
  3. mess.user.btn=登錄  

 

mess_zh_CN.properties

Java代碼  收藏代碼
  1. mess.user.name=用戶名  
  2. mess.user.password=密碼  
  3. mess.user.btn=登錄  

 

mess_en_US.properties

Java代碼  收藏代碼
  1. mess.user.name=UserName  
  2. mess.user.password=Password  
  3. mess.user.btn=Sign In  

 

SpringBoot默認國際化文件爲:classpath:message.properties,如果放在其它文件夾中,則需要在application.properties配置屬性spring.messages.basename:

Java代碼  收藏代碼
  1. #表示放在classpath的i18n文件夾,文件前綴爲mess  
  2. spring.messages.basename=i18n.mess  

 

2、自定義國際化語言解析器

Java代碼  收藏代碼
  1. import java.util.Locale;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5. import javax.servlet.http.HttpSession;  
  6.   
  7. import org.springframework.web.servlet.LocaleResolver;  
  8. import org.thymeleaf.util.StringUtils;  
  9.   
  10. /** 
  11.  * 自定義國際化語言解析器 
  12.  * 
  13.  */  
  14. public class MyLocaleResolver implements LocaleResolver{  
  15.       
  16.     private static final String I18N_LANGUAGE = "i18n_language";  
  17.     private static final String I18N_LANGUAGE_SESSION = "i18n_language_session";  
  18.   
  19.     @Override  
  20.     public Locale resolveLocale(HttpServletRequest req) {  
  21.         String i18n_language = req.getParameter(I18N_LANGUAGE);  
  22.         Locale locale = Locale.getDefault();  
  23.         if(!StringUtils.isEmpty(i18n_language)) {  
  24.             String[] language = i18n_language.split("_");  
  25.             locale = new Locale(language[0], language[1]);  
  26.               
  27.             //將國際化語言保存到session  
  28.             HttpSession session = req.getSession();  
  29.             session.setAttribute(I18N_LANGUAGE_SESSION, locale);  
  30.         }else {  
  31.             //如果沒有帶國際化參數,則判斷session有沒有保存,有保存,則使用保存的,也就是之前設置的,避免之後的請求不帶國際化參數造成語言顯示不對  
  32.             HttpSession session = req.getSession();  
  33.             Locale localeInSession = (Locale) session.getAttribute(I18N_LANGUAGE_SESSION);  
  34.             if(localeInSession != null) {  
  35.                 locale = localeInSession;  
  36.             }  
  37.         }  
  38.         return locale;  
  39.     }  
  40.   
  41.     @Override  
  42.     public void setLocale(HttpServletRequest req, HttpServletResponse res, Locale locale) {  
  43.           
  44.     }  
  45.   
  46. }  

 

3、把國際化語言解析器放到Spring容器中:

這裏創建了一個自定義的配置類:CustomMvcConfig ,繼承WebMvcConfigurerAdapter,可以擴展SpringMvc的功能,包括攔截器,轉換器等

Java代碼  收藏代碼
  1. import org.springframework.context.annotation.Bean;  
  2. import org.springframework.context.annotation.Configuration;  
  3. import org.springframework.web.servlet.LocaleResolver;  
  4. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;  
  5. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;  
  6.   
  7. import com.lqy.springboot.message.locale.MyLocaleResolver;  
  8.   
  9. //使用WebMvcConfigurerAdapter可以擴展SpringMvc的功能,包括攔截器,轉換器等  
  10. //@EnableWebMvc //設置@EnableWebMvc爲完全接管SpringMvc,但一般不要設置完全接管SpringMvc  
  11. @Configuration  
  12. public class CustomMvcConfig extends WebMvcConfigurerAdapter {  
  13.   
  14.     /** 
  15.      * 配置自己的國際化語言解析器 
  16.      * @return 
  17.      */  
  18.     @Bean  
  19.     public LocaleResolver localeResolver() {  
  20.         return new MyLocaleResolver();  
  21.     }  
  22.   
  23.     /** 
  24.      * 配置自己的攔截器 
  25.      */  
  26.     @Override  
  27.     public void addInterceptors(InterceptorRegistry registry) {  
  28.         //super.addInterceptors(registry);  
  29.     }  
  30.       
  31.       
  32. }  

 

4、頁面顯示及切換國際化操作:

Html代碼  收藏代碼
  1. <!DOCTYPE html>  
  2. <html xmlns:th="http://www.thymeleaf.org">  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <title>Insert title here</title>  
  6. <style type="text/css">  
  7. .ib{  
  8.     display: inline-block;  
  9. }  
  10. .ml20{  
  11.     margin-left: 20px;  
  12. }  
  13. .mt20{  
  14.     margin-top: 20px;  
  15. }  
  16. </style>  
  17. </head>  
  18. <body>  
  19.     <div>  
  20.         <div>[[#{mess.user.name}]]:<input th:placeholder="#{mess.user.name}"/></div>  
  21.     </div>  
  22.     <div>  
  23.         <div>[[#{mess.user.password}]]:<input th:placeholder="#{mess.user.password}"/></div>  
  24.     </div>  
  25.     <div>  
  26.         <div><button>[[#{mess.user.btn}]]</button></div>  
  27.     </div>  
  28.       
  29.     <div class="mt20">  
  30.         <span class="ib"><th:href="@{/mess(i18n_language=zh_CN)}">中文</a></span>  
  31.         <span class="ib ml20"><th:href="@{/mess(i18n_language=en_US)}">英文</a></span>  
  32.     </div>  
  33.       
  34. </body>  
  35. </html>  

 

 

 

================================

©Copyright 蕃薯耀 2018年3月27日

http://www.cnblogs.com/fanshuyao/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章