springboot + freemarker 實現國際化


前言  

寫過一篇springboot+freemarker國際化過程中的細節問題,但沒有寫過具體的國際化實現過程。正好有人在問,我就把我實現的過程貼出來,即使自己知識的一個備份,如果對別人有點用,那是再好不過了。

 

1.springboot項目創建

springboot步入2.0后,已經逐步成為了java web快速開發的首選框架,項目的創建過程也是非常簡單,教程非常多,我就不贅述了。下圖時我創建的一個demo項目。

 

pom文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <groupId>com.zhangs</groupId>
 6     <artifactId>i18n-demo</artifactId>
 7     <version>0.0.1-SNAPSHOT</version>
 8     <packaging>jar</packaging>
 9 
10     <name>i18n-demo</name>
11     <description>Demo project for Spring Boot</description>
12 
13     <parent>
14         <groupId>org.springframework.boot</groupId>
15         <artifactId>spring-boot-starter-parent</artifactId>
16         <version>2.0.6.RELEASE</version>
17         <relativePath/> <!-- lookup parent from repository -->
18     </parent>
19 
20     <properties>
21         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
23         <java.version>1.8</java.version>
24     </properties>
25 
26     <dependencies>
27         <dependency>
28             <groupId>org.springframework.boot</groupId>
29             <artifactId>spring-boot-starter-freemarker</artifactId>
30         </dependency>
31         <dependency>
32             <groupId>org.springframework.boot</groupId>
33             <artifactId>spring-boot-starter-web</artifactId>
34         </dependency>
35 
36         <dependency>
37             <groupId>org.springframework.boot</groupId>
38             <artifactId>spring-boot-starter-test</artifactId>
39             <scope>test</scope>
40         </dependency>
41     </dependencies>
42 
43     <build>
44         <plugins>
45             <plugin>
46                 <groupId>org.springframework.boot</groupId>
47                 <artifactId>spring-boot-maven-plugin</artifactId>
48             </plugin>
49         </plugins>
50     </build>
51 </project>
View Code

 

2. 各種配置

2.1 首先是application.properties的配置

  spring.messages.basename  指定了國際化文件的路徑+文件前綴(不同語言,后綴是不一樣的),如果不指定,默認是classpath下的messages_*.properties

 

2.2 增加一個全局配置類MainConfig,繼承WebMvcConfigurationSupport

addInterceptors方法中增加了一個國際化攔截器,會攔截前端_lang參數,因為localeResolver方法中實例化了CookieLocaleResolver對象,所以_lang參數會存在cookie中,所有的頁面都可以從cookie中取到_lang參數。
你也可以使用SessionLocaleResolver將參數存到session。
 1 @Configuration
 2 @ComponentScan
 3 @EnableAutoConfiguration
 4 public class MainConfig extends WebMvcConfigurationSupport {
 5     @Bean
 6     public LocaleResolver localeResolver() {
 7         CookieLocaleResolver slr = new CookieLocaleResolver();
 8         // 默認語言
 9         slr.setDefaultLocale(Locale.US);
10         return slr;
11     }
12 
13     @Bean
14     public LocaleChangeInterceptor localeChangeInterceptor() {
15         LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
16         // 參數名
17         lci.setParamName("_lang");
18         return lci;
19     }
20 
21     @Override
22     public void addInterceptors(InterceptorRegistry registry) {
23         registry.addInterceptor(localeChangeInterceptor());
24     }
25 
26     @Override
27     public void addResourceHandlers(ResourceHandlerRegistry registry) {
28         registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
29     }
30 }

 

3. 創建國際化資源文件

資源文件/i18n/message_*.properties。*號的實際值(zh_CN,en_US等)就是上一步中_lang參數的值,決定了使用哪個資源文件

英文資源message_en_US.properties

 

中文資源message_zh_CN.properties

 

4. 測試ftl頁面index.ftl

 

index.ftl

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <#import "spring.ftl" as spring>
 5     <#assign arg = ["AAA","張三"]>
 6     <title><@spring.messageArgs "title" ,arg /></title>
 7     <meta charset="UTF-8">
 8 </head>
 9 <body>
10 <br/>
11 <a class="changeLang" href="javascript:void(0);">english</a><br/>
12 <a class="changeLang" href="javascript:void(0);">中文</a><br/>
13 <@spring.message code="user.loginname"/><br/>
14 </body>
15 <script src="${request.contextPath}/static/jquery.js"></script>
16 <script>
17     $(".changeLang").on("click", function () {
18         switch ($(this).text()) {
19             case "中文": {
20                 window.location.href = "index?_lang=zh_CN";
21                 break;
22             }
23             case "english": {
24                 window.location.href = "index?_lang=en_US";
25                 break;
26             }
27         }
28     })
29 </script>
30 </html>
View Code

 

5. 測試結果

ps:因為"AAA,張三"這幾個字符是在ftl中寫死了,所以不會變

英文:

 

中文:

 

項目地址: https://github.com/czs208112/i18n-demo

 


免責聲明!

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



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