0%

SpringBoot 代码中使用国际化配置文件

国际化文件

1
2
3
4
5
6
7
8
9
10
resources
│ application.yml
│ logback.xml
│ mybatis-config.xml

└─i18n
lang.properties
lang_en_US.properties
lang_zh_CN.properties
lang_zh_TW.properties

yml 配置文件

1
2
3
4
5
6
i18n: 
locale: zh_CN
spring:
messages:
basename: i18n/lang
encoding: UTF-8

启动类注入国际化配置

1
2
3
4
5
6
7
8
9
@Value("${i18n.locale}")
private String locale;


@Bean
public LocaleResolver localeResolver() {
Locale locale = I18nUtils.localeFromString(this.locale, (Locale)null);
return new FixedLocaleResolver(locale);
}

使用工具类获取国际化配置

这里应该使用 Aware 接口.

1
2
3
4
5
6
7
8
9
10
11
12
13
public class I18nToolUtils {

/**
* 国际化信息服务
*/
private final static MessageSource messageSource = SpringBeanUtils.getBean(MessageSource.class);


public static String getI18String(String code) {
if (StringUtils.isBlank(code)) return null;
return messageSource.getMessage(code, null, LocaleContextHolder.getLocale());
}
}