SpringBoot的國際化使用

在項目中,不少時候須要國際化的支持,這篇文章要介紹一下springboot項目中國際化的使用。html

在這個項目中前端頁面使用的thymeleaf,另外加入了nekohtml去掉html嚴格校驗,若是不瞭解springboot和thymeleaf的使用,能夠去看個人上一篇文章《SpringBoot集成Thymeleaf》前端

新建一個springboot項目,pom文件代碼以下:java

<?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.dalaoyang</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配置:web

##端口號
server.port=8888


##去除thymeleaf的html嚴格校驗
spring.thymeleaf.mode=LEGACYHTML5

#設定thymeleaf文件路徑 默認爲src/main/resources/templates
spring.freemarker.template-loader-path=classpath:/templates
複製代碼

新建IndexControllerspring

@Controller
public class IndexController {

    @RequestMapping("/")
    public String hello(Model model){
        return "index";
    }
}
複製代碼

到這裏能夠看出來,其實和整合thymeleaf同樣。apache

接下來咱們要加入國際化的關鍵,在resources裏面新建messages.properties(默認配置),messages_en_US.properties(英文),messages_zh_CN.properties(中文)springboot

其中messages.properties裏面加入:bash

message = 歡迎使用國際化(默認)
複製代碼

messages_en_US.properties裏面加入:app

message = Welcome to internationalization (English)
複製代碼

messages_zh_CN.properties裏面加入maven

message = \u6b22\u8fce\u4f7f\u7528\u56fd\u9645\u5316\uff08\u4e2d\u6587\uff09
複製代碼

而後在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=en_US">English(US)</a>
<a href="/?lang=zh_CN">簡體中文</a></br>
<p><label th:text="#{message}"></label></p>


</body>
</html>
複製代碼

建立國際化配置文件,I18Config 代碼以下:

package com.dalaoyang.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 dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.config
 * @email 397600342@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.dalaoyang.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 dalaoyang
 * @Description
 * @project springboot_learn
 * @package com.dalaoyang.controller
 * @email 397600342@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就能夠自由切換語言了。

相關文章
相關標籤/搜索