1 import { format } from 'date-fns';
2 import type { LocaleData } from 'ttag';
3 import { c, useLocale as ttagUseLocale } from 'ttag';
5 import { DEFAULT_LOCALE } from '../../lib/constants';
6 import { dateLocale } from '../../lib/i18n';
7 import { loadDateLocale, loadLocale } from '../../lib/i18n/loadLocale';
8 import { setTtagLocales } from '../../lib/i18n/locales';
9 import { relocalizeText } from '../../lib/i18n/relocalize';
10 import type { UserSettings } from '../../lib/interfaces';
12 const dummyUserSettings: Pick<UserSettings, 'TimeFormat' | 'DateFormat' | 'WeekStart'> = {
18 const getTranslation = (data: string) => {
21 'plural-forms': 'nplurals=2; plural=(n != 1);',
25 'This is not a pipe': [data],
28 } as unknown as LocaleData; // the LocaleData type is wrong.
30 const getTranslationWithDate = (data: string) => {
33 'plural-forms': 'nplurals=2; plural=(n != 1);',
37 // eslint-disable-next-line no-template-curly-in-string
38 'December 16th 2021 was a ${ formattedDate }': [data],
41 } as unknown as LocaleData; // the LocaleData type is wrong.
43 const getLocalizedText = () => c('Info').t`This is not a pipe`;
44 const getLocalizedTextWithDate = () => {
45 const formattedDate = format(new Date(2021, 11, 16), 'cccc', { locale: dateLocale });
46 return c('Info').t`December 16th 2021 was a ${formattedDate}`;
49 describe('relocalizeText', () => {
52 ttagUseLocale(DEFAULT_LOCALE);
53 void loadDateLocale(DEFAULT_LOCALE);
56 it('should localize to a different locale only inside the relocalizeText context', async () => {
58 fr_FR: async () => getTranslation("Ceci n'est pas une pipe"),
59 it_IT: async () => getTranslation('Questa non è una pipa'),
61 setTtagLocales(locales);
62 await loadLocale('fr_FR', locales);
64 await relocalizeText({
66 newLocaleCode: 'it_IT',
67 userSettings: dummyUserSettings,
69 ).toEqual('Questa non è una pipa');
70 expect(getLocalizedText()).toEqual("Ceci n'est pas une pipe");
73 it('should not try to load the default locale', async () => {
75 fr_FR: async () => getTranslation("Ceci n'est pas une pipe"),
76 it_IT: async () => getTranslation('Questa non è una pipa'),
78 setTtagLocales(locales);
79 await loadLocale('fr_FR', locales);
81 await relocalizeText({
83 newLocaleCode: DEFAULT_LOCALE,
84 userSettings: dummyUserSettings,
86 ).toEqual('This is not a pipe');
87 // notice that if relocalizeText tried to load the default locale, it would fail
88 // and fall in the next test (fallback to current locale if the locale passed has no data)
91 it('should fallback to the current locale if the locale passed has no data', async () => {
93 fr_FR: async () => getTranslation("Ceci n'est pas une pipe"),
94 it_IT: async () => getTranslation('Questa non è una pipa'),
97 setTtagLocales(locales);
98 await loadLocale('fr_FR', locales);
100 await relocalizeText({
102 newLocaleCode: 'gl_ES',
103 userSettings: dummyUserSettings,
105 ).toEqual("Ceci n'est pas une pipe");
106 expect(getLocalizedText()).toEqual("Ceci n'est pas une pipe");
109 it('should fallback to the current locale if no locale is passed', async () => {
111 await relocalizeText({
113 userSettings: dummyUserSettings,
115 ).toEqual('This is not a pipe');
118 it('should relocalize without date format', async () => {
120 // eslint-disable-next-line no-template-curly-in-string
121 es_ES: async () => getTranslationWithDate('El 16 de diciembre de 2021 fue un ${ formattedDate }'),
124 await relocalizeText({
125 getLocalizedText: getLocalizedTextWithDate,
126 newLocaleCode: 'es_ES',
127 userSettings: dummyUserSettings,
129 ).toEqual('El 16 de diciembre de 2021 fue un Thursday');
132 it('should relocalize with date format', async () => {
134 // eslint-disable-next-line no-template-curly-in-string
135 es_ES: async () => getTranslationWithDate('El 16 de diciembre de 2021 fue un ${ formattedDate }'),
137 await loadDateLocale('es_ES', 'es_ES');
139 await relocalizeText({
140 getLocalizedText: getLocalizedTextWithDate,
141 newLocaleCode: 'es_ES',
142 relocalizeDateFormat: true,
143 userSettings: dummyUserSettings,
145 ).toEqual('El 16 de diciembre de 2021 fue un jueves');
148 it('should fallback to current date locale if no date locale data is found', async () => {
150 // eslint-disable-next-line no-template-curly-in-string
151 xx_XX: async () => getTranslationWithDate('blah blah blah ${ formattedDate }'),
154 await relocalizeText({
155 getLocalizedText: getLocalizedTextWithDate,
156 newLocaleCode: 'xx_XX',
157 relocalizeDateFormat: true,
158 userSettings: dummyUserSettings,
160 ).toEqual('blah blah blah Thursday');