From b65fce9fd0c5e0152f625d01e8bddaf9c6d8d030 Mon Sep 17 00:00:00 2001 From: Daniel Kimmich Date: Thu, 10 Aug 2023 19:27:06 +0200 Subject: [PATCH] =?UTF-8?q?feat(transloco):=20=F0=9F=8E=B8=20allow=20to=20?= =?UTF-8?q?omit=20available=20languages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently the available languages need to be set, otherwise the translated values won't be returned. With this change the translation will also work when omitting the availableLangs config, or setting it to null. ✅ Closes: #653 --- .../src/lib/tests/service/translate.spec.ts | 12 ++++++++ libs/transloco/src/lib/transloco.config.ts | 2 +- libs/transloco/src/lib/transloco.service.ts | 28 +++++++++++++------ libs/transloco/src/lib/types.ts | 2 +- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/libs/transloco/src/lib/tests/service/translate.spec.ts b/libs/transloco/src/lib/tests/service/translate.spec.ts index 88255352..0c234f5b 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 35a80376..f5311b20 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 1e13e1d4..9b8bc3ed 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 7ef9f37c..0248b6b4 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;