前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot(13)国际化

springboot(13)国际化

作者头像
IT架构圈
发布2018-06-01 13:04:15
4910
发布2018-06-01 13:04:15
举报
文章被收录于专栏:IT架构圈

介绍一下springboot项目中国际化的使用。

在这个项目中前端页面使用的thymeleaf,另外加入了nekohtml去掉html严格校验,如果不了解springboot和thymeleaf的使用,可以去看我的之前文章文章《springboot (二) thymeleaf》。

新建一个springboot项目,pom文件代码如下:

代码语言:javascript
复制
<?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>com.liming</groupId>
    <artifactId>springboot_internationalization</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>springboot_internationalization</name>
    <description>springboot_internationalization</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.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-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-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.15</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

从上面可以看出,其实和之前结合thymeleaf的时候一样。接下来给大家看一下application.propertie配置:

端口号

代码语言:javascript
复制
server.port=8888

去除thymeleaf的html严格校验

代码语言:javascript
复制
spring.thymeleaf.mode=LEGACYHTML5

设定thymeleaf文件路径 默认为src/main/resources/templates

代码语言:javascript
复制
spring.freemarker.template-loader-path=classpath:/templates

新建IndexController

代码语言:javascript
复制
@Controller
public class IndexController {
    @RequestMapping("/")
    public String hello(Model model){
        return "index";
    }
}

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

接下来我们要加入国际化的关键,在resources里面新建messages.properties(默认配置),messages_en_US.properties(英文),messages_zh_CN.properties(中文)

其中messages.properties里面加入:

message = 欢迎使用国际化(默认) messages_en_US.properties里面加入:

message = Welcome to internationalization (English) messages_zh_CN.properties里面加入

message = \u6b22\u8fce\u4f7f\u7528\u56fd\u9645\u5316\uff08\u4e2d\u6587\uff09 然后在templates下新建index.html,代码如下:

代码语言:javascript
复制
<!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=en_US">English(US)</a>
<a href="/?lang=zh_CN">简体中文</a></br>
<p><label th:text="#{message}"></label></p>
</body>
</html>

创建国际化配置文件,I18Config 代码如下:

代码语言:javascript
复制
package com.liming.config;
import java.util.Locale;
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.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Configuration
@EnableAutoConfiguration
@ComponentScan
/**
 * @author liming
 * @Description
 * @project springboot_learn
 * @package com.liming.config
 * @email 394498036@qq.com
 * @date 2018/3/28
 */
public class I18Config extends WebMvcConfigurerAdapter{
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        // 默认语言
        slr.setDefaultLocale(Locale.US);
        return slr;
    }
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        // 参数名
        lci.setParamName("lang");
        return lci;
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }
}
最后修改IndexController,修改成如下:
package com.liming.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 liming
 * @Description
 * @project springboot_learn
 * @package com.liming.controller
 * @email 394498036@qq.com
 * @date 2018/3/28
 */
@Controller
public class IndexController {
    @Autowired
    private MessageSource messageSource;
    @RequestMapping("/")
    public String hello(Model model){
        Locale locale = LocaleContextHolder.getLocale();
        model.addAttribute("message", messageSource.getMessage("message", null, locale));
        return "index";
    }
}

现在启动项目,访问http://localhost:8888/

然后点击中文或者English就可以自由切换语言了。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-04-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 编程坑太多 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 端口号
  • 去除thymeleaf的html严格校验
  • 设定thymeleaf文件路径 默认为src/main/resources/templates
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档