背景
基于客户的诉求,部分国际交付项目需要实现前端页面、弹窗的多语言实现。随着交付项目的增多,有此需求的项目也在日益增长。针对Java语言,多语言的实现基本都一样,因此需要一套统一的实现。
主要功能
● "静态"内容的国际化,包括异常信息,提示信息,前端显示的内容,比如菜单配置,权限配置等。
● "动态"内容的国际化,包括用户录入,可动态增加/修改的内容,比如政府部门的名称,街道地址等。
● "nacos"内容的国际化,这类内容的国际化方案试用nacos的动态注册,通过修改nacos来做到实时生效。
springboot-demo下载
📎i18n-sample-app.zip
📎i18n-sample-app_v1.0.1.zip
对接指导
1.1 setting.xml
📎settings_bpaas.xml
1.2 pom.xml
①.引入maven坐标:(2021-9-7截止,aliyun-gts-starter-i18n最新版本为1.0.5-SNAPSHOT)
②.项目根目录引入
<dependencyManagement>
<dependency>
<groupId>com.aliyun.gts.bpaas</groupId>
<artifactId>aliyun-gts-starter-i18n</artifactId>
<version>1.0.5-SNAPSHOT</version>
</dependency>
</dependencyManagement>
③.然后在使用的模块 dependencies
中添加如下依赖。
<dependency>
<groupId>com.aliyun.gts.bpaas</groupId>
<artifactId>aliyun-gts-starter-i18n</artifactId>
</dependency>
1.3 配置文件配置
server:
servlet:
context-path: /i18nTest
spring:
application:
name: aliyun-gts-starter-i18n-test
# Spring Message 的配置,使用 bundle-resource 必须配置此配置
messages:
basename: i18n/messages
encoding: UTF-8
fallbackToSystemLocale: false
gts:
i18n:
nacos-resource:
// 是否自动装配 nacosResource Bean
enable: true
serverAddress: XXX
username: XXX
password: XXXX
dataId: XXXX
group: XXXXX
memory-resource:
// 是否自动装配 memoryResource Bean
enable: true
#生成国际化标识Key
key-generator:
namespace: TFYYIOUJBHGHUBDABBDKJLKJB
default-code: code
delimiter: ^
bundle-resource:
enable: true
web:
code-field-name: code
message-field-name: message
view-object-class-name: com.aliyun.gts.bpass.i18n.support.protocol.JsonProtocol
cache:
type: redis
prefix:
host: XXXX
port: XXXX
auth: XXXX
database: XXXX
SDK与API
介绍
目前分为三种适配器方式,分别为BundleAdapter、memoryAdapter、nacosAdapter
BundleAdapter
API列表
/**
* 通过资源key和语言获取国际化资源
* @param key 资源key
* @param locale 语言信息
* @return 国际化后的字符串
*/
String getString(String key, Locale locale);
/**
* 通过code标识和唯一id获取国际化资源
* @see com.aliyun.gts.bpass.i18n.key.KeyGenerator
* @param code code标识
* @param id 唯一主键
* @param locale 语言信息
* @return 国际化资源
*/
String getStringWithCode(String code, String id, Locale locale);
/**
* 通过默认code标识和唯一id获取国际化资源
* @see com.aliyun.gts.bpass.i18n.key.KeyGenerator
* @param id 唯一主键
* @param locale 语言信息
* @return 国际化资源
*/
default String getStringWithId(String id, Locale locale) {
return getStringWithCode(null, id, locale);
}
试用demo
1.首先可以在项目的静态文件下创建i18n相关资源如图
2.设置浏览器的语种,这里以Google为例
3.语种从浏览器的Accept-Language获取
4.也可以根据以下配置设置默认的语种,如果messages配置下没有浏览器选择的语种的时候,就会试用默认的配置语种。
i18n.setDefaultLocale(new Locale("zh", "TW"));
5.api调用
@Resource
private BundleResource i18n;
public ResultResponse testGetStringForThrow(){
//设置报错抛出异常信息
i18n.setThrowExceptionIfNotExists(true);
根据浏览器当前的语言获取相应的翻译
String string = i18n.getString(key, I18nUtils.getCurrentLanguage());
String string1 = i18n.getStringWithCode(code,id,I18nUtils.getCurrentLanguage());
String string2 = i18n.getStringWithId(id,I18nUtils.getCurrentLanguage());
return ResultResponse.succResult(string);
}
效果演示
1.把浏览器设置成中文(简体)
memoryAdapter
API列表
/**
* 通过资源key和语言获取国际化资源
* @param key 资源key
* @param locale 语言信息
* @return 国际化后的字符串
*/
String getString(String key, Locale locale);
/**
* 通过code标识和唯一id获取国际化资源
* @see com.aliyun.gts.bpass.i18n.key.KeyGenerator
* @param code code标识
* @param id 唯一主键
* @param locale 语言信息
* @return 国际化资源
*/
String getStringWithCode(String code, String id, Locale locale);
/**
* 通过默认code标识和唯一id获取国际化资源
* @see com.aliyun.gts.bpass.i18n.key.KeyGenerator
* @param id 唯一主键
* @param locale 语言信息
* @return 国际化资源
*/
default String getStringWithId(String id, Locale locale) {
return getStringWithCode(null, id, locale);
}
/**
* 设置国际化资源
* @param key 资源key
* @param value 对应语言的值
* @param locale 语言信息
*/
void setString(String key, String value, Locale locale);
/**
*
* 通过code标识更新/写入国家化资源
* @param code code标识
* @param id 唯一主键
* @param locale 语言信息
*/
void setStringWithCode(String code, String id, String value, Locale locale);
/**
*
* 通过默认code标识更新/写入国家化资源
* @param id 唯一主键
* @param locale 语言信息
*/
default void setStringWithId(String id, String value, Locale locale) {
setStringWithCode(null, id, value, locale);
}
试用demo
1.设置浏览器的语种,这里以Google为例
2.语种从浏览器的Accept-Language获取
3.如果想使用中间件cacheManager
在对接项目中引入cacheManager的依赖和相关配置可以参考文档
https://dayu.work/devops/materiel/component?sceneId=2001&classificationId=2102&comId=3210
如果不使用cacheManager则走本地的内存,
注意:多机部署不建议使用本地内存。
4.api调用
@Resource
private MemoryResource i18n;
public ResultResponse testGetStringForThrow(String key){
i18n.setThrowExceptionIfNotExists(true);
String string = i18n.getString(key, I18nUtils.getCurrentLanguage());
}
public void testMemorySetStringWithCode( UserEntity userEntity){
i18n.setThrowExceptionIfNotExists(true);
i18n.setDefaultLocale(I18nUtils.getCurrentLanguage());
List<KeyValuePair> pairs = extractor.extract("userName", userEntity);
for (KeyValuePair p : pairs) {
i18n.setStringWithCode(USER_NAME_CODE, userEntity.getUserCode(), p.getValue(), p.getLocale());
}
}
public ResultResponse testMemoryGetStringWithCode( String userCode){
i18n.setThrowExceptionIfNotExists(true);
i18n.setDefaultLocale(new Locale("zh", "TW"));
String stringWithCode = i18n.getStringWithCode(USER_NAME_CODE, userCode, I18nUtils.getCurrentLanguage());
return ResultResponse.succResult(stringWithCode);
}
效果演示
1.可以动态的set 设置多语言,可以调用接口
http://localhost:8080/i18nTest/memory/testMemorySetStringWithCode
nacosAdapter
API列表
/**
* 通过资源key和语言获取国际化资源
* @param key 资源key
* @param locale 语言信息
* @return 国际化后的字符串
*/
String getString(String key, Locale locale);
/**
* 通过code标识和唯一id获取国际化资源
* @see com.aliyun.gts.bpass.i18n.key.KeyGenerator
* @param code code标识
* @param id 唯一主键
* @param locale 语言信息
* @return 国际化资源
*/
String getStringWithCode(String code, String id, Locale locale);
/**
* 通过默认code标识和唯一id获取国际化资源
* @see com.aliyun.gts.bpass.i18n.key.KeyGenerator
* @param id 唯一主键
* @param locale 语言信息
* @return 国际化资源
*/
default String getStringWithId(String id, Locale locale) {
return getStringWithCode(null, id, locale);
}
试用demo
1.首先需要在nacos注册中心配置相关的配置
2.设置浏览器的语种,这里以Google为例
3.语种从浏览器的Accept-Language获取
4.也可以根据以下配置设置默认的语种,如果messages配置下没有浏览器选择的语种的时候,就会试用默认的配置语种。
i18n.setDefaultLocale(new Locale("zh", "TW"));
5.api调用
@Resource
private NacosResource i18n;
public ResultResponse testGetStringForThrow(){
//设置报错抛出异常信息
i18n.setThrowExceptionIfNotExists(true);
根据浏览器当前的语言获取相应的翻译
String string = i18n.getString(key, I18nUtils.getCurrentLanguage());
String string1 = i18n.getStringWithCode(code,id,I18nUtils.getCurrentLanguage());
String string2 = i18n.getStringWithId(id,I18nUtils.getCurrentLanguage());
return ResultResponse.succResult(string);
}
效果演示
1.把浏览器设置成中文(简体)
2.启动上面提供的springboot-demo项目,访问接口
http://localhost:8080/i18nTest/nacos/testGetStringForThrow?key=country