【i18n】初始化多语言模块

pull/3/head
fengshuonan 2021-01-24 19:41:45 +08:00
parent cd33e36957
commit 291607886b
30 changed files with 890 additions and 0 deletions

1
kernel-d-i18n/README.md Normal file
View File

@ -0,0 +1 @@
# 多语言模块

View File

@ -0,0 +1 @@
多语言模块的api

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>kernel-d-i18n</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>i18n-api</artifactId>
<packaging>jar</packaging>
<dependencies>
<!--校验模块的api-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>validator-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,36 @@
package cn.stylefeng.roses.kernel.i18n.api;
import cn.stylefeng.roses.kernel.i18n.api.enums.TranslationEnum;
import cn.stylefeng.roses.kernel.i18n.api.pojo.TranslationDict;
import java.util.List;
import java.util.Map;
/**
* api
*
* @author fengshuonan
* @date 2021/1/24 18:50
*/
public interface TranslationApi {
/**
*
*
* @param translationDict
* @author fengshuonan
* @date 2021/1/24 19:00
*/
void init(List<TranslationDict> translationDict);
/**
*
*
* @param translationLanguages
* @return key-value-TranslationApi
* @author fengshuonan
* @date 2021/1/24 19:01
*/
Map<String, String> getTranslationDictByLanguage(TranslationEnum translationLanguages);
}

View File

@ -0,0 +1,23 @@
package cn.stylefeng.roses.kernel.i18n.api;
import cn.stylefeng.roses.kernel.i18n.api.pojo.TranslationDict;
import java.util.List;
/**
* api
*
* @author fengshuonan
* @date 2021/1/24 19:32
*/
public interface TranslationPersistenceApi {
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:33
*/
List<TranslationDict> getAllTranslationDict();
}

View File

@ -0,0 +1,21 @@
package cn.stylefeng.roses.kernel.i18n.api.constants;
/**
*
*
* @author fengshuonan
* @date 2021/1/24 16:36
*/
public interface TranslationConstants {
/**
*
*/
String I18N_MODULE_NAME = "kernel-d-i18n";
/**
*
*/
String I18N_EXCEPTION_STEP_CODE = "25";
}

View File

@ -0,0 +1,24 @@
package cn.stylefeng.roses.kernel.i18n.api.context;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.i18n.api.TranslationApi;
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:06
*/
public class TranslationContext {
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:06
*/
public TranslationApi me() {
return SpringUtil.getBean(TranslationApi.class);
}
}

View File

@ -0,0 +1,75 @@
package cn.stylefeng.roses.kernel.i18n.api.enums;
import lombok.Getter;
import java.util.ArrayList;
import java.util.List;
/**
*
*
* @author fengshuonan
* @date 2021/1/24 18:57
*/
@Getter
public enum TranslationEnum {
/**
*
*/
CHINESE(1, "中文"),
/**
*
*/
ENGLISH(2, "English");
/**
*
*/
private final Integer code;
/**
*
*/
private final String description;
TranslationEnum(Integer code, String description) {
this.code = code;
this.description = description;
}
/**
* typecode
*
* @author fengshuonan
* @date 2019/10/18 10:13
*/
public static List<String> types() {
ArrayList<String> integers = new ArrayList<>();
for (TranslationEnum value : TranslationEnum.values()) {
integers.add(value.name());
}
return integers;
}
/**
* code
*
* @author fengshuonan
* @date 2019/10/18 10:33
*/
public static TranslationEnum valueOf(Integer value) {
if (value == null) {
return null;
} else {
for (TranslationEnum translationLanguages : TranslationEnum.values()) {
if (translationLanguages.getCode().equals(value)) {
return translationLanguages;
}
}
return null;
}
}
}

View File

@ -0,0 +1,24 @@
package cn.stylefeng.roses.kernel.i18n.api.exception;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.i18n.api.constants.TranslationConstants;
import cn.stylefeng.roses.kernel.rule.abstracts.AbstractExceptionEnum;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
/**
*
*
* @author fengshuonan
* @date 2020/10/15 15:59
*/
public class TranslationException extends ServiceException {
public TranslationException(AbstractExceptionEnum exception, Object... params) {
super(TranslationConstants.I18N_MODULE_NAME, exception.getErrorCode(), StrUtil.format(exception.getUserTip(), params));
}
public TranslationException(AbstractExceptionEnum exception) {
super(TranslationConstants.I18N_MODULE_NAME, exception);
}
}

View File

@ -0,0 +1,37 @@
package cn.stylefeng.roses.kernel.i18n.api.exception.enums;
import cn.stylefeng.roses.kernel.i18n.api.constants.TranslationConstants;
import cn.stylefeng.roses.kernel.rule.abstracts.AbstractExceptionEnum;
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
import lombok.Getter;
/**
*
*
* @author fengshuonan
* @date 2021/1/24 16:40
*/
@Getter
public enum TranslationExceptionEnum implements AbstractExceptionEnum {
/**
* xx
*/
JWT_PARSE_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + TranslationConstants.I18N_EXCEPTION_STEP_CODE + "01", "jwt解析错误jwt为{}");
/**
*
*/
private final String errorCode;
/**
*
*/
private final String userTip;
TranslationExceptionEnum(String errorCode, String userTip) {
this.errorCode = errorCode;
this.userTip = userTip;
}
}

View File

@ -0,0 +1,37 @@
package cn.stylefeng.roses.kernel.i18n.api.pojo;
import cn.stylefeng.roses.kernel.i18n.api.enums.TranslationEnum;
import lombok.Data;
import java.io.Serializable;
/**
*
*
* @author stylefeng
* @since 2019-10-17
*/
@Data
public class TranslationDict implements Serializable {
/**
* FIELD_ACCOUNT
*/
private String tranCode;
/**
*
*/
private String tranName;
/**
* 1: 2:
*/
private TranslationEnum translationLanguages;
/**
*
*/
private String tranValue;
}

View File

@ -0,0 +1,44 @@
package cn.stylefeng.roses.kernel.i18n.api.pojo.request;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
*
*
* @author stylefeng
* @since 2019-10-17
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class TranslationRequest extends BaseRequest {
private static final long serialVersionUID = 1L;
/**
* id
*/
private Long tranId;
/**
*
*/
private String tranCode;
/**
*
*/
private String tranName;
/**
* 1: 2:
*/
private Integer languages;
/**
*
*/
private String tranValue;
}

View File

@ -0,0 +1 @@
多语言模块的数据管理业务

View File

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>kernel-d-i18n</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>i18n-business</artifactId>
<packaging>jar</packaging>
<dependencies>
<!--多语言翻译的sdk-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>i18n-sdk</artifactId>
<version>1.0.0</version>
</dependency>
<!--资源api模块-->
<!--用在资源控制器,资源扫描上-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>scanner-api</artifactId>
<version>1.0.0</version>
</dependency>
<!--参数校验模块-->
<!--用在控制器,参数校验-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>validator-api</artifactId>
<version>1.0.0</version>
</dependency>
<!--数据库sdk-->
<!--数据库dao框架-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>db-sdk-mp</artifactId>
<version>1.0.0</version>
</dependency>
<!--web模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,90 @@
package cn.stylefeng.roses.kernel.i18n.modular.controller;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.i18n.api.pojo.request.TranslationRequest;
import cn.stylefeng.roses.kernel.i18n.modular.entity.Translation;
import cn.stylefeng.roses.kernel.i18n.modular.service.TranslationService;
import cn.stylefeng.roses.kernel.resource.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.resource.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:18
*/
@RestController
@ApiResource(name = "多语言接口控制器")
public class TranslationController {
@Resource
private TranslationService translationService;
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:17
*/
@PostResource(name = "新增多语言配置", path = "/i18n/add")
public ResponseData addItem(TranslationRequest translationRequest) {
this.translationService.add(translationRequest);
return new SuccessResponseData();
}
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:17
*/
@PostResource(name = "新增多语言配置", path = "/i18n/edit")
public ResponseData editItem(TranslationRequest translationRequest) {
this.translationService.update(translationRequest);
return new SuccessResponseData();
}
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:20
*/
@PostResource(name = "新增多语言配置", path = "/i18n/delete")
public ResponseData delete(TranslationRequest translationRequest) {
this.translationService.delete(translationRequest);
return new SuccessResponseData();
}
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:20
*/
@PostResource(name = "新增多语言配置", path = "/i18n/detail")
public ResponseData detail(TranslationRequest translationRequest) {
Translation detail = this.translationService.findDetail(translationRequest);
return new SuccessResponseData(detail);
}
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:20
*/
@PostResource(name = "新增多语言配置", path = "/i18n/page")
public ResponseData list(TranslationRequest translationRequest) {
PageResult<Translation> page = this.translationService.findPage(translationRequest);
return new SuccessResponseData(page);
}
}

View File

@ -0,0 +1,54 @@
package cn.stylefeng.roses.kernel.i18n.modular.entity;
import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseEntity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:12
*/
@EqualsAndHashCode(callSuper = true)
@TableName("sys_translation")
@Data
public class Translation extends BaseEntity {
private static final long serialVersionUID = 1L;
/**
* id
*/
@TableId(value = "tran_id", type = IdType.ASSIGN_ID)
private Long tranId;
/**
*
*/
@TableField("tran_code")
private String tranCode;
/**
*
*/
@TableField("tran_name")
private String tranName;
/**
* 1: 2:
*/
@TableField("languages")
private Integer languages;
/**
*
*/
@TableField("tran_value")
private String tranValue;
}

View File

@ -0,0 +1,15 @@
package cn.stylefeng.roses.kernel.i18n.modular.mapper;
import cn.stylefeng.roses.kernel.i18n.modular.entity.Translation;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* Mapper
*
* @author fengshuonan
* @date 2021/1/24 19:22
*/
public interface TranslationMapper extends BaseMapper<Translation> {
}

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.stylefeng.guns.i18n.mapper.TranslationMapper">
</mapper>

View File

@ -0,0 +1,57 @@
package cn.stylefeng.roses.kernel.i18n.modular.service;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.i18n.api.TranslationPersistenceApi;
import cn.stylefeng.roses.kernel.i18n.api.pojo.request.TranslationRequest;
import cn.stylefeng.roses.kernel.i18n.modular.entity.Translation;
import com.baomidou.mybatisplus.extension.service.IService;
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:21
*/
public interface TranslationService extends IService<Translation>, TranslationPersistenceApi {
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:27
*/
void add(TranslationRequest param);
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:27
*/
void update(TranslationRequest param);
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:27
*/
void delete(TranslationRequest param);
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:28
*/
Translation findDetail(TranslationRequest param);
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:28
*/
PageResult<Translation> findPage(TranslationRequest param);
}

View File

@ -0,0 +1,53 @@
package cn.stylefeng.roses.kernel.i18n.modular.service.impl;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.i18n.api.pojo.TranslationDict;
import cn.stylefeng.roses.kernel.i18n.api.pojo.request.TranslationRequest;
import cn.stylefeng.roses.kernel.i18n.modular.entity.Translation;
import cn.stylefeng.roses.kernel.i18n.modular.mapper.TranslationMapper;
import cn.stylefeng.roses.kernel.i18n.modular.service.TranslationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.List;
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:38
*/
@Service
public class TranslationServiceImpl extends ServiceImpl<TranslationMapper, Translation> implements TranslationService {
@Override
public void add(TranslationRequest param) {
}
@Override
public void update(TranslationRequest param) {
}
@Override
public void delete(TranslationRequest param) {
}
@Override
public Translation findDetail(TranslationRequest param) {
return null;
}
@Override
public PageResult<Translation> findPage(TranslationRequest param) {
return null;
}
@Override
public List<TranslationDict> getAllTranslationDict() {
return null;
}
}

View File

@ -0,0 +1 @@
多语言模块的sdk

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>kernel-d-i18n</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>i18n-sdk</artifactId>
<packaging>jar</packaging>
<dependencies>
<!--多语言的api-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>i18n-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,49 @@
package cn.stylefeng.roses.kernel.i18n;
import cn.stylefeng.roses.kernel.i18n.api.TranslationApi;
import cn.stylefeng.roses.kernel.i18n.api.enums.TranslationEnum;
import cn.stylefeng.roses.kernel.i18n.api.pojo.TranslationDict;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:08
*/
public class TranslationContainer implements TranslationApi {
/**
*
* <p>
* keyvaluekey valuekey
*/
private static final Map<TranslationEnum, Map<String, String>> TRAN_DICT_CONTAINER = new ConcurrentHashMap<>();
@Override
public void init(List<TranslationDict> translationDict) {
// 根据语种数量创建多个语种的翻译Map
for (TranslationEnum type : TranslationEnum.values()) {
HashMap<String, String> typeMap = new HashMap<>();
TRAN_DICT_CONTAINER.put(type, typeMap);
}
// 整理数据库中的字典
for (TranslationDict translationItem : translationDict) {
TranslationEnum translationLanguages = translationItem.getTranslationLanguages();
TRAN_DICT_CONTAINER.get(translationLanguages).put(translationItem.getTranCode(), translationItem.getTranValue());
}
}
@Override
public Map<String, String> getTranslationDictByLanguage(TranslationEnum translationLanguages) {
return TRAN_DICT_CONTAINER.get(translationLanguages);
}
}

View File

@ -0,0 +1,38 @@
package cn.stylefeng.roses.kernel.i18n.listener;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.i18n.api.TranslationApi;
import cn.stylefeng.roses.kernel.i18n.api.TranslationPersistenceApi;
import cn.stylefeng.roses.kernel.i18n.api.pojo.TranslationDict;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationContextInitializedEvent;
import org.springframework.context.ApplicationListener;
import java.util.List;
/**
*
* <p>
* springconstants
*
* @author fengshuonan
* @date 2021/1/24 19:36
*/
@Slf4j
public class TranslationDictInitListener implements ApplicationListener<ApplicationContextInitializedEvent> {
@Override
public void onApplicationEvent(ApplicationContextInitializedEvent applicationContextInitializedEvent) {
TranslationPersistenceApi tanTranslationPersistenceApi = SpringUtil.getBean(TranslationPersistenceApi.class);
TranslationApi translationApi = SpringUtil.getBean(TranslationApi.class);
// 从数据库读取字典
List<TranslationDict> allTranslationDict = tanTranslationPersistenceApi.getAllTranslationDict();
if (allTranslationDict != null) {
translationApi.init(allTranslationDict);
log.info("初始化所有的翻译字典" + allTranslationDict.size() + "条!");
}
}
}

View File

@ -0,0 +1 @@
多语言模块的spring boot自动配置

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>kernel-d-i18n</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>i18n-spring-boot-starter</artifactId>
<packaging>jar</packaging>
<dependencies>
<!--多语言的sdk-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>i18n-sdk</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,14 @@
package cn.stylefeng.roses.kernel.i18n.starter;
import org.springframework.context.annotation.Configuration;
/**
*
*
* @author fengshuonan
* @date 2021/1/24 16:42
*/
@Configuration
public class GunsTranslationAutoConfiguration {
}

View File

@ -0,0 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.stylefeng.roses.kernel.i18n.starter.GunsTranslationAutoConfiguration
org.springframework.context.ApplicationListener=\
cn.stylefeng.roses.kernel.i18n.listener.TranslationDictInitListener

36
kernel-d-i18n/pom.xml Normal file
View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>roses-kernel</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>kernel-d-i18n</artifactId>
<packaging>pom</packaging>
<modules>
<module>i18n-api</module>
<module>i18n-business</module>
<module>i18n-sdk</module>
<module>i18n-spring-boot-starter</module>
</modules>
<dependencies>
<!-- 开发规则 -->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>kernel-a-rule</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>

View File

@ -49,6 +49,9 @@
<!--文件操作模块-->
<module>kernel-d-file</module>
<!--文件操作模块-->
<module>kernel-d-i18n</module>
<!--sms模块-->
<module>kernel-d-sms</module>