Skip to content

Commit f26e56f

Browse files
committed
Add i18n support
1 parent 18532ff commit f26e56f

File tree

6 files changed

+197
-24
lines changed

6 files changed

+197
-24
lines changed

package-lock.json

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

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
"@types/react-dom": "^19.0.2",
1111
"antd": "^5.22.1",
1212
"blockly": "^11.1.1",
13+
"i18next": "^24.2.2",
14+
"i18next-http-backend": "^3.0.2",
1315
"jszip": "^3.10.1",
1416
"lucide-react": "^0.460.0",
1517
"re-resizable": "^6.10.1",
1618
"react": "^18.3.1",
1719
"react-dom": "^18.3.1",
20+
"react-i18next": "^15.4.1",
1821
"react-syntax-highlighter": "^15.6.1",
1922
"web-vitals": "^2.1.4"
2023
},

public/locales/en/translation.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"project_delete": "i18n: Delete Project",
3+
"project_rename": "i18n: Rename Project",
4+
"project_copy": "i18n: Copy Project"
5+
}
6+

src/App.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import {
3535
UploadOutlined,
3636
} from '@ant-design/icons';
3737

38+
import { useTranslation } from "react-i18next";
39+
3840
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
3941
import { dracula } from 'react-syntax-highlighter/dist/esm/styles/prism';
4042

@@ -117,6 +119,8 @@ const App: React.FC = () => {
117119
const PURPOSE_RENAME_MODULE = 'RenameModule';
118120
const PURPOSE_COPY_MODULE = 'CopyModule';
119121

122+
const { t } = useTranslation();
123+
120124
const ignoreEffect = () => {
121125
if (!import.meta.env.MODE || import.meta.env.MODE === 'development') {
122126
// Development mode.
@@ -308,9 +312,9 @@ const App: React.FC = () => {
308312

309313
if (module != null) {
310314
if (module.moduleType == commonStorage.MODULE_TYPE_PROJECT) {
311-
setRenameTooltip('Rename Project');
312-
setCopyTooltip('Copy Project');
313-
setDeleteTooltip('Delete Project');
315+
setRenameTooltip(t('project_rename'));
316+
setCopyTooltip(t('project_copy'));
317+
setDeleteTooltip(t('project_delete'));
314318
} else if (module.moduleType == commonStorage.MODULE_TYPE_MECHANISM) {
315319
setRenameTooltip('Rename Mechanism');
316320
setCopyTooltip('Copy Mechanism');

src/i18n/config.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// src/i18n/config.ts
2+
3+
// Core i18next library.
4+
import i18n from "i18next";
5+
import HttpApi from "i18next-http-backend";
6+
// Bindings for React: allow components to
7+
// re-render when language changes.
8+
import { initReactI18next } from "react-i18next";
9+
10+
i18n
11+
// Wire up the backend as a plugin.
12+
.use(HttpApi)
13+
// Add React bindings as a plugin.
14+
.use(initReactI18next)
15+
// Initialize the i18next instance.
16+
.init({
17+
// Config options
18+
19+
// Specifies the default language (locale) used
20+
// when a user visits our site for the first time.
21+
// We use English here, but feel free to use
22+
// whichever locale you want.
23+
lng: "en",
24+
25+
// Fallback locale used when a translation is
26+
// missing in the active locale. Again, use your
27+
// preferred locale here.
28+
fallbackLng: "en",
29+
30+
// Normally, we want `escapeValue: true` as it
31+
// ensures that i18next escapes any code in
32+
// translation messages, safeguarding against
33+
// XSS (cross-site scripting) attacks. However,
34+
// React does this escaping itself, so we turn
35+
// it off in i18next.
36+
interpolation: {
37+
escapeValue: false,
38+
},
39+
});
40+
41+
export default i18n;

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import ReactDOM from 'react-dom/client';
33
import './index.css';
44
import App from './App';
5+
import "./i18n/config.ts"
56
import reportWebVitals from './reportWebVitals';
67

78
const root = ReactDOM.createRoot(

0 commit comments

Comments
 (0)