动态数据国际化
Hello 大家好,许久不见,之前的博客中有介绍国际化定义以及 spring-boot
静态配置文件的国际化方式。这次会带入一些场景,引发一些新的讨论,并且给出对应的解决方案,或许能给大家带来一些帮助。
背景
正式开始前先介绍一下背景,目前本人公司正在接触一些海外出口项目,国际化成为最基本的需求(具体什么语言视项目而定,客户群体不同,语言不同)。
那么试想下默认方案,其缺点显而易见,静态配置文件的形式不够灵活,扩展起来相对麻烦,如果需要新增或修改国际化信息就要重新加载对应的配置文件并重启项目,非常不友好。
所以我们需要实现动态配置国际化信息,最直接的方式就是通过数据库将国际化信息持久化,随时新增或更新国际化信息。
解决方案
为方便演示,示例使用 H2 内存数据库,直接启动项目即可
数据表设计:
列名 | 类型 | 备注 |
---|---|---|
id | bigint | 主键ID |
locale | char | 语言名称 |
code | char | 标记 |
content | varchar | 内容 |
DROP TABLE IF EXISTS sys_language;
CREATE TABLE `sys_language`
(
`id` BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
`locale` CHAR(6) DEFAULT NULL COMMENT '语言名称',
`code` CHAR(32) DEFAULT NULL COMMENT '标记',
`content` VARCHAR(1023) DEFAULT NULL COMMENT '内容'
)
INSERT INTO `sys_language` VALUES (1L, 'zh-CN','SUCCESS','成功');
INSERT INTO `sys_language` VALUES (2L, 'en-US','SUCCESS','success');
多语言适配器:
@Component
public class I18nDynamicAdapter {
@Resource
private LanguageService languageService;
public Language dynamicMessage(Locale locale, String code) {
String local = (null != locale) ? locale.toLanguageTag() : Locale.getDefault().toLanguageTag();
LambdaQueryWrapper<Language> wrapper = Wrappers.<Language>lambdaQuery()
.eq(Language::getLocale, local)
.eq(StringUtils.isNotEmpty(code), Language::getCode, code);
Language language = this.languageService.getOne(wrapper);
return (null != language) ? language : new Language().setLocale(local).setCode(code);
}
}
原先通过配置文件或代码配置的国际化资源需要重写(改为数据库获取):
@Component
public class I18nMessageSource extends AbstractMessageSource {
@Resource
private I18nDynamicAdapter i18nDynamicAdapter;
@Override
protected MessageFormat resolveCode(@Nullable String code, @Nullable Locale locale) {
Language language = i18nDynamicAdapter.dynamicMessage(locale, code);
return (null != language && StringUtils.isNotEmpty(language.getContent())) ?
createMessageFormat(language.getContent(), locale) : null;
}
}
国际化资源配置:
@Configuration
public class I18nConfiguration {
@Bean
public MessageSource messageSource() {
return new I18nMessageSource();
}
}
获取国际化内容
启动项目访问接口文档,请求头中切换语言类型即可。
1.中文
2.英文
整合DEMO仓库地址