【i18n】新增三个多语言接口

pull/3/head
fengshuonan 2021-01-27 22:30:03 +08:00
parent a71a276c28
commit 29bb26b4f9
6 changed files with 139 additions and 3 deletions

View File

@ -98,8 +98,10 @@ public class LoginUser implements Serializable {
* chineseenglish
* <p>
* languages
* <p>
*
*/
private String tranLanguageCode;
private String tranLanguageCode = "chinese";
public String getWsUrl() {
AtomicReference<String> returnUrl = new AtomicReference<>(StrUtil.EMPTY);

View File

@ -40,7 +40,7 @@ public class TranslationRequest extends BaseRequest {
/**
*
*/
@NotBlank(message = "tranLanguageCode不能为空", groups = {add.class, edit.class})
@NotBlank(message = "tranLanguageCode不能为空", groups = {add.class, edit.class, changeUserLanguage.class})
private String tranLanguageCode;
/**
@ -49,4 +49,10 @@ public class TranslationRequest extends BaseRequest {
@NotBlank(message = "tranValue不能为空", groups = {add.class, edit.class})
private String tranValue;
/**
*
*/
public @interface changeUserLanguage {
}
}

View File

@ -24,6 +24,14 @@
<version>1.0.0</version>
</dependency>
<!--字典的api-->
<!--多语言类型,存在于字典表-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>dict-api</artifactId>
<version>1.0.0</version>
</dependency>
<!--资源api模块-->
<!--用在资源控制器,资源扫描上-->
<dependency>

View File

@ -0,0 +1,86 @@
package cn.stylefeng.roses.kernel.i18n.modular.controller;
import cn.stylefeng.roses.kernel.auth.api.SessionManagerApi;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.dict.api.DictApi;
import cn.stylefeng.roses.kernel.dict.api.constants.DictConstants;
import cn.stylefeng.roses.kernel.i18n.api.context.TranslationContext;
import cn.stylefeng.roses.kernel.i18n.api.pojo.request.TranslationRequest;
import cn.stylefeng.roses.kernel.resource.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.resource.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.resource.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.rule.pojo.dict.SimpleDict;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
*
*
* @author fengshuonan
* @date 2021/1/27 21:59
*/
@RestController
@ApiResource(name = "用户多语言信息控制器")
public class UserTranslationController {
@Resource
private DictApi dictApi;
@Resource
private SessionManagerApi sessionManagerApi;
/**
*
*
* @author fengshuonan
* @date 2021/1/24 19:20
*/
@GetResource(name = "获取所有的多语言类型编码", path = "/i18n/getAllLanguages", requiredPermission = false)
public ResponseData getAllLanguages() {
List<SimpleDict> dictDetailsByDictTypeCode = dictApi.getDictDetailsByDictTypeCode(DictConstants.LANGUAGES_DICT_TYPE_CODE);
return new SuccessResponseData(dictDetailsByDictTypeCode);
}
/**
*
*
* @author fengshuonan
* @date 2021/1/27 22:00
*/
@GetResource(name = "获取当前用户的多语言字典", path = "/i18n/getUserTranslation", requiredPermission = false)
public ResponseData getUserTranslation() {
String tranLanguageCode = LoginContext.me().getLoginUser().getTranLanguageCode();
Map<String, String> translationDictByLanguage = TranslationContext.me().getTranslationDictByLanguage(tranLanguageCode);
return new SuccessResponseData(translationDictByLanguage);
}
/**
*
*
* @author fengshuonan
* @date 2021/1/27 22:04
*/
@PostResource(name = "修改当前用户的多语言配置", path = "/i18n/changeUserTranslation", requiredPermission = false)
public ResponseData changeUserTranslation(@RequestBody @Validated(TranslationRequest.changeUserLanguage.class) TranslationRequest translationRequest) {
String token = LoginContext.me().getToken();
LoginUser loginUser = LoginContext.me().getLoginUser();
// 更新当前用户的多语言配置
loginUser.setTranLanguageCode(translationRequest.getTranLanguageCode());
sessionManagerApi.updateSession(token, loginUser);
return new SuccessResponseData();
}
}

View File

@ -1,5 +1,9 @@
package cn.stylefeng.roses.kernel.dict.api;
import cn.stylefeng.roses.kernel.rule.pojo.dict.SimpleDict;
import java.util.List;
/**
* api便
*
@ -20,11 +24,21 @@ public interface DictApi {
*
*
* @param dictTypeCode
* @param dictCode
* @param dictCode
* @return
* @author liuhanqing
* @date 2021/1/16 23:18
*/
String getDictName(String dictTypeCode, String dictCode);
/**
*
*
* @param dictTypeCode
* @return
* @author fengshuonan
* @date 2021/1/27 22:13
*/
List<SimpleDict> getDictDetailsByDictTypeCode(String dictTypeCode);
}

View File

@ -23,6 +23,7 @@ import cn.stylefeng.roses.kernel.rule.enums.StatusEnum;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.roses.kernel.rule.factory.DefaultTreeBuildFactory;
import cn.stylefeng.roses.kernel.rule.pojo.dict.SimpleDict;
import cn.stylefeng.roses.kernel.rule.pojo.ztree.ZTreeNode;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
@ -333,6 +334,25 @@ public class DictServiceImpl extends ServiceImpl<DictMapper, SysDict> implements
}
}
@Override
public List<SimpleDict> getDictDetailsByDictTypeCode(String dictTypeCode) {
DictRequest dictRequest = new DictRequest();
dictRequest.setDictTypeCode(dictTypeCode);
LambdaQueryWrapper<SysDict> wrapper = createWrapper(dictRequest);
List<SysDict> dictList = this.list(wrapper);
if (dictList.isEmpty()) {
return new ArrayList<>();
}
ArrayList<SimpleDict> simpleDictList = new ArrayList<>();
for (SysDict sysDict : dictList) {
SimpleDict simpleDict = new SimpleDict();
simpleDict.setCode(sysDict.getDictCode());
simpleDict.setName(sysDict.getDictName());
simpleDictList.add(simpleDict);
}
return simpleDictList;
}
/**
* pids
*