I'm submitting a ... (check one with "x")
[ ] bug report => check the FAQ and search github for a similar issue or PR before submitting
[ ] support request => check the FAQ and search github for a similar issue before submitting
[x] feature request
Current behavior
If I implement the MissingTranslationHandler like this:
@Injectable()
export class MyHandler implements MissingTranslationHandler {
constructor(private translateService: TranslateService) {
}
public handle(key: string) {
return this.translateService.get('MISSING', { original: key });
}
}
then this results in a cyclic dependency: TranslateService depends on MissingTranslationHandler, which in turn depends on TranslateService.
Expected/desired behavior
The desired behavior would be if the handle method provided an instance of TranslateService, e.g.
public handle(key: string, translateService: TranslateService) {
return translateService.get('MISSING', { original: key });
}
or, given the #214 proposal, something like
interface TranslateHandleArgs {
key: string;
translation: TranslateService;
interpolateParams?: any;
}
public handle(params: TranslateHandleArgs) {
return params.translation.get('MISSING', { original: key });
}
Such a thing, in turn, would potentially trigger an infinite loop if the 'MISSING' key is missing, too. This should be taken into account somehow.
What is the motivation / use case for changing the behavior?
Suppose you need to convert API error codes into localized messages. APIs change frequently, and if you haven't yet covered a specific error code, then you need to output something generic along the lines of "An error occurred, code: {{code}}". That is the case this feature request would cover.
Please tell us about your environment:
Here's a workaround if someone needs it:
import { Injectable, Injector } from '@angular/core';
import { MissingTranslationHandler, TranslateService } from 'ng2-translate';
@Injectable()
export class CustomMissingTranslationHandler extends MissingTranslationHandler {
private missingKey: string;
private translation: TranslateService;
constructor(private injector: Injector) {
super();
}
public handle(key: string) {
if (this.translation == null) {
this.translation = this.injector.get(TranslateService);
}
if (this.missingKey != null) {
// already handling the missing translation, and the '__TRANSLATION_MISSING' part is missing, too.
// Return the key itself
let previouslyMissingKey = this.missingKey;
this.missingKey = null;
return previouslyMissingKey;
}
this.missingKey = key;
// do your thing using this.translation:
let result = this.translation.instant('__TRANSLATION_MISSING', { key });
this.missingKey = null;
return result;
}
}
The missing translations handler needs more love, I would love a pull request improving it because I don't have much time to work on this
Most helpful comment
Here's a workaround if someone needs it: