Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(missing-translation-handler) - add third optional param to the "… #324

Merged
merged 1 commit into from
Apr 7, 2021
Merged

feat(missing-translation-handler) - add third optional param to the "… #324

merged 1 commit into from
Apr 7, 2021

Conversation

dzena
Copy link

@dzena dzena commented Apr 7, 2021

…handle" method to accept option parameters contained in the translation key.

This will allow us to handle optional params in the missing translations if we want to fallback to a default language (the language we are sure all translations are up to date), something like this:

@Injectable()
export class MissingTranslation implements L10nMissingTranslationHandler {
    private translation: L10nTranslationService;

    constructor(@Optional() private injector: Injector) {}

    public handle(key: string, value?: string, params?: any): string | any {
        // avoid cyclic dependency
        this.translation = this.injector.get(L10nTranslationService);
        return this.translation.translate(key, params, 'en-US');
    }
}

…handle" method to accept option parameters contained in the translation key
@robisim74 robisim74 merged commit c7a0be9 into robisim74:master Apr 7, 2021
@dzena dzena deleted the f-missing-translation-handler-params branch April 7, 2021 13:13
@robisim74
Copy link
Owner

Hi Milan,

thanks a lot for this PR! I'll release a new version by tonight.

Just a note: I think it's related to #279 , but it was only an example. I think you need a more robust handle, because if the key is not present even in the default language, you risk a loop:

    public handle(key: string, value?: string, params?: any): string | any {
        // avoid cyclic dependency
        this.translation = this.injector.get(L10nTranslationService);

        if (this.translation.has(key, 'en-US')) {
            return this.translation.translate(key, params, 'en-US');
        } else {
            return 'not found';
        }
    }

I must also suggest that you consider using (alternatively) a custom TranslationFallback: https://github.com/robisim74/angular-l10n#translation-fallback

Greetings

@dzena
Copy link
Author

dzena commented Apr 7, 2021

Hi Roberto,

Thanks for such an expedite response.

Also, really appreciate you suggestions. The reason I am not using TranslationFallback is that when I started with the lib (~2 years ago) it didn't work as expected and since then I never tried it anymore. I'll certainly give it another try.

Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants