From 840c6d146ecf72b8dbf7e200e0c188f92f36c133 Mon Sep 17 00:00:00 2001 From: Tyson Paul Date: Fri, 1 Mar 2019 15:00:40 +0530 Subject: [PATCH] Language translation code cleanup --- .../language-translation.module.ts | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/app/shared/modules/language-translation/language-translation.module.ts diff --git a/src/app/shared/modules/language-translation/language-translation.module.ts b/src/app/shared/modules/language-translation/language-translation.module.ts new file mode 100644 index 000000000..e76688dab --- /dev/null +++ b/src/app/shared/modules/language-translation/language-translation.module.ts @@ -0,0 +1,47 @@ +/** + * This module is used to language translations. + * The translations are saved in a json file in /src/app/assets/i18n directory + * Docs: https://www.codeandweb.com/babeledit/tutorials/how-to-translate-your-angular7-app-with-ngx-translate + */ +import { NgModule } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; + +// import ngx-translate and the http loader +import { + TranslateLoader, + TranslateModule, + TranslateService +} from '@ngx-translate/core'; +import { TranslateHttpLoader } from '@ngx-translate/http-loader'; + +// ngx-translate - required for AOT compilation +export function HttpLoaderFactory(http: HttpClient) { + return new TranslateHttpLoader(http); +} + +@NgModule({ + declarations: [], + imports: [ + TranslateModule.forRoot({ + loader: { + provide: TranslateLoader, + useFactory: HttpLoaderFactory, + deps: [HttpClient] + } + }) + ], + exports: [ + TranslateModule + ], +}) +export class LanguageTranslationModule { + constructor( + private translate: TranslateService, + ) { + // Gets Default language from browser if available, otherwise set English ad default + this.translate.addLangs(['en', 'fr', 'ur', 'es', 'it', 'fa', 'de', 'zh-CHS']); + this.translate.setDefaultLang('en'); + const browserLang = this.translate.getBrowserLang(); + this.translate.use(browserLang.match(/en|fr|ur|es|it|fa|de|zh-CHS/) ? browserLang : 'en'); + } +}