Skip to content

Commit

Permalink
feat(transloco): 🎸 allow to omit available languages
Browse files Browse the repository at this point in the history
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
  • Loading branch information
json-derulo committed Aug 10, 2023
1 parent 6daa2ea commit b65fce9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
12 changes: 12 additions & 0 deletions libs/transloco/src/lib/tests/service/translate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));
});
2 changes: 1 addition & 1 deletion libs/transloco/src/lib/transloco.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface TranslocoConfig {
prodMode: boolean;
fallbackLang?: string | string[];
failedRetries: number;
availableLangs: AvailableLangs;
availableLangs?: AvailableLangs;
flatten: {
aot: boolean;
};
Expand Down
28 changes: 20 additions & 8 deletions libs/transloco/src/lib/transloco.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class TranslocoService implements OnDestroy {
private cache = new Map<string, Observable<Translation>>();
private firstFallbackLang: string | undefined;
private defaultLang = '';
private availableLangs: AvailableLangs = [];
private availableLangs?: AvailableLangs;
private isResolvedMissingOnce = false;
private lang: BehaviorSubject<string>;
private failedLangs = new Set<string>();
Expand All @@ -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<string>(this.getDefaultLang());
Expand Down Expand Up @@ -169,7 +169,7 @@ export class TranslocoService implements OnDestroy {
return this;
}

setAvailableLangs(langs: AvailableLangs) {
setAvailableLangs(langs: AvailableLangs | undefined) {
this.availableLangs = langs;
}

Expand All @@ -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<Translation> {
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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[];
Expand Down
2 changes: 1 addition & 1 deletion libs/transloco/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b65fce9

Please sign in to comment.