core icon indicating copy to clipboard operation
core copied to clipboard

How to custom TranslateLoader to pass multi parameters to URL?

Open ThietPhamDinh opened this issue 5 years ago • 2 comments

I have url to get file json from backend like this

/assets/${customerName}/${lang}.json

I tried to many way but i don't know how to pass customerName to url inside prefix of TranslateHttpLoader. pls help me ASAP. I really appreciate your support.

ThietPhamDinh avatar Aug 06 '19 20:08 ThietPhamDinh

Hi, I too have the same question. Do we got a answer? Actually I like to call the getTranslation method but it has only one parameter that is language. Here I like to call the api and pass the list of keys to api and get the values only for the requested key. So I am looking how to pass the parameter to the api call.

PKSSDev avatar Oct 05 '20 16:10 PKSSDev

Try below one.

//imports

let yourParam: string;

class CustomLoader implements TranslateLoader {

  constructor() { }

  getTranslation(lang: string): Observable<any> {
    console.log(yourParam);
  }
}


@NgModule({
  imports: [
    CommonModule,
    TranslateModule.forRoot({
      defaultLanguage: 'en-us',
      loader: {
        provide: TranslateLoader,
        useClass: CustomLoader,
        deps: []
      }
    }),
  ],
  exports: [CommonModule, TranslateModule],
  providers: []
})
export class i18nModule {

  static forRoot(customParam: string): ModuleWithProviders<i18nModule> {
    yourParam = customParam;
    return {
      ngModule: i18nModule
    }
  }

  constructor(translate: TranslateService) {
   	//do logic of translation
  }
}

sivarajans avatar Jan 20 '22 14:01 sivarajans