question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

safely use translate.instant()

See original GitHub issue

is theire a way to load translations when application starts so that we can safely use translate.instant()

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:9
  • Comments:27

github_iconTop GitHub Comments

350reactions
szbartnikcommented, May 6, 2017

All you need to do (as it was partially mentioned) is to preload translations using .use() method. The problem is the method is async (we should wait for the result). The best way to force the application to wait for “something” to finish before it shows up is using APP_INITIALIZER function in your AppModule.

You have to add following provider to your AppModule’s providers section:

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

And define factory function appInitializerFactory upper in the same file:

import { Injector, APP_INITIALIZER } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { LOCATION_INITIALIZED } from '@angular/common';

export function appInitializerFactory(translate: TranslateService, injector: Injector) {
  return () => new Promise<any>((resolve: any) => {
    const locationInitialized = injector.get(LOCATION_INITIALIZED, Promise.resolve(null));
    locationInitialized.then(() => {
      const langToSet = 'en-GB'
      translate.setDefaultLang('en-US');
      translate.use(langToSet).subscribe(() => {
        console.info(`Successfully initialized '${langToSet}' language.'`);
      }, err => {
        console.error(`Problem with '${langToSet}' language initialization.'`);
      }, () => {
        resolve(null);
      });
    });
  });
}

Now application will wait for translations initialization before it shows up for user.

17reactions
xStarmancommented, Sep 1, 2018

Hi, I don’t know if this is the best solution but worked for me: string “init” is any key you want

translate.get('init').subscribe((text:string) => {
//use instant call here
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

ngx-translate .instant returns key instead of value
I am trying to make a method which would accept string key and return translated string value by using translate.instant ...
Read more >
Configuring ngx-translate to load at startup in Angular
So, basically, this is what you need to do in order to have instant translations in your app: preload translations using .use() method....
Read more >
How To Use ngx-translate with Angular - DigitalOcean
The method translate. setDefaultLang('en') allows you to specify a fallback set of translations to use in case there are missing translations ...
Read more >
How to translate your Angular app with ngx-translate
1. Add ngx-translate to your Angular application 2. Set up the TranslateService in your app.module.ts 3. Create your main language translation file (in...
Read more >
ngx-translate Documentation - CodeAndWeb
Gets the instant translated value of a key (or an array of keys). This method is synchronous and the default file loader is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found