angular-l10n icon indicating copy to clipboard operation
angular-l10n copied to clipboard

Translate by word

Open johaven opened this issue 6 years ago • 1 comments

I'm submitting a...

[ ] bug report
[x] feature request
[ ] support request

I often use the same words in different sentences, for example:

  • user added
  • group added
  • user removed
  • ....

It would be interesting to have an option (in translate pipe) that allows (for a sentence) to take each word and try to translate them one by one.

This would produce lighter translation files and avoid repetition.

johaven avatar Aug 25 '19 20:08 johaven

I am not certain if I got it right, but I was able to achieve the translation by each word using the following approach:

At first, I created an own Pipe, where I inject the L10nTranslationService, I basically use this service to translate, and finally join the translation result to form a sentence.

import { Pipe } from "@angular/core";
import { L10nTranslationService } from "angular-l10n";

@Pipe({
    name: "translateSentence",
    pure: false,
})
export class TranslateSentencePipe {

    constructor(private translationService: L10nTranslationService) { }

    public transform(key: any, language: string, params?: any): string | null {
        const resultTranslation = this.translationService.translate(
            key,
            params,
            language
        );
        return Object.values(resultTranslation).join(" ");
    }
}

This is how I use it on my template:

<p>{{ ['home.hello', ' ', 'home.world', 'home.whoIAm'] | translateSentence:locale.language:{ name: 'Rob' } }}</p>

Result: Hello world!! I am Rob

robli314 avatar Oct 14 '21 22:10 robli314

Closed due to inactivity

robisim74 avatar May 14 '23 07:05 robisim74