From 5a414b8f45cbf9b4886ef27111dc13797ae555ad Mon Sep 17 00:00:00 2001 From: alecarn <133774929+alecarn@users.noreply.github.com> Date: Wed, 27 Dec 2023 09:42:43 -0500 Subject: [PATCH] fic(core): translatemodule forRoot is now called once (#1551) * feat: update Angular v17 * fix(geo): do not upgrade flexsearch since it seem not compatible with TS https://github.com/nextapps-de/flexsearch/issues/342 * fix(core/language): TranslateModule call forRoot only once with provideRootTranslation - IgoLanguageModule only export the TranslateModule BREAKING CHANGE: IgoLanguageModule don't import TranslateModule.forRoot defaultLanguageLoader is not exported anymore - provideDefaultLanguageLoader and provideLanguageLoader are replaced by DEFAULT_LANGUAGE_LOADER and set directly inside the TranslationConfig * fix(auth): test --- .../auth-monitoring.service.spec.ts | 15 +++-- .../auth/src/lib/shared/auth.service.spec.ts | 17 +++++- packages/core/src/lib/core.module.ts | 3 +- .../core/src/lib/language/language.module.ts | 28 +-------- .../core/src/lib/language/shared/index.ts | 1 + .../lib/language/shared/language.provider.ts | 58 +++++++++++++------ packages/core/src/public_api.ts | 1 - .../src/app/common/dialog/dialog.module.ts | 2 +- .../src/app/core/language/language.module.ts | 6 +- .../monitoring/monitoring.component.spec.ts | 6 +- .../src/app/core/request/request.module.ts | 2 +- 11 files changed, 77 insertions(+), 62 deletions(-) diff --git a/packages/auth/src/lib/auth-monitoring/auth-monitoring.service.spec.ts b/packages/auth/src/lib/auth-monitoring/auth-monitoring.service.spec.ts index 3cbdb4b55a..0e6e571119 100644 --- a/packages/auth/src/lib/auth-monitoring/auth-monitoring.service.spec.ts +++ b/packages/auth/src/lib/auth-monitoring/auth-monitoring.service.spec.ts @@ -4,9 +4,9 @@ import { TestBed } from '@angular/core/testing'; import { AnyMonitoringOptions, ConfigService, - IgoLanguageModule, MONITORING_OPTIONS, - MessageService + MessageService, + provideMockTranslation } from '@igo2/core'; import { ToastrModule, ToastrService } from 'ngx-toastr'; @@ -20,11 +20,16 @@ const initialize = ( options: AnyMonitoringOptions = MOCK_MONITORING_OPTIONS ) => { TestBed.configureTestingModule({ - imports: [HttpClientModule, IgoAuthModule, IgoLanguageModule, ToastrModule], + imports: [ + HttpClientModule, + IgoAuthModule, + provideMockTranslation(), + ToastrModule + ], providers: [ { provide: MONITORING_OPTIONS, useValue: options }, - { provide: ToastrService, useValue: ToastrService }, - { provide: MessageService, useValue: MessageService } + ToastrService, + MessageService ] }); diff --git a/packages/auth/src/lib/shared/auth.service.spec.ts b/packages/auth/src/lib/shared/auth.service.spec.ts index b974c0791a..6da87a4c32 100644 --- a/packages/auth/src/lib/shared/auth.service.spec.ts +++ b/packages/auth/src/lib/shared/auth.service.spec.ts @@ -1,15 +1,26 @@ import { HttpClientModule } from '@angular/common/http'; import { TestBed, inject } from '@angular/core/testing'; -import { IgoLanguageModule, IgoMessageModule } from '@igo2/core'; +import { + IgoLanguageModule, + IgoMessageModule, + provideMockTranslation +} from '@igo2/core'; + +import { TranslateService } from '@ngx-translate/core'; import { AuthService } from './auth.service'; describe('AuthService', () => { beforeEach(() => { TestBed.configureTestingModule({ - imports: [HttpClientModule, IgoLanguageModule, IgoMessageModule], - providers: [AuthService] + imports: [ + HttpClientModule, + provideMockTranslation(), + IgoLanguageModule, + IgoMessageModule + ], + providers: [AuthService, TranslateService] }); }); diff --git a/packages/core/src/lib/core.module.ts b/packages/core/src/lib/core.module.ts index 06fa5b161c..dbb14362d1 100644 --- a/packages/core/src/lib/core.module.ts +++ b/packages/core/src/lib/core.module.ts @@ -8,6 +8,7 @@ import { DBConfig, NgxIndexedDBModule } from 'ngx-indexed-db'; import { IgoActivityModule } from './activity/activity.module'; import { IgoConfigModule } from './config/config.module'; +import { provideRootTranslation } from './language'; import { IgoLanguageModule } from './language/language.module'; import { IgoMessageModule } from './message/message.module'; import { IgoErrorModule } from './request/error.module'; @@ -48,7 +49,7 @@ const dbConfig: DBConfig = { IgoActivityModule.forRoot(), IgoConfigModule.forRoot(), IgoErrorModule.forRoot(), - IgoLanguageModule.forRoot(), + provideRootTranslation(), IgoMessageModule.forRoot(), NgxIndexedDBModule.forRoot(dbConfig) ], diff --git a/packages/core/src/lib/language/language.module.ts b/packages/core/src/lib/language/language.module.ts index 38ef06de89..16849e2807 100644 --- a/packages/core/src/lib/language/language.module.ts +++ b/packages/core/src/lib/language/language.module.ts @@ -1,30 +1,8 @@ -import { ModuleWithProviders, NgModule } from '@angular/core'; +import { NgModule } from '@angular/core'; -import { - MissingTranslationHandler, - TranslateModule -} from '@ngx-translate/core'; - -import { provideDefaultLanguageLoader } from './shared/language.provider'; -import { IgoMissingTranslationHandler } from './shared/missing-translation.guard'; +import { TranslateModule } from '@ngx-translate/core'; @NgModule({ - imports: [ - TranslateModule.forRoot({ - missingTranslationHandler: { - provide: MissingTranslationHandler, - useClass: IgoMissingTranslationHandler - } - }) - ], - declarations: [], exports: [TranslateModule] }) -export class IgoLanguageModule { - static forRoot(): ModuleWithProviders { - return { - ngModule: IgoLanguageModule, - providers: [provideDefaultLanguageLoader()] - }; - } -} +export class IgoLanguageModule {} diff --git a/packages/core/src/lib/language/shared/index.ts b/packages/core/src/lib/language/shared/index.ts index 6c68119691..df283d3090 100644 --- a/packages/core/src/lib/language/shared/index.ts +++ b/packages/core/src/lib/language/shared/index.ts @@ -1,4 +1,5 @@ export * from './language.interface'; export * from './language.loader'; +export * from './language.provider'; export * from './language.service'; export * from './missing-translation.guard'; diff --git a/packages/core/src/lib/language/shared/language.provider.ts b/packages/core/src/lib/language/shared/language.provider.ts index 908d44fea8..f31d23fd1c 100644 --- a/packages/core/src/lib/language/shared/language.provider.ts +++ b/packages/core/src/lib/language/shared/language.provider.ts @@ -1,29 +1,49 @@ import { HttpBackend } from '@angular/common/http'; +import { ImportProvidersSource, Provider } from '@angular/core'; -import { TranslateLoader } from '@ngx-translate/core'; +import { + MissingTranslationHandler, + TranslateLoader, + TranslateModule, + TranslateModuleConfig +} from '@ngx-translate/core'; -import { ConfigService } from '../../config/config.service'; +import { ConfigService } from '../../config'; import { LanguageLoader } from './language.loader'; +import { IgoMissingTranslationHandler } from './missing-translation.guard'; -export function defaultLanguageLoader( - http: HttpBackend, - config?: ConfigService -) { - return new LanguageLoader(http, undefined, undefined, config); +/** + * Make sure you only call this method in the root module of your application, most of the time called AppModule. + */ +export function provideRootTranslation( + loader?: Provider +): ImportProvidersSource { + return TranslateModule.forRoot(setTranslationConfig(loader)); } -export function provideLanguageLoader(loader?) { - return { - provide: TranslateLoader, - useFactory: loader || defaultLanguageLoader, - deps: [HttpBackend] - }; +export function provideMockTranslation( + loader?: Provider +): ImportProvidersSource { + return TranslateModule.forRoot(); } -export function provideDefaultLanguageLoader(loader?) { - return { - provide: TranslateLoader, - useFactory: loader || defaultLanguageLoader, - deps: [HttpBackend, ConfigService] - }; +export const setTranslationConfig = ( + loader?: Provider +): TranslateModuleConfig => ({ + defaultLanguage: 'en', + loader: loader ?? DEFAULT_LANGUAGE_LOADER, + missingTranslationHandler: { + provide: MissingTranslationHandler, + useClass: IgoMissingTranslationHandler + } +}); + +export const DEFAULT_LANGUAGE_LOADER: Provider = { + provide: TranslateLoader, + useFactory: defaultLanguageLoader, + deps: [HttpBackend, ConfigService] +}; + +function defaultLanguageLoader(http: HttpBackend, config?: ConfigService) { + return new LanguageLoader(http, undefined, undefined, config); } diff --git a/packages/core/src/public_api.ts b/packages/core/src/public_api.ts index d9827d65a1..61bc0f03da 100644 --- a/packages/core/src/public_api.ts +++ b/packages/core/src/public_api.ts @@ -13,7 +13,6 @@ export * from './lib/request/logging.module'; export * from './lib/config/config.provider'; export * from './lib/gesture/gesture.provider'; -export * from './lib/language/shared/language.provider'; export * from './lib/route/route.service'; export * from './lib/route/route.interface'; diff --git a/projects/demo/src/app/common/dialog/dialog.module.ts b/projects/demo/src/app/common/dialog/dialog.module.ts index ed43752882..4b93ec1630 100644 --- a/projects/demo/src/app/common/dialog/dialog.module.ts +++ b/projects/demo/src/app/common/dialog/dialog.module.ts @@ -24,7 +24,7 @@ import { AppDialogComponent } from './dialog.component'; IgoFormDialogModule, IgoJsonDialogModule, MatButtonModule, - IgoLanguageModule.forRoot(), + IgoLanguageModule, MatDividerModule ], exports: [AppDialogComponent] diff --git a/projects/demo/src/app/core/language/language.module.ts b/projects/demo/src/app/core/language/language.module.ts index f84e0d1390..431db721a4 100644 --- a/projects/demo/src/app/core/language/language.module.ts +++ b/projects/demo/src/app/core/language/language.module.ts @@ -8,11 +8,7 @@ import { AppLanguageComponent } from './language.component'; @NgModule({ declarations: [AppLanguageComponent], - imports: [ - AppLanguageRoutingModule, - SharedModule, - IgoLanguageModule.forRoot() - ], + imports: [AppLanguageRoutingModule, SharedModule, IgoLanguageModule], exports: [AppLanguageComponent] }) export class AppLanguageModule {} diff --git a/projects/demo/src/app/core/monitoring/monitoring.component.spec.ts b/projects/demo/src/app/core/monitoring/monitoring.component.spec.ts index 59e8da17af..a19b065b27 100644 --- a/projects/demo/src/app/core/monitoring/monitoring.component.spec.ts +++ b/projects/demo/src/app/core/monitoring/monitoring.component.spec.ts @@ -1,15 +1,19 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { MonitoringComponent } from './monitoring.component'; import { MONITORING_OPTIONS } from '@igo2/core'; + import { MOCK_MONITORING_OPTIONS } from 'packages/core/src/lib/monitoring/__mocks__/monitoring-mock'; +import { SharedModule } from '../../shared/shared.module'; +import { MonitoringComponent } from './monitoring.component'; + describe('MonitoringComponent', () => { let component: MonitoringComponent; let fixture: ComponentFixture; beforeEach(() => { TestBed.configureTestingModule({ + imports: [SharedModule], declarations: [MonitoringComponent], providers: [ { provide: MONITORING_OPTIONS, useValue: MOCK_MONITORING_OPTIONS } diff --git a/projects/demo/src/app/core/request/request.module.ts b/projects/demo/src/app/core/request/request.module.ts index 5d9e38f9ac..17c811ebb8 100644 --- a/projects/demo/src/app/core/request/request.module.ts +++ b/projects/demo/src/app/core/request/request.module.ts @@ -13,7 +13,7 @@ import { AppRequestComponent } from './request.component'; AppRequestRoutingModule, SharedModule, HttpClientModule, - IgoLanguageModule.forRoot(), + IgoLanguageModule, IgoErrorModule.forRoot() // Only if you want register errors from http call in console // IgoLoggingModule.forRoot() // Only if you want register http calls in console ],