Skip to content

Commit 873e4ff

Browse files
authored
Replace i18next backend with i18next-fetch-backend (#37633)
Signed-off-by: Jon Koops <[email protected]>
1 parent c22f768 commit 873e4ff

File tree

8 files changed

+34
-82
lines changed

8 files changed

+34
-82
lines changed

docs/documentation/server_development/topics/themes-react.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import { KeycloakProvider } from "@keycloak/keycloak-ui-shared";
4242

4343
The pages are translated using the `i18next` library.
4444
You can set it up as described on their [website](https://react.i18next.com/).
45-
If you want to use the translations that are provided then you need to add i18next-http-backend to your project and add:
45+
If you want to use the translations that are provided then you need to add `i18next-fetch-backend` to your project and add:
4646

4747
[source,javascript]
4848
----
@@ -51,9 +51,9 @@ backend: {
5151
parse: (data: string) => {
5252
const messages = JSON.parse(data);
5353
54-
const result: Record<string, string> = {};
55-
messages.forEach((v) => (result[v.key] = v.value)); //need to convert to record
56-
return result;
54+
return Object.fromEntries(
55+
messages.map(({ key, value }) => [key, value])
56+
);
5757
},
5858
},
5959
----

js/apps/account-ui/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { KeycloakProvider } from "@keycloak/keycloak-ui-shared";
3333
### Translation
3434

3535
For the translation we use `react-i18next` you can [set it up](https://react.i18next.com/) as described on their website.
36-
If you want to use the translations that are provided then you need to add `i18next-http-backend` to your project and add:
36+
If you want to use the translations that are provided then you need to add `i18next-fetch-backend` to your project and add:
3737

3838
```ts
3939

@@ -42,9 +42,9 @@ backend: {
4242
parse: (data: string) => {
4343
const messages = JSON.parse(data);
4444

45-
const result: Record<string, string> = {};
46-
messages.forEach((v) => (result[v.key] = v.value));
47-
return result;
45+
return Object.fromEntries(
46+
messages.map(({ key, value }) => [key, value])
47+
);
4848
},
4949
},
5050
```

js/apps/account-ui/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"@patternfly/react-icons": "^5.4.2",
3232
"@patternfly/react-table": "^5.4.16",
3333
"i18next": "^24.2.2",
34-
"i18next-http-backend": "^3.0.2",
34+
"i18next-fetch-backend": "^6.0.0",
3535
"keycloak-js": "^26.2.0",
3636
"lodash-es": "^4.17.21",
3737
"react": "^18.3.1",

js/apps/account-ui/src/i18n.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LanguageDetectorModule, createInstance } from "i18next";
2-
import HttpBackend from "i18next-http-backend";
2+
import FetchBackend from "i18next-fetch-backend";
33
import { initReactI18next } from "react-i18next";
44

55
import { environment } from "./environment";
@@ -32,16 +32,14 @@ export const i18n = createInstance({
3232
environment.serverBaseUrl,
3333
`resources/${environment.realm}/account/{{lng}}`,
3434
),
35-
parse: (data: string) => {
36-
const messages = JSON.parse(data);
35+
parse(data: string) {
36+
const messages: KeyValue[] = JSON.parse(data);
3737

38-
const result: Record<string, string> = {};
39-
messages.forEach((v: KeyValue) => (result[v.key] = v.value));
40-
return result;
38+
return Object.fromEntries(messages.map(({ key, value }) => [key, value]));
4139
},
4240
},
4341
});
4442

45-
i18n.use(HttpBackend);
43+
i18n.use(FetchBackend);
4644
i18n.use(keycloakLanguageDetector);
4745
i18n.use(initReactI18next);

js/apps/admin-ui/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { KeycloakProvider } from "@keycloak/keycloak-ui-shared";
3333
### Translation
3434

3535
For the translation we use `react-i18next` you can [set it up](https://react.i18next.com/) as described on their website.
36-
If you want to use the translations that are provided then you need to add `i18next-http-backend` to your project and add:
36+
If you want to use the translations that are provided then you need to add `i18next-fetch-backend` to your project and add:
3737

3838
```ts
3939

@@ -42,9 +42,9 @@ backend: {
4242
parse: (data: string) => {
4343
const messages = JSON.parse(data);
4444

45-
const result: Record<string, string> = {};
46-
messages.forEach((v) => (result[v.key] = v.value));
47-
return result;
45+
return Object.fromEntries(
46+
messages.map(({ key, value }) => [key, value])
47+
);
4848
},
4949
},
5050
```

js/apps/admin-ui/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
"file-saver": "^2.0.5",
102102
"flat": "^6.0.1",
103103
"i18next": "^24.2.2",
104-
"i18next-http-backend": "^3.0.2",
104+
"i18next-fetch-backend": "^6.0.0",
105105
"jszip": "^3.10.1",
106106
"keycloak-js": "^26.2.0",
107107
"lodash-es": "^4.17.21",

js/apps/admin-ui/src/i18n/i18n.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createInstance } from "i18next";
2-
import HttpBackend from "i18next-http-backend";
2+
import FetchBackend from "i18next-fetch-backend";
33
import { initReactI18next } from "react-i18next";
44

55
import { environment } from "../environment";
@@ -24,14 +24,12 @@ export const i18n = createInstance({
2424
`resources/{{ns}}/admin/{{lng}}`,
2525
),
2626
parse: (data: string) => {
27-
const messages = JSON.parse(data);
27+
const messages: KeyValue[] = JSON.parse(data);
2828

29-
const result: Record<string, string> = {};
30-
messages.forEach((v: KeyValue) => (result[v.key] = v.value));
31-
return result;
29+
return Object.fromEntries(messages.map(({ key, value }) => [key, value]));
3230
},
3331
},
3432
});
3533

36-
i18n.use(HttpBackend);
34+
i18n.use(FetchBackend);
3735
i18n.use(initReactI18next);

js/pnpm-lock.yaml

Lines changed: 11 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)