From 3e2af8929f7c714f7bc3ace1f336ae5756d55994 Mon Sep 17 00:00:00 2001 From: Josh Weisman Date: Wed, 5 Feb 2020 14:29:27 +0200 Subject: [PATCH 1/2] Use lazy loading import for i18n files Solution based on https://github.com/ngx-translate/http-loader/issues/25#issuecomment-514056865 --- angular-lib/src/angular/modules.ts | 4 ++-- .../src/angular/modules/translate.module.ts | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/angular-lib/src/angular/modules.ts b/angular-lib/src/angular/modules.ts index d2f1c3b..595aeb8 100644 --- a/angular-lib/src/angular/modules.ts +++ b/angular-lib/src/angular/modules.ts @@ -1,4 +1,4 @@ import { MaterialModule } from './modules/material.module'; -import { getTranslateModule } from './modules/translate.module'; +import { getTranslateModule, LazyTranslateLoader } from './modules/translate.module'; -export { MaterialModule, getTranslateModule }; +export { MaterialModule, getTranslateModule, LazyTranslateLoader }; diff --git a/angular-lib/src/angular/modules/translate.module.ts b/angular-lib/src/angular/modules/translate.module.ts index 183dd93..e46414a 100644 --- a/angular-lib/src/angular/modules/translate.module.ts +++ b/angular-lib/src/angular/modules/translate.module.ts @@ -1,17 +1,18 @@ import { TranslateModule, TranslateLoader } from '@ngx-translate/core'; -import { TranslateHttpLoader } from '@ngx-translate/http-loader'; -import { HttpClient } from '@angular/common/http'; +import { Observable, from, of } from 'rxjs'; +import { catchError } from 'rxjs/operators' -export function createTranslateLoader(http: HttpClient) { - return new TranslateHttpLoader(http, './assets/i18n/', '.json'); -} +export class LazyTranslateLoader implements TranslateLoader { + getTranslation(lang: string): Observable { + return from(import(`../../../../.ng/src/assets/i18n/${lang}.json`)).pipe(catchError(err=>of({}))); + } + } export function getTranslateModule() { return TranslateModule.forRoot({ loader: { provide: TranslateLoader, - useFactory: (createTranslateLoader), - deps: [HttpClient] + useClass: (LazyTranslateLoader) } }); } From 7e7915c7835900332d5f2e8758f0c613e5e62cad Mon Sep 17 00:00:00 2001 From: Josh Weisman Date: Thu, 6 Feb 2020 09:45:19 +0200 Subject: [PATCH 2/2] Removed strings from assets and removed LazyTranslateLoader export --- angular-lib/src/angular/modules.ts | 4 ++-- angular-lib/src/angular/modules/translate.module.ts | 2 +- base/base/.ng/src/{assets => }/i18n/.keep | 0 cli/scripts/extract-labels.js | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename base/base/.ng/src/{assets => }/i18n/.keep (100%) diff --git a/angular-lib/src/angular/modules.ts b/angular-lib/src/angular/modules.ts index 595aeb8..d2f1c3b 100644 --- a/angular-lib/src/angular/modules.ts +++ b/angular-lib/src/angular/modules.ts @@ -1,4 +1,4 @@ import { MaterialModule } from './modules/material.module'; -import { getTranslateModule, LazyTranslateLoader } from './modules/translate.module'; +import { getTranslateModule } from './modules/translate.module'; -export { MaterialModule, getTranslateModule, LazyTranslateLoader }; +export { MaterialModule, getTranslateModule }; diff --git a/angular-lib/src/angular/modules/translate.module.ts b/angular-lib/src/angular/modules/translate.module.ts index e46414a..af7ca69 100644 --- a/angular-lib/src/angular/modules/translate.module.ts +++ b/angular-lib/src/angular/modules/translate.module.ts @@ -4,7 +4,7 @@ import { catchError } from 'rxjs/operators' export class LazyTranslateLoader implements TranslateLoader { getTranslation(lang: string): Observable { - return from(import(`../../../../.ng/src/assets/i18n/${lang}.json`)).pipe(catchError(err=>of({}))); + return from(import(`../../../../.ng/src/i18n/${lang}.json`)).pipe(catchError(err=>of({}))); } } diff --git a/base/base/.ng/src/assets/i18n/.keep b/base/base/.ng/src/i18n/.keep similarity index 100% rename from base/base/.ng/src/assets/i18n/.keep rename to base/base/.ng/src/i18n/.keep diff --git a/cli/scripts/extract-labels.js b/cli/scripts/extract-labels.js index db49fc0..20314f4 100644 --- a/cli/scripts/extract-labels.js +++ b/cli/scripts/extract-labels.js @@ -7,7 +7,7 @@ const {cwd, appBaseDir, workNg, work} = require("../lib/dirs"); syncNgDir() .then(() => extractLabels([ `--input`, `${work}/src`, - `--output`, `${work}/src/assets/i18n/labels.json`, + `--output`, `${work}/src/i18n/labels.json`, `--clean`,`--sort`, `--format`, `namespaced-json` ])) .catch(error => {