4
4
![ npm] ( https://img.shields.io/npm/dw/@larscom/ngx-translate-module-loader )
5
5
[ ![ license] ( https://img.shields.io/npm/l/@larscom/ngx-translate-module-loader.svg )] ( https://github.com/larscom/ngx-translate-module-loader/blob/main/LICENSE )
6
6
7
-
8
7
> 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.
9
8
10
9
### ✨ [ View on StackBlitz] ( https://stackblitz.com/edit/ngx-translate-module-loader )
@@ -21,52 +20,36 @@ npm i @larscom/ngx-translate-module-loader
21
20
22
21
## Usage
23
22
24
- Create an exported ` moduleHttpLoaderFactory ` function
23
+ Import the ` provideModuleTranslateLoader ` function and provide the options.
25
24
26
25
``` 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 ))
70
53
```
71
54
72
55
## Namespacing
@@ -76,20 +59,17 @@ By default, each translation file gets it's own namespace based on the `moduleNa
76
59
For example with these options:
77
60
78
61
``` ts
79
- export function moduleHttpLoaderFactory(http : HttpClient ) {
80
- const baseTranslateUrl = ' ./assets/i18n'
62
+ const baseTranslateUrl = ' ./assets/i18n'
81
63
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
+ ]
93
73
}
94
74
```
95
75
@@ -122,7 +102,7 @@ Even though all JSON files from those modules are the same, they don't conflict
122
102
The configuration is very flexible, you can even define custom templates for fetching translations.
123
103
124
104
``` ts
125
- interface IModuleTranslationOptions {
105
+ export interface IModuleTranslationOptions {
126
106
/**
127
107
* The translation module configurations
128
108
*/
@@ -155,7 +135,7 @@ interface IModuleTranslationOptions {
155
135
* Custom translate merge function after retrieving all translation files
156
136
* @param translations the resolved translation files
157
137
*/
158
- translateMerger? : (translations : Translation []) => Translation
138
+ translateMerger? : (translations : TranslationObject []) => TranslationObject
159
139
/**
160
140
* Provide custom headers at 'root' level, which means this headers gets added to every request
161
141
* unless you specify headers at 'module' level.
@@ -166,7 +146,7 @@ interface IModuleTranslationOptions {
166
146
```
167
147
168
148
``` ts
169
- interface IModuleTranslation {
149
+ export interface IModuleTranslation {
170
150
/**
171
151
* The module name
172
152
*
@@ -194,7 +174,7 @@ interface IModuleTranslation {
194
174
* Custom translation map function after retrieving a translation file
195
175
* @param translation the resolved translation file
196
176
*/
197
- translateMap? : (translation : Translation ) => Translation
177
+ translateMap? : (translation : TranslationObject ) => TranslationObject
198
178
/**
199
179
* Custom path template for fetching translations
200
180
* @example
0 commit comments