JHipster 自定义错误信息翻译
JHipster custom error message translation
如何使用 Angular UI 翻译 JHipster 应用程序中的自定义错误消息?
我添加了自定义异常处理程序:
@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[Some business message related to
SomeBusinessException]
你能告诉我在哪里可以介绍我的 BusinessMessage 的翻译吗?
更新:
我检查了 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);
}
第二个是 AlertErrorComponent 的构造函数:
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
{
"business": {
"SomeBusinessException ": "Translated text goes here. {{parameterKey}}",
}
...
}
{{parameterKey}} 将被 ngx-translate 替换为 "这里有一些值"
因此您将在屏幕上看到以下消息:
Translated text goes here. some value goes here
您还可以查看完整的源代码here
如何使用 Angular UI 翻译 JHipster 应用程序中的自定义错误消息?
我添加了自定义异常处理程序:
@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[Some business message related to SomeBusinessException]
你能告诉我在哪里可以介绍我的 BusinessMessage 的翻译吗?
更新: 我检查了 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);
}
第二个是 AlertErrorComponent 的构造函数:
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
{
"business": {
"SomeBusinessException ": "Translated text goes here. {{parameterKey}}",
}
...
}
{{parameterKey}} 将被 ngx-translate 替换为 "这里有一些值"
因此您将在屏幕上看到以下消息:
Translated text goes here. some value goes here
您还可以查看完整的源代码here