Skip to content

Commit

Permalink
Support i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
slhmy committed Sep 26, 2023
1 parent 9dc47b2 commit 7e16f16
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 5 deletions.
61 changes: 61 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
"axios": "^1.3.4",
"framer-motion": "^10.16.1",
"graphql": "16.6.0",
"i18next": "^23.5.1",
"monaco-editor": "^0.39.0",
"msw": "^1.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-i18next": "^13.2.2",
"react-markdown": "^8.0.7",
"react-redux": "^8.0.4",
"react-router-dom": "^6.4.2",
Expand Down
27 changes: 27 additions & 0 deletions src/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import EN_US_TRANSLATIONS from "./i18n/en_US";
import ZH_CN_TRANSLATIONS from "./i18n/zh_CN";

// the translations
// (tip move them in a JSON file and import them,
// or even better, manage them separated from your code: https://react.i18next.com/guides/multiple-translation-files)
const resources = {
en_US: EN_US_TRANSLATIONS,
zh_CN: ZH_CN_TRANSLATIONS,
};

i18n
.use(initReactI18next) // passes i18n down to react-i18next
.init({
resources,
lng: "en_US", // language to use, more information here: https://www.i18next.com/overview/configuration-options#languages-namespaces-resources
// you can use the i18n.changeLanguage function to change the language manually: https://www.i18next.com/overview/api#changelanguage
// if you're using a language detector, do not define the lng option

interpolation: {
escapeValue: false, // react already safes from xss
},
});

export default i18n;
1 change: 0 additions & 1 deletion src/i18n/README.md

This file was deleted.

11 changes: 11 additions & 0 deletions src/i18n/en_US.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Resource } from "i18next";

const EN_US_TRANSLATIONS: Resource = {
translation: {
Problem: "Problem",
User: "User",
Contest: "Contest",
},
};

export default EN_US_TRANSLATIONS;
11 changes: 11 additions & 0 deletions src/i18n/zh_CN.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Resource } from "i18next";

const ZH_CN_TRANSLATIONS: Resource = {
translation: {
Problem: "问题",
User: "用户",
Contest: "竞赛",
},
};

export default ZH_CN_TRANSLATIONS;
6 changes: 4 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import App from "./App";
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { worker } from "./mocks/server";

import "./i18n";
import "./index.css";

console.log("Running in:", import.meta.env.MODE);
if (import.meta.env.MODE === "mock") {
worker.start();
Expand Down
6 changes: 4 additions & 2 deletions src/layouts/adminLayout/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "@heroicons/react/24/outline";
import { ChevronDownIcon } from "@heroicons/react/20/solid";
import { useNavigate } from "react-router-dom";
import { useTranslation } from "react-i18next";

const navigation = [
{ name: "Problem", href: "/admin/problem", icon: HomeIcon, current: true },
Expand All @@ -34,6 +35,7 @@ interface SidebarProps {
export default function Sidebar(props: SidebarProps) {
const [sidebarOpen, setSidebarOpen] = useState(false);
const navigate = useNavigate();
const { t } = useTranslation();

return (
<>
Expand Down Expand Up @@ -126,7 +128,7 @@ export default function Sidebar(props: SidebarProps) {
)}
aria-hidden="true"
/>
{item.name}
{t(item.name)}
</div>
</li>
))}
Expand Down Expand Up @@ -177,7 +179,7 @@ export default function Sidebar(props: SidebarProps) {
)}
aria-hidden="true"
/>
{item.name}
{t(item.name)}
</a>
</li>
))}
Expand Down

0 comments on commit 7e16f16

Please sign in to comment.