From 76d639abec45b61ad7d7f8d2bda95472d0533dbb Mon Sep 17 00:00:00 2001 From: sige Date: Tue, 2 Jan 2024 19:03:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0i18n=E5=92=8C=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dreamworks/boditech/BoditechApplication.java | 4 +++- .../boditech/configuration/LocalConfig.java | 15 ++++++++++++ .../boditech/controller/BaseController.java | 1 + .../boditech/controller/entity/ApiResponse.java | 5 ++++ .../dreamworks/boditech/driver/task/Executor.java | 10 ++++++-- .../driver/task/TaskTestTubeRackPrepare.java | 4 +++- .../boditech/service/AccountService.java | 4 +++- .../com/dreamworks/boditech/utils/AppError.java | 8 +++++++ .../boditech/utils/AppExceptionHandler.java | 28 ++++++++++++++++++++++ .../boditech/utils/AppRuntimeException.java | 16 +++++++++++++ .../java/com/dreamworks/boditech/utils/I18n.java | 22 +++++++++++++++++ .../boditech/utils/MyExceptionHandler.java | 17 ------------- src/main/resources/application.yml | 5 +++- src/main/resources/static/i18n/messages.properties | 0 .../static/i18n/messages_en_US.properties | 1 + .../static/i18n/messages_ko_KR.properties | 1 + .../static/i18n/messages_zh_CN.properties | 1 + 17 files changed, 119 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/dreamworks/boditech/configuration/LocalConfig.java create mode 100644 src/main/java/com/dreamworks/boditech/utils/AppError.java create mode 100644 src/main/java/com/dreamworks/boditech/utils/AppExceptionHandler.java create mode 100644 src/main/java/com/dreamworks/boditech/utils/AppRuntimeException.java create mode 100644 src/main/java/com/dreamworks/boditech/utils/I18n.java delete mode 100644 src/main/java/com/dreamworks/boditech/utils/MyExceptionHandler.java create mode 100644 src/main/resources/static/i18n/messages.properties create mode 100644 src/main/resources/static/i18n/messages_en_US.properties create mode 100644 src/main/resources/static/i18n/messages_ko_KR.properties create mode 100644 src/main/resources/static/i18n/messages_zh_CN.properties diff --git a/src/main/java/com/dreamworks/boditech/BoditechApplication.java b/src/main/java/com/dreamworks/boditech/BoditechApplication.java index 58777fd..2bc991c 100644 --- a/src/main/java/com/dreamworks/boditech/BoditechApplication.java +++ b/src/main/java/com/dreamworks/boditech/BoditechApplication.java @@ -2,8 +2,10 @@ package com.dreamworks.boditech; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; +import org.springframework.web.servlet.LocaleResolver; +import org.springframework.web.servlet.i18n.SessionLocaleResolver; import org.springframework.web.socket.server.standard.ServerEndpointExporter; - +import java.util.Locale; @SpringBootApplication public class BoditechApplication { public static void main(String[] args) { diff --git a/src/main/java/com/dreamworks/boditech/configuration/LocalConfig.java b/src/main/java/com/dreamworks/boditech/configuration/LocalConfig.java new file mode 100644 index 0000000..4047901 --- /dev/null +++ b/src/main/java/com/dreamworks/boditech/configuration/LocalConfig.java @@ -0,0 +1,15 @@ +package com.dreamworks.boditech.configuration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.LocaleResolver; +import org.springframework.web.servlet.i18n.SessionLocaleResolver; +import java.util.Locale; +@Configuration +public class LocalConfig { + @Bean + public LocaleResolver localeResolver() { + SessionLocaleResolver localeResolver = new SessionLocaleResolver(); + localeResolver.setDefaultLocale(Locale.CHINA); + return localeResolver; + } +} diff --git a/src/main/java/com/dreamworks/boditech/controller/BaseController.java b/src/main/java/com/dreamworks/boditech/controller/BaseController.java index aaf657b..0305995 100644 --- a/src/main/java/com/dreamworks/boditech/controller/BaseController.java +++ b/src/main/java/com/dreamworks/boditech/controller/BaseController.java @@ -6,6 +6,7 @@ public class BaseController { response.success = true; response.message = "OK"; response.data = data; + response.code = "OK"; return response; } diff --git a/src/main/java/com/dreamworks/boditech/controller/entity/ApiResponse.java b/src/main/java/com/dreamworks/boditech/controller/entity/ApiResponse.java index f8404e1..6d8b1d8 100644 --- a/src/main/java/com/dreamworks/boditech/controller/entity/ApiResponse.java +++ b/src/main/java/com/dreamworks/boditech/controller/entity/ApiResponse.java @@ -1,6 +1,11 @@ package com.dreamworks.boditech.controller.entity; public class ApiResponse { + // success or not public boolean success ; + // error code + public String code; + // message public String message; + // data public Object data; } diff --git a/src/main/java/com/dreamworks/boditech/driver/task/Executor.java b/src/main/java/com/dreamworks/boditech/driver/task/Executor.java index 0a0481c..4faa6b7 100644 --- a/src/main/java/com/dreamworks/boditech/driver/task/Executor.java +++ b/src/main/java/com/dreamworks/boditech/driver/task/Executor.java @@ -1,6 +1,7 @@ package com.dreamworks.boditech.driver.task; import com.dreamworks.boditech.driver.Device; import com.dreamworks.boditech.service.TestService; +import com.dreamworks.boditech.utils.AppRuntimeException; import com.dreamworks.boditech.utils.MyCommon; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -63,8 +64,13 @@ public class Executor implements Runnable { continue; } - task.execute(this); - if (Task.STATUS_FINISHED.equals(task.getStatus())) { + try { + task.execute(this); + if (Task.STATUS_FINISHED.equals(task.getStatus())) { + this.tasks.remove(task); + } + } catch (AppRuntimeException e) { + LOG.error("task execute error", e); this.tasks.remove(task); } } diff --git a/src/main/java/com/dreamworks/boditech/driver/task/TaskTestTubeRackPrepare.java b/src/main/java/com/dreamworks/boditech/driver/task/TaskTestTubeRackPrepare.java index 90151db..ee3a978 100644 --- a/src/main/java/com/dreamworks/boditech/driver/task/TaskTestTubeRackPrepare.java +++ b/src/main/java/com/dreamworks/boditech/driver/task/TaskTestTubeRackPrepare.java @@ -4,6 +4,8 @@ import com.dreamworks.boditech.driver.actuator.*; import com.dreamworks.boditech.entity.MdbTestTubeRackTestTask; import com.dreamworks.boditech.entity.MdbTestTubeRackTestTaskTube; import com.dreamworks.boditech.service.TestService; +import com.dreamworks.boditech.utils.AppRuntimeException; +import com.dreamworks.boditech.utils.AppError; import com.dreamworks.boditech.utils.MyCommon; import java.util.List; import java.util.Objects; @@ -142,6 +144,6 @@ public class TaskTestTubeRackPrepare extends TaskBase { } // 任然没有找到 - throw new RuntimeException("TEST_TUBE_RACK_NOT_FOUND"); + throw new AppRuntimeException(AppError.TEST_TUBE_RACK_FEED_RACK_NOT_FOUND); } } diff --git a/src/main/java/com/dreamworks/boditech/service/AccountService.java b/src/main/java/com/dreamworks/boditech/service/AccountService.java index 01e2393..0d2bc7b 100644 --- a/src/main/java/com/dreamworks/boditech/service/AccountService.java +++ b/src/main/java/com/dreamworks/boditech/service/AccountService.java @@ -2,6 +2,8 @@ package com.dreamworks.boditech.service; import com.dreamworks.boditech.entity.ParamUserLogin; import com.dreamworks.boditech.entity.Account; import com.dreamworks.boditech.mapper.AccountMapper; +import com.dreamworks.boditech.utils.AppError; +import com.dreamworks.boditech.utils.AppRuntimeException; import jakarta.annotation.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,7 +47,7 @@ public class AccountService { Account account = userMapper.findByAccount(param.account); String hashPin = DigestUtils.md5DigestAsHex(param.pin.getBytes()); if ( null == account || !account.pin.equals(hashPin) ) { - throw new RuntimeException("ACCOUNT_LOGIN_INVALID_ACCOUNT_OR_PIN_CODE"); + throw new AppRuntimeException(AppError.ACCOUNT_LOGIN_INVALID_ACCOUNT_OR_PIN_CODE); } account.lastLoginTime = (int)(System.currentTimeMillis() / 1000); diff --git a/src/main/java/com/dreamworks/boditech/utils/AppError.java b/src/main/java/com/dreamworks/boditech/utils/AppError.java new file mode 100644 index 0000000..c804274 --- /dev/null +++ b/src/main/java/com/dreamworks/boditech/utils/AppError.java @@ -0,0 +1,8 @@ +package com.dreamworks.boditech.utils; +public enum AppError { + // account + ACCOUNT_LOGIN_INVALID_ACCOUNT_OR_PIN_CODE, + + + TEST_TUBE_RACK_FEED_RACK_NOT_FOUND +} diff --git a/src/main/java/com/dreamworks/boditech/utils/AppExceptionHandler.java b/src/main/java/com/dreamworks/boditech/utils/AppExceptionHandler.java new file mode 100644 index 0000000..ff6a81d --- /dev/null +++ b/src/main/java/com/dreamworks/boditech/utils/AppExceptionHandler.java @@ -0,0 +1,28 @@ +package com.dreamworks.boditech.utils; +import com.dreamworks.boditech.controller.entity.ApiResponse; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +@ControllerAdvice +public class AppExceptionHandler { + @ExceptionHandler(value = AppRuntimeException.class) + @ResponseBody + public ApiResponse handleAppRuntimeException(AppRuntimeException e) { + ApiResponse response = new ApiResponse(); + response.success = false; + response.message = e.getMessage(); + response.data = null; + response.code = e.getCode(); + return response; + } + + @ExceptionHandler(value = RuntimeException.class) + @ResponseBody + public ApiResponse handleRuntimeException(RuntimeException e ) { + ApiResponse response = new ApiResponse(); + response.success = false; + response.message = e.getMessage(); + response.data = null; + return response; + } +} diff --git a/src/main/java/com/dreamworks/boditech/utils/AppRuntimeException.java b/src/main/java/com/dreamworks/boditech/utils/AppRuntimeException.java new file mode 100644 index 0000000..731e6ae --- /dev/null +++ b/src/main/java/com/dreamworks/boditech/utils/AppRuntimeException.java @@ -0,0 +1,16 @@ +package com.dreamworks.boditech.utils; +public class AppRuntimeException extends RuntimeException { + // error code + private String code = ""; + + // constructor + public AppRuntimeException(AppError error) { + super(I18n.t("Error." + error.name())); + this.code = error.name(); + } + + // get error code + public String getCode() { + return this.code; + } +} diff --git a/src/main/java/com/dreamworks/boditech/utils/I18n.java b/src/main/java/com/dreamworks/boditech/utils/I18n.java new file mode 100644 index 0000000..0fa4e68 --- /dev/null +++ b/src/main/java/com/dreamworks/boditech/utils/I18n.java @@ -0,0 +1,22 @@ +package com.dreamworks.boditech.utils; +import org.springframework.context.MessageSource; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.stereotype.Component; +@Component +public class I18n { + private static MessageSource messageSource; + + // constructor + public I18n(MessageSource messageSource) { + I18n.messageSource = messageSource; + } + + // translate message + public static String t(String msgKey) { + try { + return messageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale()); + } catch (Exception e) { + return msgKey; + } + } +} diff --git a/src/main/java/com/dreamworks/boditech/utils/MyExceptionHandler.java b/src/main/java/com/dreamworks/boditech/utils/MyExceptionHandler.java deleted file mode 100644 index 504bfae..0000000 --- a/src/main/java/com/dreamworks/boditech/utils/MyExceptionHandler.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.dreamworks.boditech.utils; -import com.dreamworks.boditech.controller.entity.ApiResponse; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseBody; -@ControllerAdvice -public class MyExceptionHandler { - @ExceptionHandler(value = RuntimeException.class) - @ResponseBody - public ApiResponse handleRuntimeException(RuntimeException e ) { - ApiResponse response = new ApiResponse(); - response.success = false; - response.message = e.getMessage(); - response.data = null; - return response; - } -} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index caf4dfc..b36ded0 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,3 +1,6 @@ spring: profiles: - active: dev \ No newline at end of file + active: dev + messages: + basename: static/i18n/messages + encoding: utf-8 \ No newline at end of file diff --git a/src/main/resources/static/i18n/messages.properties b/src/main/resources/static/i18n/messages.properties new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/static/i18n/messages_en_US.properties b/src/main/resources/static/i18n/messages_en_US.properties new file mode 100644 index 0000000..2ef61aa --- /dev/null +++ b/src/main/resources/static/i18n/messages_en_US.properties @@ -0,0 +1 @@ +Error.ACCOUNT_LOGIN_INVALID_ACCOUNT_OR_PIN_CODE = "Invalid account or pin code" \ No newline at end of file diff --git a/src/main/resources/static/i18n/messages_ko_KR.properties b/src/main/resources/static/i18n/messages_ko_KR.properties new file mode 100644 index 0000000..46be940 --- /dev/null +++ b/src/main/resources/static/i18n/messages_ko_KR.properties @@ -0,0 +1 @@ +Error.ACCOUNT_LOGIN_INVALID_ACCOUNT_OR_PIN_CODE = "xxx" \ No newline at end of file diff --git a/src/main/resources/static/i18n/messages_zh_CN.properties b/src/main/resources/static/i18n/messages_zh_CN.properties new file mode 100644 index 0000000..60c01a8 --- /dev/null +++ b/src/main/resources/static/i18n/messages_zh_CN.properties @@ -0,0 +1 @@ +Error.ACCOUNT_LOGIN_INVALID_ACCOUNT_OR_PIN_CODE = 密码验证失败 \ No newline at end of file