In my Angular project, I am working with a json file located in src\assets\i18n that contains Swedish translations:
{
"COMMON": {
"BUTTON-NO-FILE": "Dokument saknas",
"BUTTON-EDIT": "Redigera",
"BUTTON-SAVE": "Spara",
"BUTTON-CANCEL": "Avbryt",
"BUTTON-OK": "Ok",
"BUTTON-CLOSE": "Stäng",
"ERRORS": {
"GENERAL-TITLE": "Ett fel har inträffat",
"401-MSG": "Testing 401 message",
"499-MSG": "Applikationen svarar inte, försök igen senare.",
"403-MSG": "Handlingen kan inte utföras, kontrollera din behörighet."",
"404-MSG"": "Resurserna som du försöker nå finns inte.",
"400-MSG": "Ett fel har inträffat. Kontakta HSB om felet fortfarande kvarstår."
}
}
}
I have added a custom message for error code 401: "Testing 401 message".
Now, there is a logic implemented to map the error messages in the translation json file based on their respective error status:
import {
HttpEvent,
HttpInterceptor,
HttpHandler,
HttpRequest,
HttpErrorResponse
} from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError, map, mergeMap } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { MessageDialogService } from '@hsbweb/hsb-shared-components-module';
import { TranslateService } from '@ngx-translate/core';
@Injectable()
export class HttpErrorInterceptor implements HttpInterceptor {
constructor(
private messageDialogService: MessageDialogService,
private translate: TranslateService) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request)
.pipe(
catchError((error: HttpErrorResponse) => {
let errorMessage = '';
switch (error.status) {
case 499:
errorMessage = this.translate.instant('COMMON.ERRORS.499-MSG');
break;
case 404:
errorMessage = this.translate.instant('COMMON.ERRORS.404-MSG');
break;
case 403:
errorMessage = this.translate.instant('COMMON.ERRORS.403-MSG');
break;
case 400:
errorMessage = this.translate.instant('COMMON.ERRORS.400-MSG');
break;
case 401:
errorMessage = this.translate.instant('COMMON.ERRORS.401-MSG');
break;
default:
errorMessage = this.translate.instant('COMMON.ERRORS.400-MSG');
break;
}
this.messageDialogService.error(
this.translate.instant('COMMON.ERRORS.GENERAL-TITLE'),
errorMessage,
this.translate.instant('COMMON.BUTTON- OK'));
return throwError(error);
})
);
}
}
Although I have specified the case for the 401-message, it still refers to COMMON.ERRORS.401-MSG
instead of the actual value in the translation file.
Do I need to make any changes to the json file after editing it? Even re-compiling the Angular project did not resolve the issue. Being new to Angular, what steps should I take next?