I'm submitting a ... (check one with "x")
[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
When I have a translation for a tag in one language but not in another, the implementation falls back into the default language.
Expected/desired behavior
In my implementation of the interface I make it very obvious to the user that a translation is missing, so that QA can pick it up and we fix it. I would like to have an option for it not to fallback to the default language but to let my handler handle it.
Reproduction of the problem
If the current behavior is a bug or you can illustrate your feature request better with an example, please provide the steps to reproduce and if possible a minimal demo of the problem via https://plnkr.co or similar (you can use this template as a starting point: http://plnkr.co/edit/tpl:btpW3l0jr5beJVjohy1Q).
http://plnkr.co/edit/DkTJVklO7O6pfLY9IZCB?p=preview
What is the expected behavior?
Let me show something other than the default language translation
What is the motivation / use case for changing the behavior?
--need to help testers find missing translations
Please tell us about your environment:
ng2-translate version: 4.2.0
Angular version: 2.2.3
Browser: [Chrome 55]
Language: [TypeScript 2.0]
Shouldn't this already be possible by providing a MissingTranslationHandler?
That's what I mean. The MissingTranslationHandler works when I don't have a translation at all, but when I have a translation for something in the default language it uses that instead. (I guess that's what default means). But I would need the behaviour that even when the default translation exists the missingTranslationHandler gives me the option to step in.
Have you tried not setting a default language?
Unfortunately if there is no default language, it is automatically set when you call use. I didn't take this use case into account because I didn't expect that someone would actually want the translations not to show up.
You could probably abuse this by setting the default lang manually. In 5.x it's not possible because the property is private, but it's public in 6.x (the beta 1 is available).
If you set the default lang to a lang that doesn't exists, it should probably work the way you expect it to work:
translateService.defaultLang = "_no-lang_";
Also hitting this issue.
I have set:
tranlsateService.setDefaultLang('en');
translateService.use('fr')
My expectation is that when a French String is not found the MissingTranslationHandler would be invoked. This isn't occurring. The MissingTranslationHandler is only invoked when there is no translation in French or English.
Since MissingTranslationHelper falls back to the default language translation if there isn't one in the desired language, and also takes empty strings as valid translations (so it results in blank spaces in app), I overload the TranslatePipe like this (same could be done for TranslateProvider) :
```custom-pipe.ts
export class CustomTranslatePipe extends TranslatePipe implements PipeTransform {
transform(key: any, args: any[]): string {
let result = super.transform(key, args);
return (result !== "" && result !== key) ? result : "Missing text (" + key + ")";
}
}
``` app.module.ts
@NgModule({
declarations: [
CustomTranslatePipe
], ...
This way, if the translation is left empty in the selected language or if it doesn't exist (no matching key), we get a "Missing text (KEY)" message that helps our teammates in charge of translating our app.
I also hit this issue. We have namespaces in our dictionary and we load the namespaces lazily with a MissingTranslationHandler.
I solved this problem now by calling both setDefaultLang and use when switching language.
[edit] just in case anybody is curious:
We lazily load our translations but we don't load them per module. Instead we load them per namespace. All our translation keys are prefixed by a namespace. Whenever the MissingTranslationHandler is called we load all the translations of the missing namespace.
@MichaelPolla How did you prevent the default TranslatePipe from being loaded?
Hello, I'm closing this issue because it's too old.
If you have a similar problem with recent version of the library, please open a new issue.
Most helpful comment
Since
MissingTranslationHelperfalls back to the default language translation if there isn't one in the desired language, and also takes empty strings as valid translations (so it results in blank spaces in app), I overload theTranslatePipelike this (same could be done forTranslateProvider) :```custom-pipe.ts
export class CustomTranslatePipe extends TranslatePipe implements PipeTransform {
transform(key: any, args: any[]): string {
let result = super.transform(key, args);
return (result !== "" && result !== key) ? result : "Missing text (" + key + ")";
}
}
This way, if the translation is left empty in the selected language or if it doesn't exist (no matching key), we get a "Missing text (KEY)" message that helps our teammates in charge of translating our app.