feat: 多语言下载

This commit is contained in:
bunny 2025-04-08 16:17:36 +08:00
parent 0a22864446
commit b63f8ca303
9 changed files with 177 additions and 40 deletions

View File

@ -12,11 +12,11 @@
**介绍视频视频** **介绍视频视频**
[环境搭建](https://www.bilibili.com/video/BV17odHY6E3S/?spm_id_from=333.1387.homepage.video_card.click&vd_source=d42b5b664efb958be39eef8ee1196a7e) - [环境搭建](https://www.bilibili.com/video/BV17odHY6E3S/?spm_id_from=333.1387.homepage.video_card.click&vd_source=d42b5b664efb958be39eef8ee1196a7e)
[运行项目](https://www.bilibili.com/video/BV1qodHYzErA/?spm_id_from=333.1387.homepage.video_card.click&vd_source=d42b5b664efb958be39eef8ee1196a7e) - [运行项目](https://www.bilibili.com/video/BV1qodHYzErA/?spm_id_from=333.1387.homepage.video_card.click&vd_source=d42b5b664efb958be39eef8ee1196a7e)
[前端部署](https://www.bilibili.com/video/BV1BddHYgEPq/?spm_id_from=333.1387.homepage.video_card.click&vd_source=d42b5b664efb958be39eef8ee1196a7e) - [前端部署](https://www.bilibili.com/video/BV1BddHYgEPq/?spm_id_from=333.1387.homepage.video_card.click&vd_source=d42b5b664efb958be39eef8ee1196a7e)
[后端部署](https://www.bilibili.com/video/BV1BddHYgEFt/?spm_id_from=333.1387.homepage.video_card.click&vd_source=d42b5b664efb958be39eef8ee1196a7e) - [后端部署](https://www.bilibili.com/video/BV1BddHYgEFt/?spm_id_from=333.1387.homepage.video_card.click&vd_source=d42b5b664efb958be39eef8ee1196a7e)
[Bunny v0.0.1 代码生成器](https://www.bilibili.com/video/BV1qddHYgErv/?spm_id_from=333.1387.homepage.video_card.click) - [Bunny v0.0.1 代码生成器](https://www.bilibili.com/video/BV1qddHYgErv/?spm_id_from=333.1387.homepage.video_card.click)
**Github地址** **Github地址**
@ -32,6 +32,8 @@
- 代码生成器前端https://gitee.com/BunnyBoss/generator-code-web - 代码生成器前端https://gitee.com/BunnyBoss/generator-code-web
- 代码生成器后端https://gitee.com/BunnyBoss/generator-code-server - 代码生成器后端https://gitee.com/BunnyBoss/generator-code-server
![DAOCHU](./images/DAOCHU.png)
# 项目预览 # 项目预览
**线上地址** **线上地址**

BIN
images/DAOCHU.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 577 KiB

View File

@ -15,6 +15,7 @@ import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -46,7 +47,7 @@ public class I18nController {
@Operation(summary = "获取管理多语言列", description = "获取管理多语言列") @Operation(summary = "获取管理多语言列", description = "获取管理多语言列")
@GetMapping("getI18nList/{page}/{limit}") @GetMapping("getI18nList/{page}/{limit}")
public Mono<Result<PageResult<I18nVo>>> getI18nList( public Result<PageResult<I18nVo>> getI18nList(
@Parameter(name = "page", description = "当前页", required = true) @Parameter(name = "page", description = "当前页", required = true)
@PathVariable("page") Integer page, @PathVariable("page") Integer page,
@Parameter(name = "limit", description = "每页记录数", required = true) @Parameter(name = "limit", description = "每页记录数", required = true)
@ -54,27 +55,33 @@ public class I18nController {
I18nDto dto) { I18nDto dto) {
Page<I18n> pageParams = new Page<>(page, limit); Page<I18n> pageParams = new Page<>(page, limit);
PageResult<I18nVo> vo = i18nService.getI18nList(pageParams, dto); PageResult<I18nVo> vo = i18nService.getI18nList(pageParams, dto);
return Mono.just(Result.success(vo)); return Result.success(vo);
}
@Operation(summary = "下载多语言配置", description = "下载多语言配置")
@GetMapping("downloadI18n")
public ResponseEntity<byte[]> downloadI18n() {
return i18nService.downloadI18n();
} }
@Operation(summary = "添加多语言", description = "添加多语言") @Operation(summary = "添加多语言", description = "添加多语言")
@PostMapping("addI18n") @PostMapping("addI18n")
public Mono<Result<String>> addI18n(@Valid @RequestBody I18nAddDto dto) { public Result<Object> addI18n(@Valid @RequestBody I18nAddDto dto) {
i18nService.addI18n(dto); i18nService.addI18n(dto);
return Mono.just(Result.success(ResultCodeEnum.ADD_SUCCESS)); return Result.success(ResultCodeEnum.ADD_SUCCESS);
} }
@Operation(summary = "更新多语言", description = "更新多语言") @Operation(summary = "更新多语言", description = "更新多语言")
@PutMapping("updateI18n") @PutMapping("updateI18n")
public Mono<Result<String>> updateI18n(@Valid @RequestBody I18nUpdateDto dto) { public Result<Object> updateI18n(@Valid @RequestBody I18nUpdateDto dto) {
i18nService.updateI18n(dto); i18nService.updateI18n(dto);
return Mono.just(Result.success(ResultCodeEnum.UPDATE_SUCCESS)); return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
} }
@Operation(summary = "删除多语言", description = "删除多语言") @Operation(summary = "删除多语言", description = "删除多语言")
@DeleteMapping("deleteI18n") @DeleteMapping("deleteI18n")
public Mono<Result<String>> deleteI18n(@RequestBody List<Long> ids) { public Result<Object> deleteI18n(@RequestBody List<Long> ids) {
i18nService.deleteI18n(ids); i18nService.deleteI18n(ids);
return Mono.just(Result.success(ResultCodeEnum.DELETE_SUCCESS)); return Result.success(ResultCodeEnum.DELETE_SUCCESS);
} }
} }

View File

@ -12,7 +12,6 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
import java.util.List; import java.util.List;
@ -34,29 +33,29 @@ public class I18nTypeController {
@Operation(summary = "获取多语言类型", description = "获取多语言类型") @Operation(summary = "获取多语言类型", description = "获取多语言类型")
@GetMapping("/noAuth/getI18nTypeList") @GetMapping("/noAuth/getI18nTypeList")
public Mono<Result<List<I18nTypeVo>>> getI18nTypeList(I18nTypeDto dto) { public Result<List<I18nTypeVo>> getI18nTypeList(I18nTypeDto dto) {
List<I18nTypeVo> voList = i18nTypeService.getI18nTypeList(dto); List<I18nTypeVo> voList = i18nTypeService.getI18nTypeList(dto);
return Mono.just(Result.success(voList)); return Result.success(voList);
} }
@Operation(summary = "添加多语言类型", description = "添加多语言类型") @Operation(summary = "添加多语言类型", description = "添加多语言类型")
@PostMapping("addI18nType") @PostMapping("addI18nType")
public Mono<Result<String>> addI18nType(@Valid @RequestBody I18nTypeAddDto dto) { public Result<Object> addI18nType(@Valid @RequestBody I18nTypeAddDto dto) {
i18nTypeService.addI18nType(dto); i18nTypeService.addI18nType(dto);
return Mono.just(Result.success(ResultCodeEnum.ADD_SUCCESS)); return Result.success(ResultCodeEnum.ADD_SUCCESS);
} }
@Operation(summary = "更新多语言类型", description = "更新多语言类型") @Operation(summary = "更新多语言类型", description = "更新多语言类型")
@PutMapping("updateI18nType") @PutMapping("updateI18nType")
public Mono<Result<String>> updateI18nType(@Valid @RequestBody I18nTypeUpdateDto dto) { public Result<Object> updateI18nType(@Valid @RequestBody I18nTypeUpdateDto dto) {
i18nTypeService.updateI18nType(dto); i18nTypeService.updateI18nType(dto);
return Mono.just(Result.success(ResultCodeEnum.UPDATE_SUCCESS)); return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
} }
@Operation(summary = "删除多语言类型", description = "删除多语言类型") @Operation(summary = "删除多语言类型", description = "删除多语言类型")
@DeleteMapping("deleteI18nType") @DeleteMapping("deleteI18nType")
public Mono<Result<String>> deleteI18nType(@RequestBody List<Long> ids) { public Result<Object> deleteI18nType(@RequestBody List<Long> ids) {
i18nTypeService.deleteI18nType(ids); i18nTypeService.deleteI18nType(ids);
return Mono.just(Result.success(ResultCodeEnum.DELETE_SUCCESS)); return Result.success(ResultCodeEnum.DELETE_SUCCESS);
} }
} }

View File

@ -9,6 +9,7 @@ import cn.bunny.dao.vo.result.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -57,4 +58,11 @@ public interface I18nService extends IService<I18n> {
* @param ids 删除id列表 * @param ids 删除id列表
*/ */
void deleteI18n(List<Long> ids); void deleteI18n(List<Long> ids);
}
/**
* 下载多语言配置
*
* @return 文件内容
*/
ResponseEntity<byte[]> downloadI18n();
}

View File

@ -12,23 +12,31 @@ import cn.bunny.services.exception.AuthCustomerException;
import cn.bunny.services.mapper.configuration.I18nMapper; import cn.bunny.services.mapper.configuration.I18nMapper;
import cn.bunny.services.mapper.configuration.I18nTypeMapper; import cn.bunny.services.mapper.configuration.I18nTypeMapper;
import cn.bunny.services.service.configuration.I18nService; import cn.bunny.services.service.configuration.I18nService;
import cn.bunny.services.utils.system.I18nUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.stream.Collectors; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/** /**
* <p> * <p>
@ -42,8 +50,11 @@ import java.util.stream.Collectors;
@Transactional @Transactional
public class I18nServiceImpl extends ServiceImpl<I18nMapper, I18n> implements I18nService { public class I18nServiceImpl extends ServiceImpl<I18nMapper, I18n> implements I18nService {
@Autowired private final I18nTypeMapper i18nTypeMapper;
private I18nTypeMapper i18nTypeMapper;
public I18nServiceImpl(I18nTypeMapper i18nTypeMapper) {
this.i18nTypeMapper = i18nTypeMapper;
}
/** /**
* * 获取多语言内容 * * 获取多语言内容
@ -57,18 +68,13 @@ public class I18nServiceImpl extends ServiceImpl<I18nMapper, I18n> implements I1
I18nType i18nType = i18nTypeMapper.selectOne(Wrappers.<I18nType>lambdaQuery().eq(I18nType::getIsDefault, true)); I18nType i18nType = i18nTypeMapper.selectOne(Wrappers.<I18nType>lambdaQuery().eq(I18nType::getIsDefault, true));
List<I18n> i18nList = list(); List<I18n> i18nList = list();
// 整理集合 HashMap<String, Object> hashMap = I18nUtil.getMap(i18nList);
Map<String, Map<String, String>> map = i18nList.stream()
.collect(Collectors.groupingBy(
I18n::getTypeName,
Collectors.toMap(I18n::getKeyName, I18n::getTranslation)));
// 返回集合
HashMap<String, Object> hashMap = new HashMap<>(map);
hashMap.put("local", Objects.requireNonNull(i18nType.getTypeName(), "zh")); hashMap.put("local", Objects.requireNonNull(i18nType.getTypeName(), "zh"));
return hashMap; return hashMap;
} }
/** /**
* * 获取管理多语言列表 * * 获取管理多语言列表
* *
@ -140,4 +146,47 @@ public class I18nServiceImpl extends ServiceImpl<I18nMapper, I18n> implements I1
baseMapper.deleteBatchIdsWithPhysics(ids); baseMapper.deleteBatchIdsWithPhysics(ids);
} }
/**
* 下载多语言配置
*
* @return 文件内容
*/
@Override
public ResponseEntity<byte[]> downloadI18n() {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
// 查找默认语言内容
List<I18n> i18nList = list();
HashMap<String, Object> hashMap = I18nUtil.getMap(i18nList);
hashMap.forEach((k, v) -> {
try {
ZipEntry zipEntry = new ZipEntry(k + ".json");
zipOutputStream.putNextEntry(zipEntry);
zipOutputStream.write(JSON.toJSONString(v).getBytes(StandardCharsets.UTF_8));
zipOutputStream.closeEntry();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
try {
zipOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
// 设置响应头
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment; filename=i18n.zip");
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
return new ResponseEntity<>(byteArrayInputStream.readAllBytes(), headers, HttpStatus.OK);
}
} }

View File

@ -0,0 +1,23 @@
package cn.bunny.services.utils.system;
import cn.bunny.dao.entity.i18n.I18n;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class I18nUtil {
@NotNull
public static HashMap<String, Object> getMap(List<I18n> i18nList) {
// 整理集合
Map<String, Map<String, String>> map = i18nList.stream()
.collect(Collectors.groupingBy(
I18n::getTypeName,
Collectors.toMap(I18n::getKeyName, I18n::getTranslation)));
// 返回集合
return new HashMap<>(map);
}
}

View File

@ -13,26 +13,26 @@ logging:
path: "logs/${spring.application.name}" path: "logs/${spring.application.name}"
name: "logs/${spring.application.name}" name: "logs/${spring.application.name}"
#mybatis-plus: # mybatis-plus:
# configuration: # configuration:
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 查看日志 # log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 查看日志
bunny: bunny:
master: master:
host: localhost host: 192.168.3.137
port: 3306 port: 3306
database: auth_admin database: auth_admin
username: root username: root
password: "123456" password: "123456"
redis: redis:
host: localhost host: 192.168.3.137
port: 6379 port: 6379
database: 0 database: 0
password: "123456" password: "123456"
minio: minio:
endpointUrl: "http://localhost:9000" # 连接地址 endpointUrl: "http://192.168.3.137:9000" # 连接地址
accessKey: bunny # 用户名 accessKey: bunny # 用户名
secretKey: "12345678" # 登录密码 secretKey: "12345678" # 登录密码
bucket-name: auth-admin # 指定哪个桶 bucket-name: auth-admin # 指定哪个桶

View File

@ -0,0 +1,49 @@
package cn.bunny.services.service.configuration.impl;
import cn.bunny.dao.entity.i18n.I18n;
import cn.bunny.services.mapper.configuration.I18nMapper;
import cn.bunny.services.utils.system.I18nUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@SpringBootTest
class I18nServiceImplTest extends ServiceImpl<I18nMapper, I18n> {
@Test
void downloadI18n() {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
// 查找默认语言内容
List<I18n> i18nList = list();
HashMap<String, Object> hashMap = I18nUtil.getMap(i18nList);
hashMap.forEach((k, v) -> {
try {
ZipEntry zipEntry = new ZipEntry(k + ".json");
zipOutputStream.putNextEntry(zipEntry);
zipOutputStream.write(JSON.toJSONString(v).getBytes(StandardCharsets.UTF_8));
zipOutputStream.closeEntry();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
try {
zipOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}