Angular JHipster自定义错误消息翻译

Angular JHipster自定义错误消息翻译,angular,spring-boot,exception,jhipster,Angular,Spring Boot,Exception,Jhipster,如何翻译JHipster应用程序中带有Angular UI的自定义错误消息 我已添加自定义异常处理程序: @ExceptionHandler default ResponseEntity<Problem> handleSomeBusinessException(final SomeBusinessException exception, final NativeWebRequest request) { final Problem problem =

如何翻译JHipster应用程序中带有Angular UI的自定义错误消息

我已添加自定义异常处理程序:

    @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);
    } 
@ExceptionHandler
default ResponseEntity handleSomeBusinessException(最终SomeBusinessException异常,最终NativeWebRequest请求){
最终问题=Problem.builder()
.withStatus(不可加工实体)
.with(“BusinessException”,SomeBusinessException.class.getName())
.with(“BusinessMessage”,exception.getMessage())
.build();
返回创建(异常、问题、请求);
} 
接下来,我修改了alert-error.component.ts

this.httpErrorListener=eventManager.subscribe('someApp.httpError',(响应:JhiEventWithContent)=>{
const httpErrorResponse=response.content;
开关(httpErrorResponse.status){
案例422:
this.addErrorAlert(httpErrorResponse.error.BusinessMessage);
打破
不幸的是,当我运行应用程序并抛出SomeBusinessException时,我在UI中看到以下内容:

找不到翻译[某些与 [例外]

你能告诉我在哪里可以介绍我的商业信息的翻译吗

更新:
我已经检查了src\main\webapp\i18n\en\error.json,但它绑定到http.code,而不是消息本身

这可以使用简单的异常名称来完成。此外,您还可以传递参数,由ngx translate代替

首先,您需要在问题中传递自定义参数

@ExceptionHandler
default ResponseEntity<Problem> handleSomeBusinessException(final SomeBusinessException exception, final NativeWebRequest request) {

    final Problem problem = Problem.builder()
        .withStatus(UNPROCESSABLE_ENTITY)
        .with("businessException", SomeBusinessException.class.getSimpleName())
        .with("parameterKey", "some value goes here")
        .build();

    return create(exception, problem, request);
} 
constructor(private alertService: JhiAlertService, private eventManager: JhiEventManager, translateService: TranslateService) {
...
     switch (httpErrorResponse.status) {
        ...
        case 422:
          this.addErrorAlert('', 'business.' + httpErrorResponse.error.businessException, httpErrorResponse.error);
          break;
三是提供参数翻译(由ngx translate支持):

src/main/webapp/i18n/en/error.json

{{parameterKey}}将被ngx translate替换为“此处有一些值”

因此,您将在屏幕上看到以下消息:

翻译后的文本放在这里,有些值放在这里


您还可以检查完整的源代码

您需要在前端json翻译文件中创建一个专用的翻译。它应该位于
src/main/webapp/i18n/en/error.json
如何指定我希望使用BusinessMessage而不是http代码进行翻译?我有多个业务层异常,都有422个codeYou c您可以使用,例如,使用业务异常的简单类名作为翻译键,而不是代码。从我所看到的
BusinessMessage
是异常消息,它可能不是一个好的翻译键。感谢您在此处提供详细答案。
constructor(private alertService: JhiAlertService, private eventManager: JhiEventManager, translateService: TranslateService) {
...
     switch (httpErrorResponse.status) {
        ...
        case 422:
          this.addErrorAlert('', 'business.' + httpErrorResponse.error.businessException, httpErrorResponse.error);
          break;
{
  "business": {
    "SomeBusinessException ": "Translated text goes here. {{parameterKey}}",
  }
  ...
}