SpringBoot實現國際化


實現方法:thymeleaf模板引擎加上BootStrap

准備工作:

1.將准備好的Bootstrap模板放在templates下讓SpringBoot進行自動配置

SpringBoot自動配置會自動到(idea的shif鍵連按兩下進入全局搜索)

 

 

 2.Bootstrp的引入(這里是maven以depency的方式引入)

<!--引入bootstrap-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.0.0</version>
        </dependency>

3.thymeleaf的引入

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

4.編寫國際化配置文件

使用ResourceBundleMessageSource管理國際化資源文件

 

 

 

 springBoot默認配置

 相關配置的application.properties:

 

 

 自己配置的國際化的代碼:

package com.zyb.webdemo.component;

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

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

/**
 * 在鏈接上攜帶區域信息
 */

public class MyLocaleResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String l = request.getParameter("l");
        Locale locale = Locale.getDefault();
        if(!StringUtils.isEmpty(l)){
            String[] split = l.split("_");
            locale = new Locale(split[0],split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}

上面附帶鏈接

 

 

 結果亂碼的解決方案;

 

 

 效果圖:

 

 

 


免責聲明!

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



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