-
HTML
<form class="form-signin" action="dashboard.html"> <img class="mb-4" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72"> <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1> <label class="sr-only" th:text="#{login.username}">Username</label> <input type="text" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus=""> <label class="sr-only" th:text="#{login.password}">Password</label> <input type="password" class="form-control" placeholder="Password" th:placeholder="#{login.password}" required=""> <div class="checkbox mb-3"> <label> <!--input是自闭标签所以使用 thymeleaf的行内写法--> <input type="checkbox" value="remember-me"> [[#{login.remember}]] </label> </div> <button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button> <p class="mt-5 mb-3 text-muted">© 2017-2018</p> <a class="btn btn-sm" th:href="@{index(locale='zh_CN')}">中文</a> <a class="btn btn-sm" th:href="@{index(locale='en_US')}">English</a> </form>
-
编写国际化资源文件
-
application.yml指定国际化资源基础名
spring: messages: basename: i18n.login
-
自定义LocaleResolver
package com.live.component; import org.springframework.util.StringUtils; import org.springframework.web.servlet.LocaleResolver; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Locale; public class MyLocaleResolver implements LocaleResolver { @Override public Locale resolveLocale(HttpServletRequest httpServletRequest) { String temp = httpServletRequest.getParameter("locale"); Locale locale = Locale.getDefault(); if (!StringUtils.isEmpty(temp)) { String[] s = temp.split("_"); locale = new Locale(s[0],s[1]); } return locale; } @Override public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { } }
-
将自定义的LocaleResolver注册到容器中
package com.live.config; import com.live.component.MyLocaleResolver; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.LocaleResolver; @Configuration public class MyLocaleResolverConfig { @Bean public LocaleResolver localeResolver() { return new MyLocaleResolver(); } }
-
配置视图映射
- 注意:这里也是springboot2.x以后有所区别的地方
- 在Spring5以后废弃了WebMvcConfigurerAdapter
- 新的实现是WebMvcConfigurer
package com.live.component; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class MyViewConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); registry.addViewController("/index").setViewName("index"); registry.addViewController("/index.*").setViewName("index"); } }
-
结构
【配置】:SpringBoot2.3.0配置国际化带点击切换
最新推荐文章于 2024-03-28 15:03:59 发布
本文介绍了如何在SpringBoot2.3.0中配置国际化,包括编写资源文件、设置基础名、自定义LocaleResolver并注册到容器,以及Spring5后配置视图映射的变化,强调了WebMvcConfigurer接口的使用。
摘要由CSDN通过智能技术生成