Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1550 translatemodule forRoot should be called only once #1551

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
]
});

Expand Down
17 changes: 14 additions & 3 deletions packages/auth/src/lib/shared/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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]
});
});

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/lib/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -48,7 +49,7 @@ const dbConfig: DBConfig = {
IgoActivityModule.forRoot(),
IgoConfigModule.forRoot(),
IgoErrorModule.forRoot(),
IgoLanguageModule.forRoot(),
provideRootTranslation(),
IgoMessageModule.forRoot(),
NgxIndexedDBModule.forRoot(dbConfig)
],
Expand Down
28 changes: 3 additions & 25 deletions packages/core/src/lib/language/language.module.ts
Original file line number Diff line number Diff line change
@@ -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<IgoLanguageModule> {
return {
ngModule: IgoLanguageModule,
providers: [provideDefaultLanguageLoader()]
};
}
}
export class IgoLanguageModule {}
1 change: 1 addition & 0 deletions packages/core/src/lib/language/shared/index.ts
Original file line number Diff line number Diff line change
@@ -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';
58 changes: 39 additions & 19 deletions packages/core/src/lib/language/shared/language.provider.ts
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 0 additions & 1 deletion packages/core/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
2 changes: 1 addition & 1 deletion projects/demo/src/app/common/dialog/dialog.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { AppDialogComponent } from './dialog.component';
IgoFormDialogModule,
IgoJsonDialogModule,
MatButtonModule,
IgoLanguageModule.forRoot(),
IgoLanguageModule,
MatDividerModule
],
exports: [AppDialogComponent]
Expand Down
6 changes: 1 addition & 5 deletions projects/demo/src/app/core/language/language.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Original file line number Diff line number Diff line change
@@ -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<MonitoringComponent>;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [SharedModule],
declarations: [MonitoringComponent],
providers: [
{ provide: MONITORING_OPTIONS, useValue: MOCK_MONITORING_OPTIONS }
Expand Down
2 changes: 1 addition & 1 deletion projects/demo/src/app/core/request/request.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
],
Expand Down