在项目中,很多时候需要国际化的支持,这篇文章要介绍一下springboot项目中国际化的使用。
在这个项目中前端页面使用的thymeleaf,另外加入了nekohtml去掉html严格校验,如果不了解springboot和thymeleaf的使用,可以去看我的上一篇文章《SpringBoot集成Thymeleaf》。
新建一个springboot项目,pom文件代码如下:
4.0.0 com.dalaoyang springboot_internationalization0.0.1-SNAPSHOT jar springboot_internationalization springboot_internationalization org.springframework.boot spring-boot-starter-parent1.5.10.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-devtoolsruntime org.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-starter-thymeleafnet.sourceforge.nekohtml nekohtml1.9.15 org.springframework.boot spring-boot-maven-plugin
从上面可以看出,其实和之前结合thymeleaf的时候一样。接下来给大家看一下application.propertie配置:
##端口号 server.port=8888 ##去除thymeleaf的html严格校验 spring.thymeleaf.mode=LEGACYHTML5 #设定thymeleaf文件路径 默认为src/main/resources/templates spring.freemarker.template-loader-path=classpath:/templates
新建IndexController
@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 = u6b22u8fceu4f7fu7528u56fdu9645u5316uff08u4e2du6587uff09
然后在templates下新建index.html,代码如下:
Title
English(US)
简体中文
创建国际化配置文件,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
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;
@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就可以自由切换语言了。
源码下载 :大老杨码云



