Search code examples
angulartypescript

How to use service in Angular ngModule


Here is a shared module and I want to use translate service inside module but the problem here is that setCulture(translate.currentLang); the translate here is unknown.

import { CommonModule } from '@angular/common';
import { inject, NgModule } from '@angular/core';
import * as ejsTranslation from './ejs-translations.json';
import { L10n, setCulture } from '@syncfusion/ej2-base';
import { TranslateService } from '@ngx-translate/core';


setCulture(translate.currentLang);
L10n.load(ejsTranslation);

@NgModule({
  declarations: [
  ],
  imports: [
    CommonModule
  ],
  providers: [TranslateService]
})
export class SharedModule { }

Solution

  • You can try initializing on the module level, like so.

    import { CommonModule } from '@angular/common';
    import { inject, NgModule } from '@angular/core';
    import * as ejsTranslation from './ejs-translations.json';
    import { L10n, setCulture } from '@syncfusion/ej2-base';
    import { TranslateService } from '@ngx-translate/core';
    ...
    @NgModule({
      declarations: [
      ],
      imports: [
        CommonModule
      ],
      providers: [TranslateService]
    })
    export class SharedModule { 
     constructor(private translate: TranslateService) {
       setCulture(translate.currentLang);
     }
    }
    

    SyncFusion Code Reference