导读


由于项目需要,需要搞下页面国际化语言处理(中英文切换),这里写了个demo,来完成中英文切换,这里只是一个登录页面,如果做到全网站,类同。

使用


1、新建一个SpringBoot项目

image.png

InterPageController跳转页面:

  1. import org.springframework.stereotype.Controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import java.util.Map;
  4. @Controller
  5. @RequestMapping("/inter")
  6. public class InterPageController {
  7. @RequestMapping("/login")
  8. public String interPage(Map<String, Object> map) {
  9. return "interPage";
  10. }
  11. }

2、新建一个登录页面

我这里登录页面起名叫interPage.html。页面样式如下:
image.png

3、修改application.properties

SpringBoot默认国际化文件为:classpath:message.properties。 如果放在其它文件夹中,则需要在application.properties配置属性spring.messages.basename属性。我这里是新建了i18n文件夹来存放。

  1. #表示放在classpath的i18n文件夹,文件前缀为laguage
  2. spring.messages.basename=i18n.laguage

对应的yml配置:

  1. spring:
  2. application:
  3. name: inter
  4. messages:
  5. basename: i18n.laguage

4、添加国际化语言文件

laguage.properties:默认(首次进入)

  1. laguage.user.name=邮箱
  2. laguage.user.password=密码
  3. laguage.user.remember=记住我
  4. laguage.user.btn=登录

laguage_zh_CN.properties:

  1. laguage.user.name=邮箱
  2. laguage.user.password=密码
  3. laguage.user.remember=记住我
  4. laguage.user.btn=登录

laguage_en_US.properties:

  1. laguage.user.name=Email
  2. laguage.user.password=Password
  3. laguage.user.remember=Remember me
  4. laguage.user.btn=Sign In

5、自定义国际化语言解析器

  1. import org.apache.commons.lang3.StringUtils;
  2. import org.springframework.web.servlet.LocaleResolver;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import javax.servlet.http.HttpSession;
  6. import java.util.Locale;
  7. /**
  8. * 国际化语言解析器(自定义)
  9. */
  10. public class MyLocaleResolver implements LocaleResolver {
  11. private static final String I18N_LANGUAGE = "i18n_language";
  12. private static final String I18N_LANGUAGE_SESSION = "i18n_language_session";
  13. @Override
  14. public Locale resolveLocale(HttpServletRequest req) {
  15. String i18n_language = req.getParameter(I18N_LANGUAGE);
  16. Locale locale = Locale.getDefault();
  17. if (!StringUtils.isEmpty(i18n_language)) {
  18. String[] language = i18n_language.split("_");
  19. locale = new Locale(language[0], language[1]);
  20. //将国际化语言保存到session
  21. HttpSession session = req.getSession();
  22. session.setAttribute(I18N_LANGUAGE_SESSION, locale);
  23. } else {
  24. //如果没有带国际化参数,则判断session有没有保存,有保存,则使用保存的,也就是之前设置的,避免之后的请求不带国际化参数造成语言显示不对
  25. HttpSession session = req.getSession();
  26. Locale localeInSession = (Locale) session.getAttribute(I18N_LANGUAGE_SESSION);
  27. if (localeInSession != null) {
  28. locale = localeInSession;
  29. }
  30. }
  31. return locale;
  32. }
  33. @Override
  34. public void setLocale(HttpServletRequest req, HttpServletResponse res, Locale locale) {
  35. }
  36. }

6、将配置放到容器中

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.web.servlet.LocaleResolver;
  4. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  5. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  6. @Configuration
  7. public class MyMvcConfig implements WebMvcConfigurer {
  8. @Override
  9. public void addViewControllers(ViewControllerRegistry registry) {
  10. registry.addViewController("/inter/login").setViewName("interPage.html");
  11. }
  12. /**
  13. * 添加区域信息解析器
  14. * @return
  15. */
  16. @Bean
  17. public LocaleResolver localeResolver(){
  18. return new MyLocaleResolver();
  19. }
  20. }

7、页面样式修改

interPage.html:

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>国际化页面</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
  8. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
  9. integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  10. <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
  11. <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
  12. integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
  13. crossorigin="anonymous"></script>
  14. </head>
  15. <body>
  16. <div class="container">
  17. <!--导航-->
  18. <nav class="navbar navbar-inverse">
  19. <div class="container-fluid">
  20. <!-- Brand and toggle get grouped for better mobile display -->
  21. <div class="navbar-header">
  22. <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
  23. data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
  24. <span class="sr-only">Toggle navigation</span>
  25. <span class="icon-bar"></span>
  26. <span class="icon-bar"></span>
  27. <span class="icon-bar"></span>
  28. </button>
  29. <a class="navbar-brand" href="#">Brand</a>
  30. </div>
  31. </div><!-- /.container-fluid -->
  32. </nav>
  33. <!--登录-->
  34. <form class="form-horizontal">
  35. <div class="form-group">
  36. <span for="inputEmail3" class="col-sm-2 control-label" th:text="#{laguage.user.name}"></span>
  37. <div class="col-sm-10">
  38. <input type="email" class="form-control" id="inputEmail3" th:placeholder="'请输入'+ #{laguage.user.name}">
  39. </div>
  40. </div>
  41. <div class="form-group">
  42. <span for="inputPassword3" class="col-sm-2 control-label" th:text="#{laguage.user.password}"></span>
  43. <div class="col-sm-10">
  44. <input type="password" class="form-control" id="inputPassword3"
  45. th:placeholder="#{laguage.user.password}">
  46. </div>
  47. </div>
  48. <div class="form-group">
  49. <div class="col-sm-offset-2 col-sm-10">
  50. <div class="checkbox">
  51. <label>
  52. <input type="checkbox"> <span th:text="#{laguage.user.remember}"></span>
  53. </label>
  54. </div>
  55. </div>
  56. </div>
  57. <div class="form-group">
  58. <div class="col-sm-offset-2 col-sm-10">
  59. <button type="submit" class="btn btn-primary" th:text="#{laguage.user.btn}">Sign in</button>
  60. </div>
  61. </div>
  62. <a type="submit" class="btn btn-success" th:href="@{/inter/login(i18n_language=zh_CN)}">中文</a>
  63. <a type="submit" class="btn btn-info" th:href="@{/inter/login(i18n_language=en_US)}">English</a>
  64. </form>
  65. </div>
  66. </body>
  67. </html>

8、样例展示

  • 首页:由于带缓存,所以有可能是英文也有可能是中文,第一次默认进入的是中文界面。

image.png
image.png

  • 英文页面:

image.png

  • 中文页面:

image.png

9、项目目录

image.png

注意


1、页面乱码

image.png
解决办法:
修改Application.properties文件,添加spring.messages.encoding=gbk

  1. spring:
  2. application:
  3. name: inter
  4. messages:
  5. basename: i18n.laguage
  6. encoding: gbk #选择合适的编码格式

最终成果:

image.png

2、拦截器配置

拦截器配置一定要与前台页面相对应,不然容易出现错误。

END


搞定~