SpringBoot页面国际化(语言切换)

it2025-10-31  1

 最终效果

一、保证编码格式都是UTF-8

二、resource目录下新建一文件夹 (方便编写国际化文件)

 

#login_zh_CN login.btn=Sign in login.password=Password login.remember=Remember me login.tip=Please sign in login.username=Username login.forgetPassword=Forget Password login.registeruser=register a new user!

 

#login_en_US login.btn=Sign in login.password=Password login.remember=Remember me login.tip=Please sign in login.username=Username login.forgetPassword=Forget Password login.registeruser=register a new user!

 

#login_ja_JP login.btn=とうろく login.password=パスワード login.remember=私を覚えて login.tip=請うとうろく login.username=ゆーざめい login.forgetPassword=パスワードを忘れる? login.registeruser=新しいアカウントを登録する!

三、我集成的Thymeleaf魔板。

<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> - 登录</title> <meta name="keywords" content=""> <meta name="description" content=""> <link rel="shortcut icon" th:href="@{img/favicon.ico}"> <link th:href="@{css/bootstrap.min.css}" rel="stylesheet"> <link th:href="@{css/font-awesome.css}" rel="stylesheet"> <link th:href="@{css/animate.css}" rel="stylesheet"> <link th:href="@{css/style.css}" rel="stylesheet"> <script th:href="@{js/jquery.min.js}"></script> <script th:href="@{js/bootstrap.min.js}"></script> </head> <body class="gray-bg"> <div class="middle-box text-center loginscreen animated fadeInDown"> <div> <div> <!-- <h1 class="logo-name">h</h1>--> </div> <h3 th:text="#{login.tip}"></h3> <form class="m-t" role="form" th:action="@{/index}"> <div class="form-group"> <input type="email" class="form-control" th:placeholder="#{login.username}" required=""> </div> <div class="form-group"> <input type="password" class="form-control" th:placeholder="#{login.password}" required=""> </div> <button type="submit" class="btn btn-primary block full-width m-b">[[#{login.btn}]]</button> <p class="text-muted text-center"> <a href="login.html#" th:text="#{login.forgetPassword}"> </a> | <a th:href="@{register}" th:text="#{login.registeruser}"></a> </p> </form> <!-- 这里传入参数不需要使用 ?使用 (key=value)--> <a class="btn btn-sm" th:href="@{/login.html(l='zh_CN')}">中文</a> <a class="btn btn-sm" th:href="@{/login.html(l='en_US')}">English</a> <a class="btn btn-sm" th:href="@{/login.html(l='ja_JP')}">セーター</a> </div> </div> </body> </html>

四、重写springmvc

package com.zxl.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.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; //应为类型要求为WebMvcConfigurer,所以我们实现其接口 //可以使用自定义类扩展MVC的功能 @Configuration public class MyMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { // 浏览器发送/test , 就会跳转到test页面; registry.addViewController("/login.html").setViewName("login"); } @Bean public LocaleResolver localeResolver(){ return new MyLocaleResolver(); } }

五、编写请求解析器

package com.zxl.config; import org.apache.commons.lang3.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 request) { String language = request.getParameter("l"); System.out.println(language); Locale locale = Locale.getDefault(); // 如果没有获取到就使用系统默认的 //如果请求链接不为空 if (!StringUtils.isEmpty(language)){ //分割请求参数 String[] split = language.split("_"); //国家,地区 locale = new Locale(split[0],split[1]); } return locale; } @Override public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { } }

 六、编写controller

package com.zxl.controller; import com.zxl.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * <p> * 前端控制器 * </p> * * @author 郑晓龙 * @since 2020-10-21 */ @Controller public class UserController { @Autowired private UserService userService; @RequestMapping({"/","login"}) public String login(){ return "login"; } }

 

最新回复(0)