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.

Translation inside feature modules

See original GitHub issue

So I’m trying to make the translation to work globally, but I don’t seem to get how it works. It might be my lack of understanding of how modules/services in Angular 2 work. Following the guide here I have the following setup right now:

  bootstrap: [AppComponent],
  declarations: [
    AppComponent,
    HomeComponent
  ],
  imports: [ 
    LocaleModule.forRoot(), // New instance of LocaleService.
    LocalizationModule.forRoot(), // New instance of LocalizationService.
    CoreModule,
    CoursesModule
    ....
  ],
})
export class AppModule {}

And my AppComponent:

@Component({
  selector: 'app',
  encapsulation: ViewEncapsulation.None,
  styleUrls: ['./app.component.scss'],
  templateUrl: './app.component.html'
})
export class AppComponent extends Locale implements OnInit {


  constructor(
    public appState: AppState,
    public locale: LocaleService,
    public localization: LocalizationService
  ) {
    super(locale, localization);

    this.locale.addLanguages(['en', 'es']);
    this.locale.definePreferredLocale('en', 'US', 30);
    this.locale.definePreferredCurrency('USD');
    this.localization.translationProvider('/locale/locale-');
    this.localization.updateTranslation();
  }
}

And then in my CoursesComponent, I have set it as the guide for Lazy routing:

@NgModule({
    imports: [
        ...
        LocaleModule, // LocaleService is singleton.
        LocalizationModule.forChild() // New instance of LocalizationService.
    ],
    declarations: [CoursesComponent]
})

The translation works in my HomeComponent, but not in my CoursesComponent. What’s weird is that if I import LocalizationModule only and not LocalizationModule.forChild(), translation works. Does that mean that it is creating an instance for the first time and the instance created from AppModule is not injected inside CoursesModule? Do I need to export the service through the CoreModule? I’m confused, any help is appreciated!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
robisim74commented, Feb 12, 2017

@Shiroh1ge I’m closing this issue. If you need, open a new issue. Greetings

1reaction
robisim74commented, Jan 20, 2017

You’ll never know until you try… 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Feature modules - Angular
A feature module delivers a cohesive set of functionality focused on a specific application need such as a user workflow, routing, or forms....
Read more >
Features Translations | Drupal.org
Features Translations adds a new Features component called Translations that allows to import/export sets of translated strings.
Read more >
(Angular) ngx-translate Feature Module not extending Parent ...
Now i can access only feature module's translation only from feature component. I can see in network the parent's translation is getting ...
Read more >
Are you still assembling translations in enormous files in times ...
This way, feature-specific modules will contain their own translations that will be loaded on demand, instead of fetching gigantic files ...
Read more >
Inline Loaders | Transloco Angular i18n - GitHub Pages
When working on a feature module or a library (which is common in a monorepo environment), we may want to have the translation...
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