springboot实现返回值国际化
开始一个新项目,有幸接触到后端进行添加国际化的设计,学习大佬的代码,并自己写了一个demo,记录如下:
- 创建一个springboot工程,引入spring-web-starter的坐标
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>compile</scope>
</dependency>
</dependencies>
- 创建文件夹以及配置文件
- 创建中文配置文件
4.配置文件信息,点击OK
5.添加配置文件信息
6.看一下结构目录
7.创建一个实体类,在每次调用的时候加载该类
@Component
@Scope(value = WebApplicationContext.SCOPE_REQUEST)
public class MsgStorage {
private Locale locale;
public Locale getLocale() {
return locale;
}
public void setLocale(Locale locale) {
this.locale = locale;
}
}
8.进行国际化语言的配置
import javax.inject.Provider;
public class I18n {
@Autowired
private Provider<MsgStorage> msgStorage;
private final MessageSource messageSource;
public I18n(MessageSource messageSource){
this.messageSource = messageSource;
}
public String getMessage(String msg){
return messageSource.getMessage(msg,null, msgStorage.get().getLocale());
}
public String getMessage1(String msg,Object[] args){
return messageSource.getMessage(msg,args,msgStorage.get().getLocale());
}
}
9.将国际化的配置进行加载
import java.util.Locale;
@Configuration
public class I18nConfig {
@Bean
public I18n i18nService() {
return new I18n(messageSource());
}
@Bean
public ResourceBundleMessageSource messageSource() {
Locale.setDefault(Locale.CHINESE);
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasenames("i18n/demo");// name of the resource bundle
source.setUseCodeAsDefaultMessage(true);
source.setDefaultEncoding("UTF-8");
return source;
}
}
10.编写一个拦截器,在每次请求的时候将语言信息记录一下
import javax.inject.Provider;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
@Component
public class ApiInterceptor implements HandlerInterceptor {
@Autowired
private Provider<MsgStorage> msgStorage;
@Autowired
public ApiInterceptor(Provider<MsgStorage> msgStorage) {
this.msgStorage = msgStorage;
}
/**
* 编写拦截器,设置判断语言类别
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//获取请求投中的语言类型
String language = request.getHeader("language");
//根据语言类型进行设置
if("en".equals(language)){
msgStorage.get().setLocale(Locale.ENGLISH);
}
if("zh".equals(language)){
msgStorage.get().setLocale(Locale.CHINESE);
}
return true;
}
}
11.将拦截器进行加载
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Autowired
private ApiInterceptor apiInterceptor;
/**
* 将拦截器注册到web容器中
* @param registry
*/
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(apiInterceptor);
}
/**
* 解决乱码问题
* @return
*/
@Bean
public HttpMessageConverter<String> responseBodyConverter() {
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
stringHttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8));
return stringHttpMessageConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(responseBodyConverter());
}
}
12.编写一个配置类
public class CommonRespMsg {
/**
* 你好,宝贝
*/
public static final String DEMO_HELLO_BABY = "DEMO_HELLO_BABY";
/**
* 你好,小姑娘
*/
public static final String DEMO_HELLO_GIRL = "DEMO_HELLO_GIRL";
}
13.配置controller进行测试
@RestController
@RequestMapping("i18n")
public class I18nController {
@Autowired
private I18n i18n;
@GetMapping("baby")
public String baby(){
return i18n.getMessage(CommonRespMsg.DEMO_HELLO_BABY);
}
@GetMapping("girl")
public String girl(){
return i18n.getMessage(CommonRespMsg.DEMO_HELLO_GIRL);
}
}
14.进行测试
中文:
英文:
希望对你有帮助