Skip to content

Commit a13b700

Browse files
authored
feat(service): add option to set default language in root config (#1105)
1 parent 7241c86 commit a13b700

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,25 @@ export function createTranslateLoader(http: HttpClient) {
204204
export class AppModule { }
205205
```
206206

207-
#### 2. Init the `TranslateService` for your application:
207+
#### 2. Define the `default language` for the application
208+
209+
```ts
210+
@NgModule({
211+
imports: [
212+
BrowserModule,
213+
TranslateModule.forRoot({
214+
defaultLanguage: 'en'
215+
})
216+
],
217+
providers: [
218+
219+
],
220+
bootstrap: [AppComponent]
221+
})
222+
export class AppModule { }
223+
```
224+
225+
#### 3. Init the `TranslateService` for your application:
208226

209227
```ts
210228
import {Component} from '@angular/core';
@@ -229,7 +247,7 @@ export class AppComponent {
229247
}
230248
```
231249

232-
#### 3. Define the translations:
250+
#### 4. Define the translations:
233251

234252
Once you've imported the `TranslateModule`, you can put your translations in a json file that will be imported with the `TranslateHttpLoader`. The following translations should be stored in `en.json`.
235253

@@ -259,7 +277,7 @@ The `TranslateParser` understands nested JSON objects. This means that you can h
259277

260278
You can then access the value by using the dot notation, in this case `HOME.HELLO`.
261279

262-
#### 4. Use the service, the pipe or the directive:
280+
#### 5. Use the service, the pipe or the directive:
263281

264282
You can either use the `TranslateService`, the `TranslatePipe` or the `TranslateDirective` to get your translation values.
265283

@@ -326,7 +344,7 @@ Or even simpler using the content of your element as a key:
326344
<div translate [translateParams]="{value: 'world'}">HELLO</div>
327345
```
328346

329-
#### 5. Use HTML tags:
347+
#### 6. Use HTML tags:
330348

331349
You can easily use raw HTML tags within your translations.
332350

projects/ngx-translate/core/src/lib/translate.service.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {isDefined, mergeDeep} from "./util";
1111

1212
export const USE_STORE = new InjectionToken<string>('USE_STORE');
1313
export const USE_DEFAULT_LANG = new InjectionToken<string>('USE_DEFAULT_LANG');
14+
export const DEFAULT_LANGUAGE = new InjectionToken<string>('DEFAULT_LANGUAGE');
1415
export const USE_EXTEND = new InjectionToken<string>('USE_EXTEND');
1516

1617
export interface TranslationChangeEvent {
@@ -144,8 +145,10 @@ export class TranslateService {
144145
* @param compiler An instance of the compiler currently used
145146
* @param parser An instance of the parser currently used
146147
* @param missingTranslationHandler A handler for missing translations.
147-
* @param isolate whether this service should use the store or not
148148
* @param useDefaultLang whether we should use default language translation when current language translation is missing.
149+
* @param isolate whether this service should use the store or not
150+
* @param extend To make a child module extend (and use) translations from parent modules.
151+
* @param defaultLanguage Set the default language using configuration
149152
*/
150153
constructor(public store: TranslateStore,
151154
public currentLoader: TranslateLoader,
@@ -154,7 +157,12 @@ export class TranslateService {
154157
public missingTranslationHandler: MissingTranslationHandler,
155158
@Inject(USE_DEFAULT_LANG) private useDefaultLang: boolean = true,
156159
@Inject(USE_STORE) private isolate: boolean = false,
157-
@Inject(USE_EXTEND) private extend: boolean = false) {
160+
@Inject(USE_EXTEND) private extend: boolean = false,
161+
@Inject(DEFAULT_LANGUAGE) defaultLanguage: string) {
162+
/** set the default language from configuration */
163+
if (defaultLanguage) {
164+
this.setDefaultLang(defaultLanguage);
165+
}
158166
}
159167

160168
/**

projects/ngx-translate/core/src/public_api.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import {NgModule, ModuleWithProviders, Provider} from "@angular/core";
22
import {TranslateLoader, TranslateFakeLoader} from "./lib/translate.loader";
3-
import {TranslateService} from "./lib/translate.service";
43
import {MissingTranslationHandler, FakeMissingTranslationHandler} from "./lib/missing-translation-handler";
54
import {TranslateParser, TranslateDefaultParser} from "./lib/translate.parser";
65
import {TranslateCompiler, TranslateFakeCompiler} from "./lib/translate.compiler";
76
import {TranslateDirective} from "./lib/translate.directive";
87
import {TranslatePipe} from "./lib/translate.pipe";
98
import {TranslateStore} from "./lib/translate.store";
10-
import {USE_STORE} from "./lib/translate.service";
11-
import {USE_EXTEND} from "./lib/translate.service";
12-
import {USE_DEFAULT_LANG} from "./lib/translate.service";
9+
import {USE_DEFAULT_LANG, DEFAULT_LANGUAGE, USE_STORE, TranslateService, USE_EXTEND} from "./lib/translate.service";
1310

1411
export * from "./lib/translate.loader";
1512
export * from "./lib/translate.service";
@@ -30,6 +27,7 @@ export interface TranslateModuleConfig {
3027
// extends translations for a given language instead of ignoring them if present
3128
extend?: boolean;
3229
useDefaultLang?: boolean;
30+
defaultLanguage?: string;
3331
}
3432

3533
@NgModule({
@@ -58,6 +56,7 @@ export class TranslateModule {
5856
{provide: USE_STORE, useValue: config.isolate},
5957
{provide: USE_DEFAULT_LANG, useValue: config.useDefaultLang},
6058
{provide: USE_EXTEND, useValue: config.extend},
59+
{provide: DEFAULT_LANGUAGE, useValue: config.defaultLanguage},
6160
TranslateService
6261
]
6362
};
@@ -77,6 +76,7 @@ export class TranslateModule {
7776
{provide: USE_STORE, useValue: config.isolate},
7877
{provide: USE_DEFAULT_LANG, useValue: config.useDefaultLang},
7978
{provide: USE_EXTEND, useValue: config.extend},
79+
{provide: DEFAULT_LANGUAGE, useValue: config.defaultLanguage},
8080
TranslateService
8181
]
8282
};

0 commit comments

Comments
 (0)