diff --git a/src/i18n/en_US.ts b/src/i18n/en_US.ts index a9ce9cdd0..d59dc4008 100644 --- a/src/i18n/en_US.ts +++ b/src/i18n/en_US.ts @@ -2,6 +2,8 @@ import { Resource } from "i18next"; const EN_US_TRANSLATIONS: Resource = { translation: { + "English (US)": "English (US)", + "Simple Chinese": "Simple Chinese", Problem: "Problem", User: "User", Contest: "Contest", diff --git a/src/i18n.ts b/src/i18n/i18n.ts similarity index 80% rename from src/i18n.ts rename to src/i18n/i18n.ts index bfa791c84..92d455414 100644 --- a/src/i18n.ts +++ b/src/i18n/i18n.ts @@ -1,7 +1,12 @@ 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"; +import EN_US_TRANSLATIONS from "./en_US"; +import ZH_CN_TRANSLATIONS from "./zh_CN"; + +export const LANGUAGE_SELECTIONS = [ + { value: "en_US", label: "English (US)" }, + { value: "zh_CN", label: "Simple Chinese" }, +]; // the translations // (tip move them in a JSON file and import them, diff --git a/src/i18n/zh_CN.ts b/src/i18n/zh_CN.ts index f90552e7a..42ae02e9f 100644 --- a/src/i18n/zh_CN.ts +++ b/src/i18n/zh_CN.ts @@ -2,6 +2,8 @@ import { Resource } from "i18next"; const ZH_CN_TRANSLATIONS: Resource = { translation: { + "English (US)": "英语 (美式)", + "Simple Chinese": "简体中文", Problem: "问题", User: "用户", Contest: "竞赛", diff --git a/src/index.tsx b/src/index.tsx index 93962b25b..19e3984c8 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -4,7 +4,7 @@ import ReactDOM from "react-dom/client"; import reportWebVitals from "./reportWebVitals"; import { worker } from "./mocks/server"; -import "./i18n"; +import "./i18n/i18n"; import "./index.css"; console.log("Running in:", import.meta.env.MODE); diff --git a/src/layouts/LanguageMenu.tsx b/src/layouts/LanguageMenu.tsx new file mode 100644 index 000000000..24fc8fa3d --- /dev/null +++ b/src/layouts/LanguageMenu.tsx @@ -0,0 +1,54 @@ +import { Menu, Transition } from "@headlessui/react"; +import { Fragment } from "react"; +import { useTranslation } from "react-i18next"; +import { joinClasses } from "../utils/common"; +import { LanguageIcon } from "@heroicons/react/24/outline"; +import { changeLanguage } from "i18next"; +import { LANGUAGE_SELECTIONS } from "../i18n/i18n"; + +const LanguageMenu: React.FC = () => { + const { t } = useTranslation(); + + return ( + <> + {/* Profile dropdown */} + + + Open user menu + + + + + {LANGUAGE_SELECTIONS.map((item) => ( + + {({ active }) => ( +
{ + changeLanguage(item.value); + }} + > + {t(item.label)} +
+ )} +
+ ))} +
+
+
+ + ); +}; + +export default LanguageMenu; diff --git a/src/layouts/UserMenu.tsx b/src/layouts/UserMenu.tsx index 1b95917ef..9574f53bb 100644 --- a/src/layouts/UserMenu.tsx +++ b/src/layouts/UserMenu.tsx @@ -24,7 +24,7 @@ const UserMenu: React.FC = (props) => { <> {/* Profile dropdown */} - + Open user menu
-
+
+ {/* Profile dropdown */}
diff --git a/src/layouts/userLayout/Header.tsx b/src/layouts/userLayout/Header.tsx index cffbea830..2759727d3 100644 --- a/src/layouts/userLayout/Header.tsx +++ b/src/layouts/userLayout/Header.tsx @@ -9,6 +9,8 @@ import { } from "@heroicons/react/24/outline"; import UserMenu from "../UserMenu"; import { joinClasses } from "../../utils/common"; +import LanguageMenu from "../LanguageMenu"; +import { useTranslation } from "react-i18next"; const user = { name: "Tom Cook", @@ -35,6 +37,8 @@ const userNavigation = [ ]; export default function Header() { + const { t } = useTranslation(); + return ( {({ open }) => ( @@ -68,18 +72,21 @@ export default function Header() { aria-current={item.current ? "page" : undefined} > {item.icon} - {item.name} + {t(item.name)} ))}
- {/* Profile dropdown */} - +
+ + {/* Profile dropdown */} + +
{/* Mobile menu button */} @@ -110,7 +117,7 @@ export default function Header() { )} aria-current={item.current ? "page" : undefined} > - {item.name} + {t(item.name)} ))}