【发布时间】:2021-02-10 03:27:50
【问题描述】:
如何使用 Angular UI 翻译 JHipster App 中的自定义错误消息?
我添加了自定义异常处理程序:
@ExceptionHandler
default ResponseEntity<Problem> handleSomeBusinessException(final SomeBusinessException exception, final NativeWebRequest request) {
final Problem problem = Problem.builder()
.withStatus(UNPROCESSABLE_ENTITY)
.with("BusinessException", SomeBusinessException .class.getName())
.with("BusinessMessage", exception.getMessage())
.build();
return create(exception, problem, request);
}
接下来我修改了alert-error.component.ts
this.httpErrorListener = eventManager.subscribe('someApp.httpError', (response: JhiEventWithContent<HttpErrorResponse>) => {
const httpErrorResponse = response.content;
switch (httpErrorResponse.status) {
case 422:
this.addErrorAlert(httpErrorResponse.error.BusinessMessage);
break;
不幸的是,当我运行应用程序并抛出 SomeBusinessException 时,我在 UI 中看到以下内容:
translation-not-found[一些与 SomeBusinessException]
你能告诉我在哪里可以介绍我的BusinessMessage的翻译吗?
更新: 我检查了 src\main\webapp\i18n\en\error.json 但它绑定到 http.code,而不是消息本身
【问题讨论】:
-
你需要在前端的json翻译文件中创建一个专门的翻译。它应该在
src/main/webapp/i18n/en/error.json -
如何指定我希望使用 BusinessMessage 进行翻译,而不是使用 http 代码?我有多个业务层异常,都有 422 代码
-
你可以使用例如使用业务异常的简单类名作为翻译键而不是代码。据我所知,
BusinessMessage是异常消息,它可能不是一个好的翻译键。
标签: angular spring-boot exception jhipster