當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
Spring Boot通过url设置国际化
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot通过url设置国际化
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄
?
?
理論
源碼
?
理論
通過(guò)獲取瀏覽器中g(shù)et方法的參數(shù),構(gòu)造LocaleResolver,從LocaleResolver中調(diào)用對(duì)應(yīng)的properties文件。
在MvcConfig中創(chuàng)建這個(gè)自己定義的LocaleResolver類(lèi)!
程序運(yùn)行截圖如下:
?
源碼
程序結(jié)構(gòu)如下:
MyLocaleResolver.java
package internationdemotest.demo.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 {@Overridepublic Locale resolveLocale(HttpServletRequest httpServletRequest) {String l = httpServletRequest.getParameter("l");Locale locale = Locale.getDefault();if(!StringUtils.isEmpty(l)){String[] split = l.split("_");locale = new Locale(split[0], split[1]);}return locale;}@Overridepublic void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {} }MyMvcConfig.java
package internationdemotest.demo.config;import internationdemotest.demo.component.MyLocaleResolver; 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.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configuration public class MyMvcConfig extends WebMvcConfigurerAdapter{@Beanpublic WebMvcConfigurerAdapter webMvcConfigurerAdapter(){WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("index");registry.addViewController("index.html").setViewName("index");}};return adapter;}@Beanpublic LocaleResolver localeResolver(){return new MyLocaleResolver();} }index.properties
index.hehe=呵呵#index_en_US.properties
index.hehe=hehehehehehheheheheheindex_zh_CN.properties
index.hehe=呵呵index.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1 th:text="#{index.hehe}">呵呵</h1><a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a> <a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a></body> </html>application.properties
spring.messages.basename=i18n.index?
總結(jié)
以上是生活随笔為你收集整理的Spring Boot通过url设置国际化的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Web笔记-使用jsonp解决跨域请求(
- 下一篇: Spring Boot中实现简单表单提交