Skip to content
This repository was archived by the owner on Nov 8, 2021. It is now read-only.

Load translations with promise, to allow preloading#30

Open
baso10 wants to merge 1 commit into
ngx-translate:masterfrom
baso10:loadPromise
Open

Load translations with promise, to allow preloading#30
baso10 wants to merge 1 commit into
ngx-translate:masterfrom
baso10:loadPromise

Conversation

@baso10
Copy link
Copy Markdown

@baso10 baso10 commented Oct 8, 2017

With APP_INITIALIZER and Promise we can load translations before app is started.

Usage example:

let translateHttpLoader: TranslateHttpLoader;

export function loadTranslations(http: HttpClient) {  
      return () => httpLoaderFactory(http).preLoad("en");
}

// AoT requires an exported function for factories
export function httpLoaderFactory(http: HttpClient) {
      if(translateHttpLoader == null) {
        translateHttpLoader = new TranslateHttpLoader(http);
      }
      return translateHttpLoader;
}

@NgModule({
   imports: [...,
      TranslateModule.forRoot({
        loader: {
          provide: TranslateLoader,
          useFactory: httpLoaderFactory,
          deps: [HttpClient]
        }
      })
   ],
   declarations: [...],
   providers: [
       ..,
      {provide: APP_INITIALIZER, useFactory: loadTranslations, deps: [HttpClient], multi: true},
     ...
   ],
  bootstrap: [AppComponent]
})

export class AppModule {}

Comment thread src/http-loader.ts Outdated
import {HttpClient} from "@angular/common/http";
import {TranslateLoader} from "@ngx-translate/core";
import "rxjs/add/operator/map";
import {Observable} from 'rxjs/Rx';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the import to 'rxjs/Observable'.

Comment thread src/http-loader.ts Outdated
*/
public preLoad(lang: string): Promise<any> {
return new Promise((resolve, reject) => {
this.http.get(`${this.prefix}${lang}${this.suffix}`)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can change the signature to:

get().subscribe(
result => ()....,
error => ()....
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case better to use:

    public preLoad(lang: string): Promise<any> {
        return this.httpClient.get(`${this.prefix}${lang}${this.suffix}`)
            .toPromise()
            .then(result => {
                this.loadedTranslations[lang] = result;
                return result;
            })
            .catch(() => null);
    }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree

@tiaguinho
Copy link
Copy Markdown

@baso10 Can you make this changes?

@akaNightmare
Copy link
Copy Markdown

Guys, any updates?

@sefasenturk95
Copy link
Copy Markdown

Still no updates on this?

Comment thread src/http-loader.ts Outdated
@@ -1,7 +1,10 @@
import {HttpClient} from "@angular/common/http";
import {TranslateLoader} from "@ngx-translate/core";
import {Observable} from 'rxjs/Observable';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to be imported from 'rxjs'

@gtteamamxx
Copy link
Copy Markdown

Can we push it?

@tiaguinho
Copy link
Copy Markdown

@baso10 please make the changes on import and request as @akaNightmare has suggested so we can merge this into master.

@gtteamamxx
Copy link
Copy Markdown

Guys, come on :D

@tiaguinho
Copy link
Copy Markdown

@ocombe can you merge this?

@thomastoledo
Copy link
Copy Markdown

We would really need this feature guys :D

@vtolstov
Copy link
Copy Markdown

ping...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants