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

feat(transloco): 🎸 allow to omit available languages #689

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions docs/docs/getting-started/config-options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ translocoConfig({
availableLangs: ['en', 'es']
})
```
When the active language is not included in the `availableLangs`, the translations of this language will not be returned.
You can omit this option or set it to `null`, in this case the translations will be returned for any language, if available.

### `missingHandler.allowEmpty`
Whether to allow empty values: (defaults to `false`)
Expand Down
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 @@
prodMode: boolean;
fallbackLang?: string | string[];
failedRetries: number;
availableLangs: AvailableLangs;
availableLangs?: AvailableLangs;
flatten: {
aot: boolean;
};
Expand Down Expand Up @@ -46,7 +46,7 @@
interpolation: ['{{', '}}'],
};

type DeepPartial<T> = T extends Array<any>

Check warning on line 49 in libs/transloco/src/lib/transloco.config.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
? T
: T extends object
? { [P in keyof T]?: DeepPartial<T[P]> }
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
Loading