mirror of https://gitee.com/stylefeng/roses
【Translation】多语言模块方法调整以及修改字典类型字段
parent
25735b77a9
commit
4632548c9f
|
@ -1,5 +1,6 @@
|
||||||
package cn.stylefeng.roses.kernel.i18n.api.enums;
|
package cn.stylefeng.roses.kernel.i18n.api.enums;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -17,24 +18,24 @@ public enum TranslationEnum {
|
||||||
/**
|
/**
|
||||||
* 中文
|
* 中文
|
||||||
*/
|
*/
|
||||||
CHINESE(1, "中文"),
|
CHINESE("chinese", "中文"),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 英文
|
* 英文
|
||||||
*/
|
*/
|
||||||
ENGLISH(2, "English");
|
ENGLISH("english", "English");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 语种编码
|
* 语种编码
|
||||||
*/
|
*/
|
||||||
private final Integer code;
|
private final String code;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 语种的中文描述
|
* 语种的中文描述
|
||||||
*/
|
*/
|
||||||
private final String description;
|
private final String description;
|
||||||
|
|
||||||
TranslationEnum(Integer code, String description) {
|
TranslationEnum(String code, String description) {
|
||||||
this.code = code;
|
this.code = code;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
@ -59,8 +60,8 @@ public enum TranslationEnum {
|
||||||
* @author fengshuonan
|
* @author fengshuonan
|
||||||
* @date 2019/10/18 10:33
|
* @date 2019/10/18 10:33
|
||||||
*/
|
*/
|
||||||
public static TranslationEnum valueOf(Integer value) {
|
public static TranslationEnum getValue(String value) {
|
||||||
if (value != null) {
|
if (StrUtil.isNotBlank(value)) {
|
||||||
for (TranslationEnum translationLanguages : TranslationEnum.values()) {
|
for (TranslationEnum translationLanguages : TranslationEnum.values()) {
|
||||||
if (translationLanguages.getCode().equals(value)) {
|
if (translationLanguages.getCode().equals(value)) {
|
||||||
return translationLanguages;
|
return translationLanguages;
|
||||||
|
|
|
@ -38,10 +38,10 @@ public class TranslationRequest extends BaseRequest {
|
||||||
private String tranName;
|
private String tranName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 1:中文 2:英语
|
* 语种字典
|
||||||
*/
|
*/
|
||||||
@NotBlank(message = "language不能为空", groups = {add.class, edit.class})
|
@NotBlank(message = "language不能为空", groups = {add.class, edit.class})
|
||||||
private Integer language;
|
private String tranLanguageCode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 翻译的值
|
* 翻译的值
|
||||||
|
|
|
@ -36,7 +36,7 @@ public class TranslationController {
|
||||||
* @date 2021/1/24 19:17
|
* @date 2021/1/24 19:17
|
||||||
*/
|
*/
|
||||||
@PostResource(name = "新增多语言配置", path = "/i18n/add")
|
@PostResource(name = "新增多语言配置", path = "/i18n/add")
|
||||||
public ResponseData addItem(@RequestBody @Validated(TranslationRequest.add.class) TranslationRequest translationRequest) {
|
public ResponseData add(@RequestBody @Validated(TranslationRequest.add.class) TranslationRequest translationRequest) {
|
||||||
this.translationService.add(translationRequest);
|
this.translationService.add(translationRequest);
|
||||||
return new SuccessResponseData();
|
return new SuccessResponseData();
|
||||||
}
|
}
|
||||||
|
@ -48,8 +48,8 @@ public class TranslationController {
|
||||||
* @date 2021/1/24 19:17
|
* @date 2021/1/24 19:17
|
||||||
*/
|
*/
|
||||||
@PostResource(name = "新增多语言配置", path = "/i18n/edit")
|
@PostResource(name = "新增多语言配置", path = "/i18n/edit")
|
||||||
public ResponseData editItem(@RequestBody @Validated(BaseRequest.edit.class) TranslationRequest translationRequest) {
|
public ResponseData edit(@RequestBody @Validated(BaseRequest.edit.class) TranslationRequest translationRequest) {
|
||||||
this.translationService.update(translationRequest);
|
this.translationService.edit(translationRequest);
|
||||||
return new SuccessResponseData();
|
return new SuccessResponseData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ public class TranslationController {
|
||||||
*/
|
*/
|
||||||
@PostResource(name = "新增多语言配置", path = "/i18n/delete")
|
@PostResource(name = "新增多语言配置", path = "/i18n/delete")
|
||||||
public ResponseData delete(@RequestBody @Validated(BaseRequest.delete.class) TranslationRequest translationRequest) {
|
public ResponseData delete(@RequestBody @Validated(BaseRequest.delete.class) TranslationRequest translationRequest) {
|
||||||
this.translationService.delete(translationRequest);
|
this.translationService.del(translationRequest);
|
||||||
return new SuccessResponseData();
|
return new SuccessResponseData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ public class TranslationController {
|
||||||
*/
|
*/
|
||||||
@GetResource(name = "新增多语言配置", path = "/i18n/detail")
|
@GetResource(name = "新增多语言配置", path = "/i18n/detail")
|
||||||
public ResponseData detail(@Validated(BaseRequest.detail.class) TranslationRequest translationRequest) {
|
public ResponseData detail(@Validated(BaseRequest.detail.class) TranslationRequest translationRequest) {
|
||||||
Translation detail = this.translationService.findDetail(translationRequest);
|
Translation detail = this.translationService.detail(translationRequest);
|
||||||
return new SuccessResponseData(detail);
|
return new SuccessResponseData(detail);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,8 +84,8 @@ public class TranslationController {
|
||||||
* @date 2021/1/24 19:20
|
* @date 2021/1/24 19:20
|
||||||
*/
|
*/
|
||||||
@GetResource(name = "新增多语言配置", path = "/i18n/page")
|
@GetResource(name = "新增多语言配置", path = "/i18n/page")
|
||||||
public ResponseData list(TranslationRequest translationRequest) {
|
public ResponseData page(TranslationRequest translationRequest) {
|
||||||
PageResult<Translation> page = this.translationService.findPage(translationRequest);
|
PageResult<Translation> page = this.translationService.getPage(translationRequest);
|
||||||
return new SuccessResponseData(page);
|
return new SuccessResponseData(page);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,10 +40,10 @@ public class Translation extends BaseEntity {
|
||||||
private String tranName;
|
private String tranName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 1:中文 2:英语
|
* 语种字典
|
||||||
*/
|
*/
|
||||||
@TableField("language")
|
@TableField("tran_language_code")
|
||||||
private Integer language;
|
private String tranLanguageCode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 翻译的值
|
* 翻译的值
|
||||||
|
|
|
@ -6,6 +6,8 @@ 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.entity.Translation;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 多语言表 服务类
|
* 多语言表 服务类
|
||||||
*
|
*
|
||||||
|
@ -15,43 +17,67 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
public interface TranslationService extends IService<Translation>, TranslationPersistenceApi {
|
public interface TranslationService extends IService<Translation>, TranslationPersistenceApi {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增翻译项
|
* 新增
|
||||||
*
|
*
|
||||||
* @author fengshuonan
|
* @param translationRequest 参数对象
|
||||||
* @date 2021/1/24 19:27
|
* @author chenjinlong
|
||||||
|
* @date 2021/1/26 12:52
|
||||||
*/
|
*/
|
||||||
void add(TranslationRequest param);
|
void add(TranslationRequest translationRequest);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 更新翻译项
|
* 删除
|
||||||
*
|
*
|
||||||
* @author fengshuonan
|
* @param translationRequest 参数对象
|
||||||
* @date 2021/1/24 19:27
|
* @author chenjinlong
|
||||||
|
* @date 2021/1/26 12:52
|
||||||
*/
|
*/
|
||||||
void update(TranslationRequest param);
|
void del(TranslationRequest translationRequest);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除翻译项
|
* 修改
|
||||||
*
|
*
|
||||||
* @author fengshuonan
|
* @param translationRequest 参数对象
|
||||||
* @date 2021/1/24 19:27
|
* @author chenjinlong
|
||||||
|
* @date 2021/1/26 12:52
|
||||||
*/
|
*/
|
||||||
void delete(TranslationRequest param);
|
void edit(TranslationRequest translationRequest);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询详情
|
* 查询-详情-根据主键id
|
||||||
*
|
*
|
||||||
* @author fengshuonan
|
* @param translationRequest 参数对象
|
||||||
* @date 2021/1/24 19:28
|
* @author chenjinlong
|
||||||
|
* @date 2021/1/26 12:52
|
||||||
*/
|
*/
|
||||||
Translation findDetail(TranslationRequest param);
|
Translation detail(TranslationRequest translationRequest);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询列表
|
* 查询-详情-按实体对象
|
||||||
*
|
*
|
||||||
* @author fengshuonan
|
* @param translationRequest 参数对象
|
||||||
* @date 2021/1/24 19:28
|
* @author chenjinlong
|
||||||
|
* @date 2021/1/26 12:52
|
||||||
*/
|
*/
|
||||||
PageResult<Translation> findPage(TranslationRequest param);
|
Translation detailBy(TranslationRequest translationRequest);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询-列表-按实体对象
|
||||||
|
*
|
||||||
|
* @param translationRequest 参数对象
|
||||||
|
* @author chenjinlong
|
||||||
|
* @date 2021/1/26 12:52
|
||||||
|
*/
|
||||||
|
List<Translation> listBy(TranslationRequest translationRequest);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询-列表-分页-按实体对象
|
||||||
|
*
|
||||||
|
* @param translationRequest 参数对象
|
||||||
|
* @author chenjinlong
|
||||||
|
* @date 2021/1/26 12:52
|
||||||
|
*/
|
||||||
|
PageResult<Translation> getPage(TranslationRequest translationRequest);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,61 +33,55 @@ import java.util.List;
|
||||||
public class TranslationServiceImpl extends ServiceImpl<TranslationMapper, Translation> implements TranslationService {
|
public class TranslationServiceImpl extends ServiceImpl<TranslationMapper, Translation> implements TranslationService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(TranslationRequest param) {
|
public void add(TranslationRequest translationRequest) {
|
||||||
|
|
||||||
// 1.构造实体
|
|
||||||
Translation translation = new Translation();
|
Translation translation = new Translation();
|
||||||
BeanUtil.copyProperties(param, translation);
|
BeanUtil.copyProperties(translationRequest, translation);
|
||||||
|
|
||||||
// 2.保存到库中
|
|
||||||
this.save(translation);
|
this.save(translation);
|
||||||
|
// 更新对应常量
|
||||||
// 3.添加对应context
|
this.saveContext(translation);
|
||||||
TranslationEnum translationEnum = TranslationEnum.valueOf(param.getLanguage());
|
|
||||||
TranslationDict translationDict = TranslationDictFactory.createTranslationDict(translationEnum, translation);
|
|
||||||
TranslationContext.me().addTranslationDict(translationDict);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(TranslationRequest param) {
|
public void edit(TranslationRequest translationRequest) {
|
||||||
|
Translation translation = this.queryTranslation(translationRequest);
|
||||||
// 1.根据id获取信息
|
BeanUtil.copyProperties(translationRequest, translation);
|
||||||
Translation translation = this.queryTranslation(param);
|
|
||||||
|
|
||||||
// 2.请求参数转化为实体
|
|
||||||
BeanUtil.copyProperties(param, translation);
|
|
||||||
|
|
||||||
// 3.更新记录
|
|
||||||
this.updateById(translation);
|
this.updateById(translation);
|
||||||
|
// 更新对应常量
|
||||||
// 4.更新对应常量context
|
this.saveContext(translation);
|
||||||
TranslationEnum translationEnum = TranslationEnum.valueOf(param.getLanguage());
|
|
||||||
TranslationDict translationDict = TranslationDictFactory.createTranslationDict(translationEnum, translation);
|
|
||||||
TranslationContext.me().addTranslationDict(translationDict);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(TranslationRequest param) {
|
public void del(TranslationRequest translationRequest) {
|
||||||
|
Translation translation = this.queryTranslation(translationRequest);
|
||||||
// 1.根据id获取实体
|
this.removeById(translationRequest.getTranId());
|
||||||
Translation translation = this.queryTranslation(param);
|
// 更新对应常量
|
||||||
|
this.saveContext(translation);
|
||||||
// 2.删除该记录
|
|
||||||
this.removeById(param.getTranId());
|
|
||||||
|
|
||||||
// 3.删除对应context
|
|
||||||
TranslationEnum translationEnum = TranslationEnum.valueOf(translation.getLanguage());
|
|
||||||
TranslationContext.me().deleteTranslationDict(translationEnum, translation.getTranCode());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Translation findDetail(TranslationRequest param) {
|
public Translation detail(TranslationRequest translationRequest) {
|
||||||
return queryTranslation(param);
|
return this.queryTranslation(translationRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PageResult<Translation> findPage(TranslationRequest param) {
|
public Translation detailBy(TranslationRequest translationRequest) {
|
||||||
LambdaQueryWrapper<Translation> wrapper = createWrapper(param);
|
List<Translation> list = this.listBy(translationRequest);
|
||||||
|
if (list.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return list.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Translation> listBy(TranslationRequest translationRequest) {
|
||||||
|
LambdaQueryWrapper<Translation> queryWrapper = this.createWrapper(translationRequest);
|
||||||
|
return this.list(queryWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PageResult<Translation> getPage(TranslationRequest translationRequest) {
|
||||||
|
LambdaQueryWrapper<Translation> wrapper = createWrapper(translationRequest);
|
||||||
Page<Translation> page = this.page(PageFactory.defaultPage(), wrapper);
|
Page<Translation> page = this.page(PageFactory.defaultPage(), wrapper);
|
||||||
return PageResultFactory.createPageResult(page);
|
return PageResultFactory.createPageResult(page);
|
||||||
}
|
}
|
||||||
|
@ -97,7 +91,7 @@ public class TranslationServiceImpl extends ServiceImpl<TranslationMapper, Trans
|
||||||
List<Translation> list = this.list();
|
List<Translation> list = this.list();
|
||||||
ArrayList<TranslationDict> translationDictList = new ArrayList<>();
|
ArrayList<TranslationDict> translationDictList = new ArrayList<>();
|
||||||
for (Translation translation : list) {
|
for (Translation translation : list) {
|
||||||
TranslationEnum translationEnum = TranslationEnum.valueOf(translation.getLanguage());
|
TranslationEnum translationEnum = TranslationEnum.getValue(translation.getTranLanguageCode());
|
||||||
TranslationDict translationDict = TranslationDictFactory.createTranslationDict(translationEnum, translation);
|
TranslationDict translationDict = TranslationDictFactory.createTranslationDict(translationEnum, translation);
|
||||||
translationDictList.add(translationDict);
|
translationDictList.add(translationDict);
|
||||||
}
|
}
|
||||||
|
@ -105,45 +99,55 @@ public class TranslationServiceImpl extends ServiceImpl<TranslationMapper, Trans
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取字典项的对象
|
* 根据主键id获取对象
|
||||||
*
|
*
|
||||||
* @author fengshuonan
|
* @param
|
||||||
* @date 2021/1/24 21:54
|
* @return
|
||||||
|
* @author chenjinlong
|
||||||
|
* @date 2021/1/26 13:28
|
||||||
*/
|
*/
|
||||||
private Translation queryTranslation(TranslationRequest param) {
|
private Translation queryTranslation(TranslationRequest translationRequest) {
|
||||||
Translation translation = this.getById(param.getTranId());
|
Translation translation = this.getById(translationRequest.getTranId());
|
||||||
if (ObjectUtil.isEmpty(translation)) {
|
if (ObjectUtil.isEmpty(translation)) {
|
||||||
throw new TranslationException(TranslationExceptionEnum.NOT_EXISTED, param.getTranId());
|
throw new TranslationException(TranslationExceptionEnum.NOT_EXISTED, translationRequest.getTranId());
|
||||||
}
|
}
|
||||||
return translation;
|
return translation;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建多语言的wrapper
|
* 实体构建queryWrapper
|
||||||
*
|
*
|
||||||
* @author fengshuonan
|
* @author fengshuonan
|
||||||
* @date 2021/1/24 22:03
|
* @date 2021/1/24 22:03
|
||||||
*/
|
*/
|
||||||
private LambdaQueryWrapper<Translation> createWrapper(TranslationRequest param) {
|
private LambdaQueryWrapper<Translation> createWrapper(TranslationRequest translationRequest) {
|
||||||
LambdaQueryWrapper<Translation> queryWrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<Translation> queryWrapper = new LambdaQueryWrapper<>();
|
||||||
|
|
||||||
if (ObjectUtil.isNotNull(param)) {
|
String tranCode = translationRequest.getTranCode();
|
||||||
|
String tranName = translationRequest.getTranName();
|
||||||
// 如果编码不为空,则带上名称搜素搜条件
|
String tranLanguageCode = translationRequest.getTranLanguageCode();
|
||||||
if (ObjectUtil.isNotEmpty(param.getTranCode())) {
|
// SQL条件拼接
|
||||||
queryWrapper.like(Translation::getTranCode, param.getTranCode());
|
queryWrapper.like(ObjectUtil.isNotEmpty(tranCode), Translation::getTranCode, tranCode);
|
||||||
}
|
queryWrapper.like(ObjectUtil.isNotEmpty(tranName), Translation::getTranName, tranName);
|
||||||
|
queryWrapper.eq(ObjectUtil.isNotEmpty(tranLanguageCode), Translation::getTranLanguageCode, tranLanguageCode);
|
||||||
// 如果翻译名称不为空,则带上翻译名称
|
// 排序
|
||||||
if (ObjectUtil.isNotEmpty(param.getTranName())) {
|
queryWrapper.orderByDesc(Translation::getTranCode);
|
||||||
queryWrapper.like(Translation::getTranName, param.getTranName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 按时间倒序
|
|
||||||
queryWrapper.orderByDesc(Translation::getCreateTime);
|
|
||||||
|
|
||||||
return queryWrapper;
|
return queryWrapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新对应常量
|
||||||
|
*
|
||||||
|
* @param translation
|
||||||
|
* @author chenjinlong
|
||||||
|
* @date 2021/1/26 13:45
|
||||||
|
*/
|
||||||
|
private void saveContext(Translation translation) {
|
||||||
|
TranslationEnum translationEnum = TranslationEnum.getValue(translation.getTranLanguageCode());
|
||||||
|
TranslationDict translationDict = TranslationDictFactory.createTranslationDict(translationEnum, translation);
|
||||||
|
TranslationContext.me().addTranslationDict(translationDict);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue