Spring Boot项目实践-员工管理系统(三)·国际化

本次实战项目主要是借鉴b站上的视频资源【狂神说Java】SpringBoot最新教程IDEA版通俗易懂完成的,有需求的话,可以直接去b站观看完整的视频教程,本文若有不对之处,望不吝赐教,谢谢~
博文前提:

通俗地讲,国际化就是实现网页语言的切换

一、配置首页的国际化

(1)国际化涉及语言,就得要考虑编码,故应使IDEA编码格式为utf-8
在这里插入图片描述

(2)在resources目录下创建i18n(internationalization)目录,并在此目录下创建下列文件
在这里插入图片描述
注意“Resource Bundle ‘login’”目录是系统自动生成的。

(3)配置登录元素国际化
点击“Resource Bundle”按钮进入可视化界面
在这里插入图片描述
点击加号新建一个property key:login.tip
在这里插入图片描述
在这里插入图片描述
相应的再新建其他几个property key。
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
(4)在application.properties文件中新增国际化的相关配置

#配置国际化相关文件位置
spring.messages.basename=i18n.login

二、修改前端页面

修改index.html文件,主要是引入国际化。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Signin Template for Bootstrap</title>
    <!-- Bootstrap core CSS -->
    <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link th:href="@{/css/signin.css}" rel="stylesheet">
</head>

<body class="text-center">
<form class="form-signin" action="dashboard.html">
    <img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" alt="" width="72" height="72" src="">
    <!--修改-->
    <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>-->
    <label>
        <!--修改-->
        <input type="text" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
    </label>
    <!--修改-->
    <!--<label class="sr-only" th:text="#{login.password}">Password</label>-->
    <label>
        <!--修改-->
        <input type="password" class="form-control" th:placeholder="#{login.password}" required="">
    </label>
    <div class="checkbox mb-3">
        <!--修改-->
        <label>
            <input type="checkbox" value="remember-me">[[#{login.rememberMe}]]
        </label>
    </div>
    <!--修改-->
    <button class="btn btn-lg btn-primary btn-block" type="submit">[[#{login.signInBtn}]]</button>
    <p class="mt-5 mb-3 text-muted">© 2017-2018</p>
    <!--修改-->
    <a class="btn btn-sm" th:href="@{/index.html(language='zh_CN')}">中文</a>
    <a class="btn btn-sm" th:href="@{/index.html(language='en_US')}">English</a>
</form>

</body>

</html>

(2)启动项目成功后,在浏览器中输入

http://localhost:8080/index.html

可得到如下页面:
在这里插入图片描述

三、新建国际化解析器配置类

(1)新建国际化解析器配置类
MyLocaleResolver.java

package com.example.employee_management.config;

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;

/**
 * @className: MyLocalResolver
 * @description: 国际化解析器配置类
 */
public class MyLocaleResolver implements LocaleResolver {
    /**
     * 解析请求
     *
     * @param httpServletRequest
     * @return
     */
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {

        //获取请求中的语言参数
        String language = httpServletRequest.getParameter("language");

        //如果没有就使用默认的
        Locale locale = Locale.getDefault();

        //请求链接携带了国际化参数
        if (!StringUtils.isEmpty(language)) {

            //格式:zh_CN
            String[] splits = language.split("_");
            //国家 地区
            locale = new Locale(splits[0], splits[1]);

        }

        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}

(2)在MVC配置类里面将定义的国际化组件放入ioc容器中

package com.example.employee_management.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;

/**
 * @className: MyMvcConfig
 * @description: MVC 控制器  借助注解完成控制器而不用手动编写
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    /**
     * MVC 添加首页控制器
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //这里 “/”和“/index.html”效果一样,因为web项目默认页是index.html
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
    }


    /**
     * 将国际化组件放入ioc容器中
     *
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }
}

(3)再次启动项目成功后,在浏览器中输入

http://localhost:8080/index.html

现在点击“中文”“English”链接即可实现语言的切换。
在这里插入图片描述

四、总结

关于实现国际化需要注意的地方有:

  • 对于前端界面的每一个元素都需要创建国际化配置项,并且要在springboot默认的配置文件里指明国际化配置项文件位置
  • 需要创建一个国际化配置类,并且要在之前建立好的MVC配置类里面将国际化组件放入ioc容器中

在这里插入图片描述
2020.04.10

发布了82 篇原创文章 · 获赞 91 · 访问量 9499

猜你喜欢

转载自blog.csdn.net/ataraxy_/article/details/105441289