Translate by word
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.
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
Closed due to inactivity