diff --git a/libs/transloco/src/lib/tests/service/translate.spec.ts b/libs/transloco/src/lib/tests/service/translate.spec.ts index 88255352b..0c234f5b6 100644 --- a/libs/transloco/src/lib/tests/service/translate.spec.ts +++ b/libs/transloco/src/lib/tests/service/translate.spec.ts @@ -79,4 +79,16 @@ describe('translate', () => { 'Admin Lazy spanish' ); })); + + it('should return the translation when availableLangs is skipped', fakeAsync(() => { + service.setAvailableLangs(undefined); + loadLang(service); + expect(service.translate('home')).toEqual(mockLangs['en'].home); + })); + + it('should return the translation when availableLangs is set to null', fakeAsync(() => { + service.setAvailableLangs(null); + loadLang(service); + expect(service.translate('home')).toEqual(mockLangs['en'].home); + })); }); diff --git a/libs/transloco/src/lib/transloco.config.ts b/libs/transloco/src/lib/transloco.config.ts index 35a803763..f5311b204 100644 --- a/libs/transloco/src/lib/transloco.config.ts +++ b/libs/transloco/src/lib/transloco.config.ts @@ -8,7 +8,7 @@ export interface TranslocoConfig { prodMode: boolean; fallbackLang?: string | string[]; failedRetries: number; - availableLangs: AvailableLangs; + availableLangs?: AvailableLangs; flatten: { aot: boolean; }; diff --git a/libs/transloco/src/lib/transloco.service.ts b/libs/transloco/src/lib/transloco.service.ts index 1e13e1d4d..9b8bc3edb 100644 --- a/libs/transloco/src/lib/transloco.service.ts +++ b/libs/transloco/src/lib/transloco.service.ts @@ -102,7 +102,7 @@ export class TranslocoService implements OnDestroy { private cache = new Map>(); private firstFallbackLang: string | undefined; private defaultLang = ''; - private availableLangs: AvailableLangs = []; + private availableLangs?: AvailableLangs; private isResolvedMissingOnce = false; private lang: BehaviorSubject; private failedLangs = new Set(); @@ -129,7 +129,7 @@ export class TranslocoService implements OnDestroy { service = this; this.config = structuredClone(userConfig); - this.setAvailableLangs(this.config.availableLangs || []); + this.setAvailableLangs(this.config.availableLangs); this.setFallbackLangForMissingTranslation(this.config); this.setDefaultLang(this.config.defaultLang); this.lang = new BehaviorSubject(this.getDefaultLang()); @@ -169,7 +169,7 @@ export class TranslocoService implements OnDestroy { return this; } - setAvailableLangs(langs: AvailableLangs) { + setAvailableLangs(langs: AvailableLangs | undefined) { this.availableLangs = langs; } @@ -181,7 +181,7 @@ export class TranslocoService implements OnDestroy { * depending on how the available languages are set in your module. */ getAvailableLangs() { - return this.availableLangs; + return this.availableLangs ?? []; } load(path: string, options: LoadOptions = {}): Observable { @@ -626,7 +626,11 @@ export class TranslocoService implements OnDestroy { * @internal */ _isLangScoped(lang: string) { - return this.getAvailableLangsIds().indexOf(lang) === -1; + const availableLangsIds = this.getAvailableLangsIds(); + if (!availableLangsIds) { + return true; + } + return availableLangsIds.indexOf(lang) === -1; } /** @@ -636,7 +640,11 @@ export class TranslocoService implements OnDestroy { * False if the given string is not an available language. */ isLang(lang: string): boolean { - return this.getAvailableLangsIds().indexOf(lang) !== -1; + const availableLangsIds = this.getAvailableLangsIds(); + if (!availableLangsIds) { + return true; + } + return availableLangsIds.indexOf(lang) !== -1; } /** @@ -701,8 +709,12 @@ export class TranslocoService implements OnDestroy { return size(this.getTranslation(lang)); } - private getAvailableLangsIds(): string[] { - const first = this.getAvailableLangs()[0]; + private getAvailableLangsIds(): string[] | null { + const first = this.getAvailableLangs()?.[0]; + + if (isNil(first)) { + return null; + } if (isString(first)) { return this.getAvailableLangs() as string[]; diff --git a/libs/transloco/src/lib/types.ts b/libs/transloco/src/lib/types.ts index 7ef9f37c1..0248b6b45 100644 --- a/libs/transloco/src/lib/types.ts +++ b/libs/transloco/src/lib/types.ts @@ -30,7 +30,7 @@ export interface LangDefinition { id: string; label: string; } -export type AvailableLangs = string[] | LangDefinition[]; +export type AvailableLangs = string[] | LangDefinition[] | null; export interface SetTranslationOptions { merge?: boolean; emitChange?: boolean;