目录
配置
LocaleConfig
package com.example.demo.config;
/**
* Created on 2019/8/19.
*
* @author yangsen
*/
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.util.Locale;
/**
* 配置国际化语言
* @author Max.Yang
*/
@Configuration
public class LocaleConfig implements WebMvcConfigurer {
/**
* 默认解析器 其中locale表示默认语言
*/
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(Locale.CHINESE);
return localeResolver;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:static/i18n/messages");
messageSource.setCacheSeconds(10);
messageSource.setDefaultEncoding("GBK");
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
// 参数名
lci.setParamName("lang");
return lci;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
WebMvcConfigurer 会拦截请求lang后面的请求进行映射,
messageSource.setBasename("classpath:static/i18n/messages");一般这个会配置在application.properties,可是我配置完不成功
其次是GBK编码,如果是utf-8的话会变成???
MessageUtils
package com.example.demo.util;
/**
* Created on 2019/8/19.
*
* @author yangsen
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
/**
* 国际化工具类
* @author Max.Yang
*/
@Component
public class MessageUtils {
@Autowired
private MessageSource messageSource;
/**
* 获取单个国际化翻译值
*/
public String get(String msgKey) {
return messageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale());
}
}
后面这个可以设定Locale.US,或者Locale.CHINESE来指定映射的内容
TestController
package com.example.demo.controller;
import com.example.demo.util.MessageUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* Created on 2019/8/19.
*
* @author yangsen
*/
@RestController
public class TestController {
@Resource
MessageUtils messageUtils;
//http://localhost:8080/get?lang=en_US
@GetMapping("/get")
public String a(){
return messageUtils.get("user.title");
}
}
因为在默认配置的话是中文。所以其他lang请求都是映射到中文那里,如果要映射到英文的话需要请求http://localhost:8080/get?lang=en_US
github
https://github.com/dajitui/i18n-demo/tree/master