springBoot 實現中文國際化


一:實現效果如下:

二 SpringBoot 國際化配置

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

messages.properties

messages.user.name=用戶名  
messages.user.password=密碼  
messages.user.btn=登錄 

messages_en_US.properties

messages.user.name=UserName  
messages.user.password=Password  
messages.user.btn=Sign In  
messages.keyword=keyword

messages_zh_CN.properties 

messages.user.name=\u7528\u6237\u540d
messages.user.password=\u5bc6\u7801
messages.user.btn=\u767b\u5f55
messages.keyword=\u5173\u952e\u8bcd

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

#表示放在classpath的i18n文件夾,文件前綴為mess
spring.messages.basename=i18n/messages

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

package com.joyny.ecas.config;

import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Locale;

/**
 * 自定義國際化語言解析器 
 * 
 */  
public class MyLocaleResolver implements LocaleResolver {
      
    private static final String I18N_LANGUAGE = "i18n_language";  
    private static final String I18N_LANGUAGE_SESSION = "i18n_language_session";  
  
    @Override  
    public Locale resolveLocale(HttpServletRequest req) {
        String i18n_language = req.getParameter(I18N_LANGUAGE);  
        Locale locale = Locale.getDefault();  
        if(!StringUtils.isEmpty(i18n_language)) {
            String[] language = i18n_language.split("_");  
            locale = new Locale(language[0], language[1]);  
              
            //將國際化語言保存到session  
            HttpSession session = req.getSession();  
            session.setAttribute(I18N_LANGUAGE_SESSION, locale);  
        }else {  
            //如果沒有帶國際化參數,則判斷session有沒有保存,有保存,則使用保存的,也就是之前設置的,避免之后的請求不帶國際化參數造成語言顯示不對  
            HttpSession session = req.getSession();
            Locale localeInSession = (Locale) session.getAttribute(I18N_LANGUAGE_SESSION);  
            if(localeInSession != null) {  
                locale = localeInSession;  
            }  
        }  
        return locale;  
    }  
  
    @Override  
    public void setLocale(HttpServletRequest req, HttpServletResponse res, Locale locale) {
          
    }  
  
} 

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

package com.joyny.ecas.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//WebMvcConfigurerAdapter
//使用WebMvcConfigurerAdapter可以擴展SpringMvc的功能,包括攔截器,轉換器等
//@EnableWebMvc //設置@EnableWebMvc為完全接管SpringMvc,但一般不要設置完全接管SpringMvc  
@Configuration
public class CustomMvcConfig implements WebMvcConfigurer {
  
    /** 
     * 配置自己的國際化語言解析器 
     * @return 
     */  
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }

    /** 
     * 配置自己的攔截器 
     */  
    @Override  
    public void addInterceptors(InterceptorRegistry registry) {
        //super.addInterceptors(registry);  
    }  
      
      
}  

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

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <style type="text/css">
        .ib{
            display: inline-block;
        }
        .ml20{
            margin-left: 20px;
        }
        .mt20{
            margin-top: 20px;
        }
    </style>
</head>
<body>
<div>
    <div>[[#{messages.user.name}]]:<input th:placeholder="#{messages.user.name}"/></div>
</div>
<div>
    <div>[[#{messages.user.password}]]:<input th:placeholder="#{messages.user.password}"/></div>
</div>
<div>
    <div><button>[[#{messages.user.btn}]]</button></div>
</div>

<div class="mt20">
    <span class="ib"><a th:href="@{/mapdemo/hello(i18n_language=zh_CN)}">中文</a></span>
    <span class="ib ml20"><a th:href="@{/mapdemo/hello(i18n_language=en_US)}">英文</a></span>
</div>

</body>
</html>

  

  


免責聲明!

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



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