|
@@ -2,17 +2,32 @@ package com.xy.service;
|
|
|
|
|
|
import com.xy.annotate.RestMappingController;
|
|
|
import com.xy.dto.TranslateDto;
|
|
|
+import com.xy.entity.SysDict;
|
|
|
import com.xy.translate.baidu.TransApi;
|
|
|
import com.xy.utils.R;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.core.io.InputStreamResource;
|
|
|
+import org.springframework.core.io.Resource;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.StandardOpenOption;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
/**
|
|
|
* 字典翻译服务
|
|
|
*/
|
|
@@ -35,16 +50,40 @@ public class SysDictTranslateService {
|
|
|
}
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * 全量翻译
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- @PostMapping("/all")
|
|
|
- @ApiOperation("全量翻译-已翻译的忽略")
|
|
|
- public R<?> translateAll() {
|
|
|
- sysDictService.translateAllDict();
|
|
|
- return R.ok();
|
|
|
+ @PostMapping("/exportTranslateTxt")
|
|
|
+ @ApiOperation("导出需要翻译的字典到txt文件")
|
|
|
+ public ResponseEntity<Resource> exportTranslateTxt() throws IOException {
|
|
|
+ List<SysDict> list = sysDictService.list();
|
|
|
+ if (list == null || list.isEmpty()) {
|
|
|
+ return ResponseEntity.noContent().build();
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> msgList = list.stream()
|
|
|
+ .map(SysDict::getMsg)
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 对 msgList 进行处理,每一行开头拼接 "D_"
|
|
|
+ List<String> processedMsgList = msgList.stream()
|
|
|
+ .map(msg -> "D_" + msg)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 创建临时文件
|
|
|
+ try {
|
|
|
+ Path tempFile = Files.createTempFile("dict", ".txt");
|
|
|
+ Files.write(tempFile, processedMsgList, StandardCharsets.UTF_8, StandardOpenOption.WRITE);
|
|
|
+
|
|
|
+ InputStreamResource resource = new InputStreamResource(Files.newInputStream(tempFile.toFile().toPath()));
|
|
|
+
|
|
|
+ return ResponseEntity.ok()
|
|
|
+ .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=dict.txt")
|
|
|
+ .contentType(MediaType.TEXT_PLAIN)
|
|
|
+ .contentLength(tempFile.toFile().length())
|
|
|
+ .body(resource);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|