Skip to content

Commit b65fce9

Browse files
committed
feat(transloco): 🎸 allow to omit available languages
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: jsverse#653
1 parent 6daa2ea commit b65fce9

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

libs/transloco/src/lib/tests/service/translate.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,16 @@ describe('translate', () => {
7979
'Admin Lazy spanish'
8080
);
8181
}));
82+
83+
it('should return the translation when availableLangs is skipped', fakeAsync(() => {
84+
service.setAvailableLangs(undefined);
85+
loadLang(service);
86+
expect(service.translate('home')).toEqual(mockLangs['en'].home);
87+
}));
88+
89+
it('should return the translation when availableLangs is set to null', fakeAsync(() => {
90+
service.setAvailableLangs(null);
91+
loadLang(service);
92+
expect(service.translate('home')).toEqual(mockLangs['en'].home);
93+
}));
8294
});

libs/transloco/src/lib/transloco.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface TranslocoConfig {
88
prodMode: boolean;
99
fallbackLang?: string | string[];
1010
failedRetries: number;
11-
availableLangs: AvailableLangs;
11+
availableLangs?: AvailableLangs;
1212
flatten: {
1313
aot: boolean;
1414
};

libs/transloco/src/lib/transloco.service.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class TranslocoService implements OnDestroy {
102102
private cache = new Map<string, Observable<Translation>>();
103103
private firstFallbackLang: string | undefined;
104104
private defaultLang = '';
105-
private availableLangs: AvailableLangs = [];
105+
private availableLangs?: AvailableLangs;
106106
private isResolvedMissingOnce = false;
107107
private lang: BehaviorSubject<string>;
108108
private failedLangs = new Set<string>();
@@ -129,7 +129,7 @@ export class TranslocoService implements OnDestroy {
129129
service = this;
130130
this.config = structuredClone(userConfig);
131131

132-
this.setAvailableLangs(this.config.availableLangs || []);
132+
this.setAvailableLangs(this.config.availableLangs);
133133
this.setFallbackLangForMissingTranslation(this.config);
134134
this.setDefaultLang(this.config.defaultLang);
135135
this.lang = new BehaviorSubject<string>(this.getDefaultLang());
@@ -169,7 +169,7 @@ export class TranslocoService implements OnDestroy {
169169
return this;
170170
}
171171

172-
setAvailableLangs(langs: AvailableLangs) {
172+
setAvailableLangs(langs: AvailableLangs | undefined) {
173173
this.availableLangs = langs;
174174
}
175175

@@ -181,7 +181,7 @@ export class TranslocoService implements OnDestroy {
181181
* depending on how the available languages are set in your module.
182182
*/
183183
getAvailableLangs() {
184-
return this.availableLangs;
184+
return this.availableLangs ?? [];
185185
}
186186

187187
load(path: string, options: LoadOptions = {}): Observable<Translation> {
@@ -626,7 +626,11 @@ export class TranslocoService implements OnDestroy {
626626
* @internal
627627
*/
628628
_isLangScoped(lang: string) {
629-
return this.getAvailableLangsIds().indexOf(lang) === -1;
629+
const availableLangsIds = this.getAvailableLangsIds();
630+
if (!availableLangsIds) {
631+
return true;
632+
}
633+
return availableLangsIds.indexOf(lang) === -1;
630634
}
631635

632636
/**
@@ -636,7 +640,11 @@ export class TranslocoService implements OnDestroy {
636640
* False if the given string is not an available language.
637641
*/
638642
isLang(lang: string): boolean {
639-
return this.getAvailableLangsIds().indexOf(lang) !== -1;
643+
const availableLangsIds = this.getAvailableLangsIds();
644+
if (!availableLangsIds) {
645+
return true;
646+
}
647+
return availableLangsIds.indexOf(lang) !== -1;
640648
}
641649

642650
/**
@@ -701,8 +709,12 @@ export class TranslocoService implements OnDestroy {
701709
return size(this.getTranslation(lang));
702710
}
703711

704-
private getAvailableLangsIds(): string[] {
705-
const first = this.getAvailableLangs()[0];
712+
private getAvailableLangsIds(): string[] | null {
713+
const first = this.getAvailableLangs()?.[0];
714+
715+
if (isNil(first)) {
716+
return null;
717+
}
706718

707719
if (isString(first)) {
708720
return this.getAvailableLangs() as string[];

libs/transloco/src/lib/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export interface LangDefinition {
3030
id: string;
3131
label: string;
3232
}
33-
export type AvailableLangs = string[] | LangDefinition[];
33+
export type AvailableLangs = string[] | LangDefinition[] | null;
3434
export interface SetTranslationOptions {
3535
merge?: boolean;
3636
emitChange?: boolean;

0 commit comments

Comments
 (0)