Skip to content

Commit

Permalink
feat: added fallbackIgnoresFallbackLocales setting
Browse files Browse the repository at this point in the history
  • Loading branch information
schummar committed Jul 19, 2023
1 parent ec43a5d commit 64ca937
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/react/translator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function createTranslator<D extends Dict, ProvidedArgs extends string = n
fallbackLocale,
fallbackToLessSpecific = true,
fallback: defaultFallback,
fallbackIgnoresFallbackLocales = false,
placeholder: defaultPlaceholder,
warn,
dateTimeFormatOptions,
Expand Down Expand Up @@ -87,6 +88,7 @@ export function createTranslator<D extends Dict, ProvidedArgs extends string = n
id,
values,
fallback,
fallbackIgnoresFallbackLocales,
placeholder,
locale,
warn,
Expand Down Expand Up @@ -155,6 +157,7 @@ export function createTranslator<D extends Dict, ProvidedArgs extends string = n
id,
values,
fallback,
fallbackIgnoresFallbackLocales,
placeholder,
locale,
warn,
Expand Down
4 changes: 3 additions & 1 deletion src/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function translate<F = never>({
id,
values,
fallback,
fallbackIgnoresFallbackLocales,
placeholder,
locale,
warn,
Expand All @@ -24,14 +25,15 @@ export function translate<F = never>({
id: string;
values?: any;
fallback?: F | ((id: string, sourceTranslation?: string | readonly string[]) => F);
fallbackIgnoresFallbackLocales?: boolean;
placeholder?: F | ((id: string, sourceTranslation?: string | readonly string[]) => F);
locale: string;
warn?: (locale: string, id: string) => void;
cache: Cache;
ignoreMissingArgs: boolean | string | ((id: string, template: string) => string) | undefined;
providedArgs: Record<string, ICUArgument | ICUDateArgument> | undefined;
}): string | F | (string | F)[] | F {
if (fallback !== undefined) {
if (fallback !== undefined && fallbackIgnoresFallbackLocales) {
dicts = dicts.slice(0, 1);
}

Expand Down
2 changes: 2 additions & 0 deletions src/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const createGetTranslator =
fallbackLocale,
fallbackToLessSpecific = true,
fallback: globalFallback,
fallbackIgnoresFallbackLocales = false,
warn,
sourceLocale,
dateTimeFormatOptions,
Expand All @@ -38,6 +39,7 @@ export const createGetTranslator =
id,
values,
fallback,
fallbackIgnoresFallbackLocales,
locale,
warn,
cache: store.cache,
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ export type CreateTranslatorOptions<D extends Dict, ProvidedArgs extends string
* @default true
*/
fallbackToLessSpecific?: boolean;
/** If a fallback is provided to a translation string
* - if `fallbackIgnoresFallbackLocales` is true, the fallback will be used if there is no match in the current locale
* - if `fallbackIgnoresFallbackLocales` is false, the fallback will be used if there is no match in the current or any fallback locales
* @default false
*/
fallbackIgnoresFallbackLocales?: boolean;
/** Dictionaries. Either a record with locales as keys or a function that takes a locale and returns a promise of a dictionary
* @param locale the active locale
*/
Expand Down
34 changes: 34 additions & 0 deletions test/translator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,40 @@ describe('fallback order', () => {
expect(_t('key1')).toBe('key1:de');
expect(_t('deOnly')).toBe('deOnly:de');
});

test('with fallbackIgnoresFallbackLocales=true', async () => {
const { getTranslator } = createTranslator({
sourceLocale: 'de',
sourceDictionary: dictDe,
fallbackLocale: 'de',
dicts: {
de: { key1: 'value' },
en: {},
},
fallbackIgnoresFallbackLocales: true,
});

const _t = await getTranslator('en');
expect(_t('key1', {}, { fallback: 'fallback' })).toBe('fallback');
expect(_t.unknown('key2', {}, { fallback: 'fallback' })).toBe('fallback');
});

test('with fallbackIgnoresFallbackLocales=false', async () => {
const { getTranslator } = createTranslator({
sourceLocale: 'de',
sourceDictionary: dictDe,
fallbackLocale: 'de',
dicts: {
de: { key1: 'value' },
en: {},
},
fallbackIgnoresFallbackLocales: false,
});

const _t = await getTranslator('en');
expect(_t('key1', {}, { fallback: 'fallback' })).toBe('key1:de');
expect(_t.unknown('key2', {}, { fallback: 'fallback' })).toBe('fallback');
});
});

describe('ignoreMissingArgs', () => {
Expand Down

0 comments on commit 64ca937

Please sign in to comment.