SpringBoot后台国际化

SpringBoot后台国际化

新建国际化资源文件

在resources下面新建了三个文件

1

messages.properties是必须的,内容可以为空,但是必须有这个文件

messages_zh_CN.properties和messages_en_US.properties分别是中文和英文资源

2

3

配置国际化

如果是从请求头中获取Local Message

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

@Configuration
public class LocaleConfig extends AcceptHeaderLocaleResolver implements WebMvcConfigurer {

@Override
public Locale resolveLocale(HttpServletRequest request) {
String headerLang = request.getHeader("language");
return headerLang == null || headerLang.isEmpty()
? Locale.CHINESE
: new Locale(headerLang);
}
@Bean
public LocaleResolver localeResolver(){
return new LocaleConfig();
}
}

如果是从HTTP请求中参数Local Message

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import org.springframework.context.annotation.Bean;
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.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

@Configuration
public class LocaleConfig {
/**
* 默认解析器 其中locale表示默认语言
*/
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(Locale.CHINESE);
return localeResolver;
}

/**
* 默认拦截器 其中lang表示切换语言的参数名
*/
@Bean
public WebMvcConfigurer localeInterceptor() {
return new WebMvcConfigurer() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
localeInterceptor.setParamName("lang");
registry.addInterceptor(localeInterceptor);
}
};
}
}

编写工具类获取Local Message

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

@Component
public class MessageUtils {

@Autowired
private MessageSource messageSource;

public String getLocale(String msgCode) {
try {
return messageSource.getMessage(msgCode, null, LocaleContextHolder.getLocale());
} catch (Exception e) {
return msgCode;
}
}
}

使用

在REST API中使用

1
2
3
4
5
6
7
8
9
10
11
12
@RestController
@RequestMapping("/TestController")
public class TestController {

@Resource
private MessageUtils messageUtils;

@PostMapping("/test")
public String test() {
return messageUtils.getLocale("test");
}
}

测试

4

5

如果出现中文乱码的情况

6

在Settings中将编码格式改成UTF-8

7

然后将乱码的文字重新编写一下就好了

8

如果对您有帮助,可以打赏呦!