',
+ replace: { foo: '
' },
interpolation: { escapeValue: true },
}),
).toBe('Foo <div>');
@@ -445,7 +457,7 @@ describe('I18nextTranslationApi', () => {
foo: 'Foo $t(bar) $t(baz)',
bar: 'Nested',
baz: 'Baz {{ qux }}',
- });
+ } as const);
expect(snapshot.t('foo', { qux: 'Deep' })).toBe('Foo Nested Baz Deep');
});
@@ -460,24 +472,40 @@ describe('I18nextTranslationApi', () => {
relativeSecondsShort:
'= {{ x, relativeTime(range: second; style: short) }}',
list: '= {{ x, list }}',
- });
+ } as const);
- expect(snapshot.t('plain', { x: 5 })).toBe('= 5');
- expect(snapshot.t('number', { x: 5 })).toBe('= 5');
- expect(snapshot.t('number', { x: 5, minimumFractionDigits: 1 })).toBe(
- '= 5.0',
- );
- expect(snapshot.t('numberFixed', { x: 5 })).toBe('= 5.00');
+ expect(snapshot.t('plain', { replace: { x: '5' } })).toBe('= 5');
+ expect(snapshot.t('number', { replace: { x: 5 } })).toBe('= 5');
expect(
- snapshot.t('numberFixed', { x: 5, minimumFractionDigits: 3 }),
+ snapshot.t('number', {
+ replace: { x: 5 },
+ formatParams: { x: { minimumFractionDigits: 1 } },
+ }),
+ ).toBe('= 5.0');
+ expect(snapshot.t('numberFixed', { replace: { x: 5 } })).toBe('= 5.00');
+ expect(
+ snapshot.t('numberFixed', {
+ replace: { x: 5 },
+ formatParams: { x: { minimumFractionDigits: 3 } },
+ }),
).toBe('= 5.000');
- expect(snapshot.t('relativeTime', { x: 3 })).toBe('= in 3 days');
- expect(snapshot.t('relativeTime', { x: -3 })).toBe('= 3 days ago');
- expect(snapshot.t('relativeTime', { x: 15, range: 'weeks' })).toBe(
- '= in 15 weeks',
+ expect(snapshot.t('relativeTime', { replace: { x: 3 } })).toBe(
+ '= in 3 days',
+ );
+ expect(snapshot.t('relativeTime', { replace: { x: -3 } })).toBe(
+ '= 3 days ago',
);
expect(
- snapshot.t('relativeTime', { x: 15, range: 'weeks', style: 'short' }),
+ snapshot.t('relativeTime', {
+ replace: { x: 15 },
+ formatParams: { x: { range: 'weeks' } },
+ }),
+ ).toBe('= in 15 weeks');
+ expect(
+ snapshot.t('relativeTime', {
+ replace: { x: 15 },
+ formatParams: { x: { range: 'weeks', style: 'short' } },
+ }),
).toBe('= in 15 wk.');
expect(snapshot.t('relativeSeconds', { x: 1 })).toBe('= in 1 second');
expect(snapshot.t('relativeSeconds', { x: 2 })).toBe('= in 2 seconds');
@@ -500,15 +528,14 @@ describe('I18nextTranslationApi', () => {
derp_other: 'derps',
derpWithCount_one: '{{ count }} derp',
derpWithCount_other: '{{ count }} derps',
- });
+ } as const);
- // TODO(Rugvip): Support plural keys
- expect(snapshot.t('derp' as any, { count: 1 })).toBe('derp');
- expect(snapshot.t('derp' as any, { count: 2 })).toBe('derps');
- expect(snapshot.t('derp' as any, { count: 0 })).toBe('derps');
- expect(snapshot.t('derpWithCount' as any, { count: 1 })).toBe('1 derp');
- expect(snapshot.t('derpWithCount' as any, { count: 2 })).toBe('2 derps');
- expect(snapshot.t('derpWithCount' as any, { count: 0 })).toBe('0 derps');
+ expect(snapshot.t('derp', { count: 1 })).toBe('derp');
+ expect(snapshot.t('derp', { count: 2 })).toBe('derps');
+ expect(snapshot.t('derp', { count: 0 })).toBe('derps');
+ expect(snapshot.t('derpWithCount', { count: 1 })).toBe('1 derp');
+ expect(snapshot.t('derpWithCount', { count: 2 })).toBe('2 derps');
+ expect(snapshot.t('derpWithCount', { count: 0 })).toBe('0 derps');
});
});
});
diff --git a/packages/core-app-api/src/apis/implementations/TranslationApi/I18nextTranslationApi.ts b/packages/core-app-api/src/apis/implementations/TranslationApi/I18nextTranslationApi.ts
index 49fb12de46..01e75d6df2 100644
--- a/packages/core-app-api/src/apis/implementations/TranslationApi/I18nextTranslationApi.ts
+++ b/packages/core-app-api/src/apis/implementations/TranslationApi/I18nextTranslationApi.ts
@@ -17,6 +17,7 @@
import {
AppLanguageApi,
TranslationApi,
+ TranslationFunction,
TranslationMessages,
TranslationRef,
TranslationResource,
@@ -301,13 +302,14 @@ export class I18nextTranslationApi implements TranslationApi {
return { ready: false };
}
- const t = this.#i18n.getFixedT(null, internalRef.id);
+ const t = this.#i18n.getFixedT(
+ null,
+ internalRef.id,
+ ) as TranslationFunction;
return {
ready: true,
- t: (key, options) => {
- return t(key as string, { ...options });
- },
+ t,
};
}