SpringBoot整合國際化功能


(1)、編寫國際化配置文件

  在resources下新建i18n文件夾,並新建以下文件

  ①index.properties

   1 username=username

  ②index_en_US.properties

   1 username=username 

  ③index_zh_CN.properties

   1 username=用戶名 

(2)、使用ResourceBundleMessageSource管理國際化資源文件

*SpringBoot已經自動配置了管理國際化資源文件的組件

(3)在配置文件中指定國際化資源文件的文件夾及基礎文件

 1 #指定國際化資源文件的文件夾及基礎文件 2 spring.messages.basename=i18n/index 

(4)* 編寫自定義的Locale區域解析器

 1 package cn.coreqi.config;
 2 
 3 import org.springframework.util.StringUtils;
 4 import org.springframework.web.servlet.LocaleResolver;
 5 
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 import java.util.Locale;
 9 
10 /**
11  * SpringBoot默認的Locale解析器是根據請求頭的區域信息進行解析的(瀏覽器語言)
12  * 使用自定義的Locale解析器對url的區域信息進行解析達到點擊切換區域效果
13  * 一旦我們自定義的區域解析器注冊到Spring容器中,則SpringBoot提供的將不自動注冊
14  */
15 public class MyLocaleResolver implements LocaleResolver {
16     @Override
17     public Locale resolveLocale(HttpServletRequest httpServletRequest) {
18         String l = httpServletRequest.getParameter("l");
19         if(!StringUtils.isEmpty((l))){
20             String [] s = l.split("_");
21             return new Locale(s[0],s[1]);
22         }
23         return Locale.getDefault();
24     }
25 
26     @Override
27     public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
28 
29     }
30 }

(5)注冊我們自定義的區域解析器

 1 package cn.coreqi.config;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.web.servlet.LocaleResolver;
 6 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 7 import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
 8 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 9 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
10 
11 /**
12  * 擴展SpringMVC
13  * SpringBoot2使用的Spring5,因此將WebMvcConfigurerAdapter改為WebMvcConfigurer
14  * 使用WebMvcConfigurer擴展SpringMVC好處既保留了SpringBoot的自動配置,又能用到我們自己的配置
15  */
16 //@EnableWebMvc //如果我們需要全面接管SpringBoot中的SpringMVC配置則開啟此注解,
17                 //開啟后,SpringMVC的自動配置將會失效。
18 @Configuration
19 public class WebConfig implements WebMvcConfigurer {
20     @Override
21     public void addViewControllers(ViewControllerRegistry registry) {
22         //設置對“/”的請求映射到index
23         //如果沒有數據返回到頁面,沒有必要用控制器方法對請求進行映射
24         registry.addViewController("/").setViewName("index");
25     }
26 
27     //注冊我們自定義的區域解析器,一旦將我們的區域解析器注冊到Spring容器中則SpingBoot
28     //默認提供的區域解析器將不會自動注冊
29     @Bean
30     public LocaleResolver localeResolver(){
31         return new MyLocaleResolver();
32     }
33 }

(6)視圖中引用國際化內容

 1 <!DOCTYPE html>
 2 <html lang="en" xmlns:th="http://www.thymeleaf.org">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Index首頁</title>
 6 </head>
 7 <body>
 8 <h1 th:text="#{username}"></h1>
 9 </body>
10 </html>

(7)測試


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM