Core: Hide translation labels until language file is loaded

Created on 29 Sep 2017  路  16Comments  路  Source: ngx-translate/core

I'm submitting a ... (check one with "x")

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

Question
How do I hide translation labels until language file is loaded? I assume this would involve some kind of cloak directive.

Current behavior
Problem is when I have something like {{ Section.Title | translate }}, Section.Title is visible until language file is loaded.

  • ngx-translate version: latest
  • Angular version: 4.0

Most helpful comment

@lazabazsa

That's what I do. (LocalSettingsService= custom service, Store = ngrx store)

providers: [
    {
      provide: APP_INITIALIZER,
      useFactory: appInitializerFactory,
      deps: [TranslateService, LocalSettingsService, Store, Injector],
      multi: true
    }
 ]

and

export function appInitializerFactory(translateService: TranslateService, localSettingsService: LocalSettingsService, store: Store<AppState>, injector: Injector): () => Promise<any> {
  return () => new Promise<any>((resolve: any) => {
    const locationInitialized = injector.get(LOCATION_INITIALIZED, Promise.resolve(null));
    locationInitialized.then(() => {
      translateService.addLangs(['en', 'de']);
      translateService.setDefaultLang('en');
      let selectedLanguage = localSettingsService.getSelectedLanguage();
      if (!selectedLanguage) {
        const browserLang = translateService.getBrowserLang();
        selectedLanguage = browserLang.match(/en|de/) ? browserLang : 'en';
      }
      translateService.use(selectedLanguage).pipe(take(1)).subscribe(() => store.dispatch(new HydrateSelectedLanguageAction(selectedLanguage)),
        err => console.error(err), () => resolve(null));
    });
  });
}

All 16 comments

Same question...

may be should make it hidden by default and in ts make listener and in this listener activate this block ?

Is there an update on this matter?

bump**

This is such a basic thing to have.. Any update on this?

Hi guys. I fix it in my situation but I'm not sure that it help you

TranslatePipe.prototype.updateValue = function (key, interpolateParams, translations) {
  var _this = this;
  var onTranslation = function (res) {
      **_this.value = res !== undefined ? res != key ? res: "" : key;** //_rewrited line_
      _this.lastKey = key;
      _this._ref.markForCheck();
  };
  if (translations) {
      var res = this.translate.getParsedResult(translations, key, interpolateParams);
      if (typeof res.subscribe === 'function') {
          res.subscribe(onTranslation);
      }
      else {
          onTranslation(res);
      }
  }
  this.translate.get(key, interpolateParams).subscribe(onTranslation);
}

_this.value = res !== undefined ? res != key ? res: "" : key;

In my situation I just skip value in pipe if pipe-value == translation key.

For example

{{Labels.Name | translation }}

in my code if res == Labels.Name i just set empty string - but if value != Labels.Name
I just set Value.
So pipe waiting for value that not equeal translation key Labels.Name

I add thit prototype in app.module. But i think You can add it anywhere

Has anyone found a solution to this issue?

Any updates on this?

@endless-shining @Maryna-Yelakova Not 100% sure if it fixed it for me, but I don't see the keys anymore when I initialize the translation stuff within APP_INITIALIZER.

@MJomaa it does not work for me

@MJomaa Could you please share your code snippet? I tried it, but couldn't make it work. Thanks!

This is still an issue, with 2 Angular versions up and one year later :(

@lazabazsa

That's what I do. (LocalSettingsService= custom service, Store = ngrx store)

providers: [
    {
      provide: APP_INITIALIZER,
      useFactory: appInitializerFactory,
      deps: [TranslateService, LocalSettingsService, Store, Injector],
      multi: true
    }
 ]

and

export function appInitializerFactory(translateService: TranslateService, localSettingsService: LocalSettingsService, store: Store<AppState>, injector: Injector): () => Promise<any> {
  return () => new Promise<any>((resolve: any) => {
    const locationInitialized = injector.get(LOCATION_INITIALIZED, Promise.resolve(null));
    locationInitialized.then(() => {
      translateService.addLangs(['en', 'de']);
      translateService.setDefaultLang('en');
      let selectedLanguage = localSettingsService.getSelectedLanguage();
      if (!selectedLanguage) {
        const browserLang = translateService.getBrowserLang();
        selectedLanguage = browserLang.match(/en|de/) ? browserLang : 'en';
      }
      translateService.use(selectedLanguage).pipe(take(1)).subscribe(() => store.dispatch(new HydrateSelectedLanguageAction(selectedLanguage)),
        err => console.error(err), () => resolve(null));
    });
  });
}

thanks @MJomaa, it's works

Just in case:

this.translated = await this.translateService.get('key.to.label').toPromise();

Any updates on this issue? It's the basic thing to have..

Was this page helpful?
0 / 5 - 0 ratings