core icon indicating copy to clipboard operation
core copied to clipboard

Add possibility to pass a desired target language to TranslatePipe

Open mzellho opened this issue 9 years ago • 11 comments
trafficstars

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 Using the TranslatePipe, there is no possibility to get a translation for a language other than the one that is set up - one can only pass interpolateParams to TranslatePipe.

Expected/desired behavior It would be cool if one could also pass the token of the desired langauge for the translation to the pipe in case you need to translate a key to two (or more) different languages on the same page. Of course, this could be handled via the TranslateService using a method or another custom pipe, but passing the langauge token to the pipe would be a more elegant solution for the user.

<p>english: {{ some.key | translate }}</p>
<p>french: {{ some.key | translate:'fr' }}</p>
<p>german: {{ some.key | translate:'de' }}</p>

mzellho avatar Sep 14 '16 09:09 mzellho

Hello, this is an interesting feature, but this requires some big changes in the code :)

I keep it here, but don't expect this to be added soon unless someone else wants to do a PR

ocombe avatar Sep 14 '16 09:09 ocombe

@ocombe: Glad you like the idea :-). Not sure if I can really find enough time at the moment, but if I can, I'd be happy to help. Just in case: Would there be any design proposals for the feature from your side?

A little question about the ways I took to get a rather hacky, but working workaround for the moment...

I implemented a custom pipe that forwards the key to TranslateService:

let currentLanguage: string = this.translateService.currentLang;

this.translateService.currentLang = languageCode;
let translation: string = this.translateService.instant(key);
this.translateService.currentLang = currentLanguage;

return translation;

First I tried to go with use(lang: string), but I had some strange effects there. But since I wanted to avoid emitting any events in order to avoid performance issues anyway, I switched to above way and noticed, that the second language was missing in the translations object of the TranslateService. So I added all supported languages in the constructor of our component by calling use(lang: string):

translateService.use("de");
translateService.use("en");
translateService.setDefaultLang("de");
translateService.use(userLang);

Now, this works. But feels a little odd - can you think of some better approach?

mzellho avatar Sep 14 '16 11:09 mzellho

It's a great function。I need it.

alanhe421 avatar Jan 03 '17 03:01 alanhe421

@ocombe do you think this is gonna be added anytime soon?

The workaround I posted last year is still working with @angular:4.4.4 and @ngx-translate:8.0.0, but from time to time I am facing the issue that the application is being loaded in the wrong language which has a slightly annoying impact on the stability of my webtests...

Thanks!

mzellho avatar Oct 13 '17 08:10 mzellho

I think this is also discussed in this issue : https://github.com/ngx-translate/core/issues/719

@MZellhofer thanks for the workaround. But it probably leads to a complete refresh of the UI (?) and also trigger all subscriptions of _ TranslateService.onLangChange_ property right ?

nilebma avatar Jan 21 '18 23:01 nilebma

I made an implementation for a non-pipe usage (because I didn't need the pipe at this moment), but converting it to a pipe would be pretty easy:

public getTranslation(key: string, language: string, interpolationParams?: Object): Observable<string> {
    return this.translator.getTranslation(language).pipe(
      map(translations => {
        return this.translator.getParsedResult(translations, key, interpolationParams);
      })
    );
  }

Supamiu avatar Nov 05 '18 15:11 Supamiu

Please team consider applying the changes from angular-translate: https://github.com/angular-translate/angular-translate/commit/e591462af379150cb1bae29f493137cb825025b1

core function: $translate.instant(translationId, interpolateParams, interpolation, forceLanguage);

Wingzzzzz avatar Jan 11 '19 04:01 Wingzzzzz

It's 2024 and this feature is still very much needed. Any updates?

ThanosAlmighty avatar Feb 08 '24 16:02 ThanosAlmighty

We also came across the need for this feature. Any updates?

dananaprey avatar Apr 11 '24 15:04 dananaprey

https://github.com/ngx-translate/core/issues/233#issuecomment-2049997248

I ended up creating a custom implementation. I'm happy to share my approach if you would like.

ThanosAlmighty avatar Apr 12 '24 19:04 ThanosAlmighty

#233 (comment) We've already done it without these feature, but I'd take a look)

dananaprey avatar May 06 '24 14:05 dananaprey