Skip to content

Commit c43a0a6

Browse files
committed
fix(i18n): fallback of translation wasn't working properly
Signed-off-by: Alexandre Philibeaux <aphilibeaux@scaleway.com>
1 parent 402eb35 commit c43a0a6

File tree

7 files changed

+70
-70
lines changed

7 files changed

+70
-70
lines changed

.changeset/poor-eyes-itch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@scaleway/use-i18n": patch
3+
---
4+
5+
Fix fallback issue on the default local during translation

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
- run: pnpm run build
3838
- run: pnpm run lint
3939
test:
40-
runs-on: ubuntu-22.04
40+
runs-on: ubuntu-24.04
4141
strategy:
4242
matrix:
4343
node: ["20"]

package.json

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,5 @@
8484
"commitizen": {
8585
"path": "./node_modules/cz-conventional-changelog"
8686
}
87-
},
88-
"jest": {
89-
"testEnvironment": "jsdom",
90-
"setupFiles": ["jest-localstorage-mock"],
91-
"collectCoverageFrom": ["packages/*/src/**/*.{ts,tsx,js,jsx}"],
92-
"modulePathIgnorePatterns": ["locales", "__typetests__", "dist"],
93-
"coverageReporters": ["text", "cobertura"],
94-
"transformIgnorePatterns": [
95-
"node_modules/(?!(.*(query-string|decode-uri-component|split-on-first|filter-obj)))"
96-
],
97-
"reporters": [
98-
"default",
99-
[
100-
"jest-junit",
101-
{
102-
"outputDirectory": ".reports",
103-
"outputName": "tests.xml"
104-
}
105-
]
106-
]
10787
}
10888
}

packages/use-i18n/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"type": "module",
1010
"module": "./dist/index.js",
1111
"types": "./dist/index.d.ts",
12+
"files": [
13+
"dist/"
14+
],
1215
"exports": {
1316
".": {
1417
"types": "./dist/index.d.ts",

packages/use-i18n/src/__tests__/__snapshots__/formatDate.test.ts.snap

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,55 @@ exports[`formatDate > should return passed object if not valid date 1`] = `
66
}
77
`;
88

9-
exports[`formatDate > should work with custom format and locale de 1`] = `"Donnerstag, 13. F 20 n. Chr., 16:28:00 Koordinierte Weltzeit"`;
9+
exports[`formatDate > should work with custom format and locale de 1`] = `"Donnerstag, 13. F 20 n. Chr., 16:28:00 Mitteleuropäische Normalzeit"`;
1010

11-
exports[`formatDate > should work with custom format and locale de 2`] = `"Donnerstag, 13. F 20 n. Chr., 15:28:00 Koordinierte Weltzeit"`;
11+
exports[`formatDate > should work with custom format and locale de 2`] = `"Donnerstag, 13. F 20 n. Chr., 16:28:00 Mitteleuropäische Normalzeit"`;
1212

13-
exports[`formatDate > should work with custom format and locale de 3`] = `"Donnerstag, 13. F 20 n. Chr., 15:28:00 Koordinierte Weltzeit"`;
13+
exports[`formatDate > should work with custom format and locale de 3`] = `"Donnerstag, 13. F 20 n. Chr., 16:28:00 Mitteleuropäische Normalzeit"`;
1414

15-
exports[`formatDate > should work with custom format and locale en 1`] = `"Thursday, F 13, 20 AD, 04:28:00 PM Coordinated Universal Time"`;
15+
exports[`formatDate > should work with custom format and locale en 1`] = `"Thursday, F 13, 20 AD, 04:28:00 PM Central European Standard Time"`;
1616

17-
exports[`formatDate > should work with custom format and locale en 2`] = `"Thursday, F 13, 20 AD, 03:28:00 PM Coordinated Universal Time"`;
17+
exports[`formatDate > should work with custom format and locale en 2`] = `"Thursday, F 13, 20 AD, 04:28:00 PM Central European Standard Time"`;
1818

19-
exports[`formatDate > should work with custom format and locale en 3`] = `"Thursday, F 13, 20 AD, 03:28:00 PM Coordinated Universal Time"`;
19+
exports[`formatDate > should work with custom format and locale en 3`] = `"Thursday, F 13, 20 AD, 04:28:00 PM Central European Standard Time"`;
2020

21-
exports[`formatDate > should work with custom format and locale es 1`] = `"jueves, 13 F 20 d. C., 16:28:00 (tiempo universal coordinado)"`;
21+
exports[`formatDate > should work with custom format and locale es 1`] = `"jueves, 13 F 20 d. C., 16:28:00 (hora estándar de Europa central)"`;
2222

23-
exports[`formatDate > should work with custom format and locale es 2`] = `"jueves, 13 F 20 d. C., 15:28:00 (tiempo universal coordinado)"`;
23+
exports[`formatDate > should work with custom format and locale es 2`] = `"jueves, 13 F 20 d. C., 16:28:00 (hora estándar de Europa central)"`;
2424

25-
exports[`formatDate > should work with custom format and locale es 3`] = `"jueves, 13 F 20 d. C., 15:28:00 (tiempo universal coordinado)"`;
25+
exports[`formatDate > should work with custom format and locale es 3`] = `"jueves, 13 F 20 d. C., 16:28:00 (hora estándar de Europa central)"`;
2626

27-
exports[`formatDate > should work with custom format and locale fr 1`] = `"jeudi 13 F 20 ap. J.-C. 16:28:00 temps universel coordonné"`;
27+
exports[`formatDate > should work with custom format and locale fr 1`] = `"jeudi 13 F 20 ap. J.-C. 16:28:00 heure normale d’Europe centrale"`;
2828

29-
exports[`formatDate > should work with custom format and locale fr 2`] = `"jeudi 13 F 20 ap. J.-C. 15:28:00 temps universel coordonné"`;
29+
exports[`formatDate > should work with custom format and locale fr 2`] = `"jeudi 13 F 20 ap. J.-C. 16:28:00 heure normale d’Europe centrale"`;
3030

31-
exports[`formatDate > should work with custom format and locale fr 3`] = `"jeudi 13 F 20 ap. J.-C. 15:28:00 temps universel coordonné"`;
31+
exports[`formatDate > should work with custom format and locale fr 3`] = `"jeudi 13 F 20 ap. J.-C. 16:28:00 heure normale d’Europe centrale"`;
3232

33-
exports[`formatDate > should work with custom format and locale ro 1`] = `"joi, 13 F 20 d.Hr., 16:28:00 Timpul universal coordonat"`;
33+
exports[`formatDate > should work with custom format and locale ro 1`] = `"joi, 13 F 20 d.Hr., 16:28:00 Ora standard a Europei Centrale"`;
3434

35-
exports[`formatDate > should work with custom format and locale ro 2`] = `"joi, 13 F 20 d.Hr., 15:28:00 Timpul universal coordonat"`;
35+
exports[`formatDate > should work with custom format and locale ro 2`] = `"joi, 13 F 20 d.Hr., 16:28:00 Ora standard a Europei Centrale"`;
3636

37-
exports[`formatDate > should work with custom format and locale ro 3`] = `"joi, 13 F 20 d.Hr., 15:28:00 Timpul universal coordonat"`;
37+
exports[`formatDate > should work with custom format and locale ro 3`] = `"joi, 13 F 20 d.Hr., 16:28:00 Ora standard a Europei Centrale"`;
3838

39-
exports[`formatDate > should work with format "hour", for date = "2020-02-13T15:28:00.000Z" and locale "de" 1`] = `"13. Februar 2020 um 15:28"`;
39+
exports[`formatDate > should work with format "hour", for date = "2020-02-13T15:28:00.000Z" and locale "de" 1`] = `"13. Februar 2020 um 16:28"`;
4040

41-
exports[`formatDate > should work with format "hour", for date = "2020-02-13T15:28:00.000Z" and locale "en" 1`] = `"February 13, 2020 at 3:28 PM"`;
41+
exports[`formatDate > should work with format "hour", for date = "2020-02-13T15:28:00.000Z" and locale "en" 1`] = `"February 13, 2020 at 4:28 PM"`;
4242

43-
exports[`formatDate > should work with format "hour", for date = "2020-02-13T15:28:00.000Z" and locale "es" 1`] = `"13 de febrero de 2020, 15:28"`;
43+
exports[`formatDate > should work with format "hour", for date = "2020-02-13T15:28:00.000Z" and locale "es" 1`] = `"13 de febrero de 2020, 16:28"`;
4444

45-
exports[`formatDate > should work with format "hour", for date = "2020-02-13T15:28:00.000Z" and locale "fr" 1`] = `"13 février 2020 à 15:28"`;
45+
exports[`formatDate > should work with format "hour", for date = "2020-02-13T15:28:00.000Z" and locale "fr" 1`] = `"13 février 2020 à 16:28"`;
4646

47-
exports[`formatDate > should work with format "hour", for date = "2020-02-13T15:28:00.000Z" and locale "ro" 1`] = `"13 februarie 2020 la 15:28"`;
47+
exports[`formatDate > should work with format "hour", for date = "2020-02-13T15:28:00.000Z" and locale "ro" 1`] = `"13 februarie 2020 la 16:28"`;
4848

49-
exports[`formatDate > should work with format "hour", for date = "1581607680000" and locale "de" 1`] = `"13. Februar 2020 um 15:28"`;
49+
exports[`formatDate > should work with format "hour", for date = "1581607680000" and locale "de" 1`] = `"13. Februar 2020 um 16:28"`;
5050

51-
exports[`formatDate > should work with format "hour", for date = "1581607680000" and locale "en" 1`] = `"February 13, 2020 at 3:28 PM"`;
51+
exports[`formatDate > should work with format "hour", for date = "1581607680000" and locale "en" 1`] = `"February 13, 2020 at 4:28 PM"`;
5252

53-
exports[`formatDate > should work with format "hour", for date = "1581607680000" and locale "es" 1`] = `"13 de febrero de 2020, 15:28"`;
53+
exports[`formatDate > should work with format "hour", for date = "1581607680000" and locale "es" 1`] = `"13 de febrero de 2020, 16:28"`;
5454

55-
exports[`formatDate > should work with format "hour", for date = "1581607680000" and locale "fr" 1`] = `"13 février 2020 à 15:28"`;
55+
exports[`formatDate > should work with format "hour", for date = "1581607680000" and locale "fr" 1`] = `"13 février 2020 à 16:28"`;
5656

57-
exports[`formatDate > should work with format "hour", for date = "1581607680000" and locale "ro" 1`] = `"13 februarie 2020 la 15:28"`;
57+
exports[`formatDate > should work with format "hour", for date = "1581607680000" and locale "ro" 1`] = `"13 februarie 2020 la 16:28"`;
5858

5959
exports[`formatDate > should work with format "hour", for date = "new Date(2020, 1, 13, 16, 28)" and locale "de" 1`] = `"13. Februar 2020 um 16:28"`;
6060

@@ -66,25 +66,25 @@ exports[`formatDate > should work with format "hour", for date = "new Date(2020,
6666

6767
exports[`formatDate > should work with format "hour", for date = "new Date(2020, 1, 13, 16, 28)" and locale "ro" 1`] = `"13 februarie 2020 la 16:28"`;
6868

69-
exports[`formatDate > should work with format "hourOnly", for date = "2020-02-13T15:28:00.000Z" and locale "de" 1`] = `"15:28"`;
69+
exports[`formatDate > should work with format "hourOnly", for date = "2020-02-13T15:28:00.000Z" and locale "de" 1`] = `"16:28"`;
7070

71-
exports[`formatDate > should work with format "hourOnly", for date = "2020-02-13T15:28:00.000Z" and locale "en" 1`] = `"3:28 PM"`;
71+
exports[`formatDate > should work with format "hourOnly", for date = "2020-02-13T15:28:00.000Z" and locale "en" 1`] = `"4:28 PM"`;
7272

73-
exports[`formatDate > should work with format "hourOnly", for date = "2020-02-13T15:28:00.000Z" and locale "es" 1`] = `"15:28"`;
73+
exports[`formatDate > should work with format "hourOnly", for date = "2020-02-13T15:28:00.000Z" and locale "es" 1`] = `"16:28"`;
7474

75-
exports[`formatDate > should work with format "hourOnly", for date = "2020-02-13T15:28:00.000Z" and locale "fr" 1`] = `"15:28"`;
75+
exports[`formatDate > should work with format "hourOnly", for date = "2020-02-13T15:28:00.000Z" and locale "fr" 1`] = `"16:28"`;
7676

77-
exports[`formatDate > should work with format "hourOnly", for date = "2020-02-13T15:28:00.000Z" and locale "ro" 1`] = `"15:28"`;
77+
exports[`formatDate > should work with format "hourOnly", for date = "2020-02-13T15:28:00.000Z" and locale "ro" 1`] = `"16:28"`;
7878

79-
exports[`formatDate > should work with format "hourOnly", for date = "1581607680000" and locale "de" 1`] = `"15:28"`;
79+
exports[`formatDate > should work with format "hourOnly", for date = "1581607680000" and locale "de" 1`] = `"16:28"`;
8080

81-
exports[`formatDate > should work with format "hourOnly", for date = "1581607680000" and locale "en" 1`] = `"3:28 PM"`;
81+
exports[`formatDate > should work with format "hourOnly", for date = "1581607680000" and locale "en" 1`] = `"4:28 PM"`;
8282

83-
exports[`formatDate > should work with format "hourOnly", for date = "1581607680000" and locale "es" 1`] = `"15:28"`;
83+
exports[`formatDate > should work with format "hourOnly", for date = "1581607680000" and locale "es" 1`] = `"16:28"`;
8484

85-
exports[`formatDate > should work with format "hourOnly", for date = "1581607680000" and locale "fr" 1`] = `"15:28"`;
85+
exports[`formatDate > should work with format "hourOnly", for date = "1581607680000" and locale "fr" 1`] = `"16:28"`;
8686

87-
exports[`formatDate > should work with format "hourOnly", for date = "1581607680000" and locale "ro" 1`] = `"15:28"`;
87+
exports[`formatDate > should work with format "hourOnly", for date = "1581607680000" and locale "ro" 1`] = `"16:28"`;
8888

8989
exports[`formatDate > should work with format "hourOnly", for date = "new Date(2020, 1, 13, 16, 28)" and locale "de" 1`] = `"16:28"`;
9090

@@ -156,25 +156,25 @@ exports[`formatDate > should work with format "numeric", for date = "new Date(20
156156

157157
exports[`formatDate > should work with format "numeric", for date = "new Date(2020, 1, 13, 16, 28)" and locale "ro" 1`] = `"2020-02-13"`;
158158

159-
exports[`formatDate > should work with format "numericHour", for date = "2020-02-13T15:28:00.000Z" and locale "de" 1`] = `"2020-02-13 15:28"`;
159+
exports[`formatDate > should work with format "numericHour", for date = "2020-02-13T15:28:00.000Z" and locale "de" 1`] = `"2020-02-13 16:28"`;
160160

161-
exports[`formatDate > should work with format "numericHour", for date = "2020-02-13T15:28:00.000Z" and locale "en" 1`] = `"2020-02-13 3:28 PM"`;
161+
exports[`formatDate > should work with format "numericHour", for date = "2020-02-13T15:28:00.000Z" and locale "en" 1`] = `"2020-02-13 4:28 PM"`;
162162

163-
exports[`formatDate > should work with format "numericHour", for date = "2020-02-13T15:28:00.000Z" and locale "es" 1`] = `"2020-02-13 15:28"`;
163+
exports[`formatDate > should work with format "numericHour", for date = "2020-02-13T15:28:00.000Z" and locale "es" 1`] = `"2020-02-13 16:28"`;
164164

165-
exports[`formatDate > should work with format "numericHour", for date = "2020-02-13T15:28:00.000Z" and locale "fr" 1`] = `"2020-02-13 15:28"`;
165+
exports[`formatDate > should work with format "numericHour", for date = "2020-02-13T15:28:00.000Z" and locale "fr" 1`] = `"2020-02-13 16:28"`;
166166

167-
exports[`formatDate > should work with format "numericHour", for date = "2020-02-13T15:28:00.000Z" and locale "ro" 1`] = `"2020-02-13 15:28"`;
167+
exports[`formatDate > should work with format "numericHour", for date = "2020-02-13T15:28:00.000Z" and locale "ro" 1`] = `"2020-02-13 16:28"`;
168168

169-
exports[`formatDate > should work with format "numericHour", for date = "1581607680000" and locale "de" 1`] = `"2020-02-13 15:28"`;
169+
exports[`formatDate > should work with format "numericHour", for date = "1581607680000" and locale "de" 1`] = `"2020-02-13 16:28"`;
170170

171-
exports[`formatDate > should work with format "numericHour", for date = "1581607680000" and locale "en" 1`] = `"2020-02-13 3:28 PM"`;
171+
exports[`formatDate > should work with format "numericHour", for date = "1581607680000" and locale "en" 1`] = `"2020-02-13 4:28 PM"`;
172172

173-
exports[`formatDate > should work with format "numericHour", for date = "1581607680000" and locale "es" 1`] = `"2020-02-13 15:28"`;
173+
exports[`formatDate > should work with format "numericHour", for date = "1581607680000" and locale "es" 1`] = `"2020-02-13 16:28"`;
174174

175-
exports[`formatDate > should work with format "numericHour", for date = "1581607680000" and locale "fr" 1`] = `"2020-02-13 15:28"`;
175+
exports[`formatDate > should work with format "numericHour", for date = "1581607680000" and locale "fr" 1`] = `"2020-02-13 16:28"`;
176176

177-
exports[`formatDate > should work with format "numericHour", for date = "1581607680000" and locale "ro" 1`] = `"2020-02-13 15:28"`;
177+
exports[`formatDate > should work with format "numericHour", for date = "1581607680000" and locale "ro" 1`] = `"2020-02-13 16:28"`;
178178

179179
exports[`formatDate > should work with format "numericHour", for date = "new Date(2020, 1, 13, 16, 28)" and locale "de" 1`] = `"2020-02-13 16:28"`;
180180

packages/use-i18n/src/__tests__/usei18n.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ describe('i18n hook', () => {
497497
error: new Error(
498498
'The intl string context variable "oldFrenchVariable" was not provided to the string "onTranslateError fonction sera appelé car il manque une variable en français {oldFrenchVariable}"',
499499
),
500+
defaultLocale: 'en',
500501
currentLocale: 'fr',
501502
key: 'translate.error',
502503
value:

packages/use-i18n/src/usei18n.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ type LoadDateLocaleError = (error: Error) => void
188188

189189
const initialDefaultTranslations = {}
190190

191+
// TODO: improve type from Provider based on a Generic to have 'fr' | 'en'
192+
type Locale = string
193+
191194
const I18nContextProvider = ({
192195
children,
193196
defaultLoad,
@@ -207,20 +210,21 @@ const I18nContextProvider = ({
207210
loadDateLocale?: LoadLocaleFn
208211
loadDateLocaleAsync: LoadLocaleFnAsync
209212
onLoadDateLocaleError?: LoadDateLocaleError
210-
defaultLocale: string
213+
defaultLocale: Locale
211214
defaultTranslations: TranslationsByLocales
212215
enableDefaultLocale: boolean
213216
enableDebugKey: boolean
214-
localeItemStorage: string
215-
supportedLocales: string[]
217+
localeItemStorage: Locale
218+
supportedLocales: Locale[]
216219
onTranslateError?: ({
217220
error,
218221
currentLocale,
219222
value,
220223
key,
221224
}: {
222225
error: Error
223-
currentLocale: string
226+
currentLocale: Locale
227+
defaultLocale: Locale
224228
value: string
225229
key: string
226230
}) => void
@@ -281,6 +285,7 @@ const I18nContextProvider = ({
281285
namespace,
282286
})
283287
}
288+
284289
result[currentLocale] = await load({
285290
locale: currentLocale,
286291
namespace,
@@ -293,6 +298,10 @@ const I18nContextProvider = ({
293298

294299
setTranslations(prevState => ({
295300
...prevState,
301+
[defaultLocale]: {
302+
...prevState[defaultLocale],
303+
...result.defaultLocale.default,
304+
},
296305
[currentLocale]: {
297306
...prevState[currentLocale],
298307
...trad,
@@ -394,6 +403,7 @@ const I18nContextProvider = ({
394403
if (!value) {
395404
return ''
396405
}
406+
397407
if (context) {
398408
try {
399409
return formatters
@@ -403,6 +413,7 @@ const I18nContextProvider = ({
403413
onTranslateError?.({
404414
error: err as Error,
405415
currentLocale,
416+
defaultLocale,
406417
value,
407418
key,
408419
})

0 commit comments

Comments
 (0)