44![ npm] ( https://img.shields.io/npm/dw/@larscom/ngx-translate-module-loader )
55[ ![ license] ( https://img.shields.io/npm/l/@larscom/ngx-translate-module-loader.svg )] ( https://github.com/larscom/ngx-translate-module-loader/blob/main/LICENSE )
66
7-
87> Highly configurable and flexible translations loader for [ @ngx-translate/core ] ( https://github.com/ngx-translate/core ) . Fetch multiple translations (http only) and configure them to your needs. Each translation file has it's own ** namespace** out of the box so the key/value pairs do not conflict with each other.
98
109### ✨ [ View on StackBlitz] ( https://stackblitz.com/edit/ngx-translate-module-loader )
@@ -21,52 +20,36 @@ npm i @larscom/ngx-translate-module-loader
2120
2221## Usage
2322
24- Create an exported ` moduleHttpLoaderFactory ` function
23+ Import the ` provideModuleTranslateLoader ` function and provide the options.
2524
2625``` ts
27- import { NgModule } from ' @angular/core'
28- import { BrowserModule } from ' @angular/platform-browser'
29- import { HttpClientModule , HttpClient } from ' @angular/common/http'
30- import { TranslateModule , TranslateLoader } from ' @ngx-translate/core'
31- import { ModuleTranslateLoader , IModuleTranslationOptions } from ' @larscom/ngx-translate-module-loader'
32- import { AppComponent } from ' ./app'
33-
34- export function moduleHttpLoaderFactory(http : HttpClient ) {
35- const baseTranslateUrl = ' ./assets/i18n'
36-
37- const options: IModuleTranslationOptions = {
38- modules: [
39- // final url: ./assets/i18n/en.json
40- { baseTranslateUrl },
41- // final url: ./assets/i18n/feature1/en.json
42- { baseTranslateUrl , moduleName: ' feature1' },
43- // final url: ./assets/i18n/feature2/en.json
44- { baseTranslateUrl , moduleName: ' feature2' }
45- ]
46- }
47-
48- return new ModuleTranslateLoader (http , options )
49- }
50-
51- @NgModule ({
52- imports: [
53- BrowserModule ,
54- HttpClientModule ,
55- TranslateModule .forRoot ({
56- loader: {
57- provide: TranslateLoader ,
58- useFactory: moduleHttpLoaderFactory ,
59- deps: [HttpClient ]
60- }
61- })
62- ],
63- bootstrap: [AppComponent ]
64- })
65- export class AppModule {
66- constructor (readonly translate : TranslateService ) {
67- translate .setDefaultLang (' en' )
68- }
69- }
26+ import { provideHttpClient } from ' @angular/common/http'
27+ import { bootstrapApplication } from ' @angular/platform-browser'
28+ import { provideModuleTranslateLoader } from ' @larscom/ngx-translate-module-loader'
29+ import { provideTranslateService } from ' @ngx-translate/core'
30+ import { AppComponent } from ' ./app/app.component'
31+
32+ const baseTranslateUrl = ' ./assets/i18n'
33+
34+ bootstrapApplication (AppComponent , {
35+ providers: [
36+ provideTranslateService ({
37+ // use loader and provide options
38+ loader: provideModuleTranslateLoader ({
39+ modules: [
40+ // final url: ./assets/i18n/en.json
41+ { baseTranslateUrl },
42+ // final url: ./assets/i18n/feature1/en.json
43+ { moduleName: ' feature1' , baseTranslateUrl },
44+ // final url: ./assets/i18n/feature2/en.json
45+ { moduleName: ' feature2' , baseTranslateUrl }
46+ ]
47+ })
48+ }),
49+ // http client is mandatory
50+ provideHttpClient ()
51+ ]
52+ }).catch ((err ) => console .error (err ))
7053```
7154
7255## Namespacing
@@ -76,20 +59,17 @@ By default, each translation file gets it's own namespace based on the `moduleNa
7659For example with these options:
7760
7861``` ts
79- export function moduleHttpLoaderFactory(http : HttpClient ) {
80- const baseTranslateUrl = ' ./assets/i18n'
62+ const baseTranslateUrl = ' ./assets/i18n'
8163
82- const options: IModuleTranslationOptions = {
83- modules: [
84- // no moduleName/namespace
85- { baseTranslateUrl },
86- // namespace: FEATURE1
87- { baseTranslateUrl , moduleName: ' feature1' },
88- // namespace: FEATURE2
89- { baseTranslateUrl , moduleName: ' feature2' }
90- ]
91- }
92- return new ModuleTranslateLoader (http , options )
64+ const options: IModuleTranslationOptions = {
65+ modules: [
66+ // no moduleName/namespace
67+ { baseTranslateUrl },
68+ // namespace: FEATURE1
69+ { baseTranslateUrl , moduleName: ' feature1' },
70+ // namespace: FEATURE2
71+ { baseTranslateUrl , moduleName: ' feature2' }
72+ ]
9373}
9474```
9575
@@ -122,7 +102,7 @@ Even though all JSON files from those modules are the same, they don't conflict
122102The configuration is very flexible, you can even define custom templates for fetching translations.
123103
124104``` ts
125- interface IModuleTranslationOptions {
105+ export interface IModuleTranslationOptions {
126106 /**
127107 * The translation module configurations
128108 */
@@ -155,7 +135,7 @@ interface IModuleTranslationOptions {
155135 * Custom translate merge function after retrieving all translation files
156136 * @param translations the resolved translation files
157137 */
158- translateMerger? : (translations : Translation []) => Translation
138+ translateMerger? : (translations : TranslationObject []) => TranslationObject
159139 /**
160140 * Provide custom headers at 'root' level, which means this headers gets added to every request
161141 * unless you specify headers at 'module' level.
@@ -166,7 +146,7 @@ interface IModuleTranslationOptions {
166146```
167147
168148``` ts
169- interface IModuleTranslation {
149+ export interface IModuleTranslation {
170150 /**
171151 * The module name
172152 *
@@ -194,7 +174,7 @@ interface IModuleTranslation {
194174 * Custom translation map function after retrieving a translation file
195175 * @param translation the resolved translation file
196176 */
197- translateMap? : (translation : Translation ) => Translation
177+ translateMap? : (translation : TranslationObject ) => TranslationObject
198178 /**
199179 * Custom path template for fetching translations
200180 * @example
0 commit comments