To use ng-translation you have to import the NgTranslationModule.forRoot() in the root module of your Angular application:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { NgTranslationModule } from '@logikum/ng-translation';
@NgModule({
imports: [
BrowserModule,
NgTranslationModule.forRoot( configuration )
],
bootstrap: [
AppComponent
]
})
export class AppModule { }
The forRoot method requires a configuration object, see later.
In feature modules you have to use the NgTranslationModule.forChild() method, or if you use a shared module that you import in multiple other feature modules, you can export the NgTranslationModule to make sure you don't have to import it in every module.
@NgModule({
exports: [
CommonModule,
NgTranslationModule.forChild()
]
})
export class SharedModule { }
For lazy loaded modules yuo have to use the LoadTranslationsGuard in the route definitions to load the translations before loading the modules:
import { RouterModule, Routes } from '@angular/router';
import { NgTranslationModule, LoadTranslationsGuard } from '@logikum/ng-translation';
const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: SetupComponent },
{
path: 'lazy',
loadChildren: './lazy/lazy.module#LazyModule',
canLoad: [ LoadTranslationsGuard ]
},
{ path: '**', redirectTo: 'home' }
];
@NgModule({
imports: [
BrowserModule,
RouterModule.forRoot( routes ),
NgTranslationModule.forRoot( configuration )
]
})
export class AppModule { }
The translations belonging to the lazy loaded modules are identified by the path. To avoid path conflicts you can use an alternative way to identify the proper translation. Add a data object having a section prefix property to the route definition:
const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: SetupComponent },
{
path: 'lazy',
loadChildren: './lazy/lazy.module#LazyModule',
canLoad: [ LoadTranslationsGuard ],
data: { sectionPrefix: 'laggard' }
},
{ path: '**', redirectTo: 'home' }
];