SpringBoot 国际化配置,SpringBoot Locale 国际化使用方法

2018 年 8 月 2 日 0 条评论 18.73k 次阅读 5 人点赞

在项目中,很多时候需要国际化的支持,这篇文章要介绍一下springboot项目中多语言国际化的使用。
本文项目结构如图:

 

springboot默认就支持国际化的,而且不需要你过多的做什么配置,只需要在resources/下创建国际化配置文件即可,注意名称必须以messages开始。 messages.properties (默认的语言配置文件,当找不到其他语言的配置的时候,使用该文件进行展示)。
messages_zh_CN.properties(中文)
messages_en_US.properties(英文)

messages_zh_CN.properties   内容如下:

welcome = 您好,欢迎你!

messages_en_US.properties   内容如下:

welcome = hello, welcome !

在这个项目中前端页面使用的thymeleaf。创建Springboot项目不在讲述,Eclipase,IDEA创建项目都很方便快捷。pom文件记得加入 thymeleaf 支持。
pom.xml  文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.auan</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

 

新建IndexController:

package cn.auan.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.ui.Model;


/**
 * @author 十三月
 * @Description
 * @date 2018-08-02 下午4:57
 */
@Controller
public class IndexController {

    @Autowired
    private MessageSource messageSource;

    @RequestMapping("/")
    public String hello(Model model){

        Locale locale = LocaleContextHolder.getLocale();
        model.addAttribute("welcome", messageSource.getMessage("welcome", null, locale));

        return "index";
    }
}

 

 

到这里可以看出来,其实和整合thymeleaf一样。

然后在templates下新建index.html,代码如下:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    点击切换语言:
    <a href="?lang=zh_CN">简体中文</a> &nbsp;&nbsp;
    <a href="?lang=en_US">English(US)</a><br>
    <p>
        <h2 th:text="#{welcome}"></h2>
    </p>
</body>
</html>

 

创建国际化配置文件,LocaleConfig 代码如下:
我这里是通过Cookie方式,用Session也可以。

package cn.auan.config;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

import java.util.Locale;

/**
 * @author 十三月
 * @Description 国际化配置
 * @date 2018-08-02 下午4:57
 **/


@Configuration
@EnableAutoConfiguration
@ComponentScan
public class LocaleConfig extends WebMvcConfigurerAdapter {

    //Cookie
    @Bean
    public LocaleResolver localeResolver() {
        CookieLocaleResolver localeResolver = new CookieLocaleResolver();
        localeResolver.setCookieName("localeCookie");
        //设置默认区域
        localeResolver.setDefaultLocale(Locale.ENGLISH);
        localeResolver.setCookieMaxAge(3600);//设置cookie有效期.
        return localeResolver;
    }



    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        // 参数名
        lci.setParamName("lang");
        return lci;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }



}

 

现在启动项目,访问 http://localhost:8080/
然后点击中文或者English就可以自由切换语言了。
springboot-i18n 国际化

 

项目包下载路径:https://gitee.com/xueleilei/Springboot-internationalization

雷雷

这个人太懒什么东西都没留下

文章评论(0)

(Spamcheck Enabled)