console/frontend/I18N.md
This project has been set up with internationalization (i18n) using the i18next and react-i18next libraries.
src/i18n/index.ts - Main i18n configurationsrc/locales/en.js - English translationssrc/locales/zh.js - Chinese translationssrc/hooks/useTranslation.ts - Custom hook combining react-i18next with our locale storesrc/components/LanguageSwitcher/index.tsx - Language switcher componentimport { useTranslation } from 'react-i18next';
const MyComponent = () => {
const { t } = useTranslation();
return <div>{t('keyName')}</div>;
};
import { useTranslation } from 'react-i18next';
import dayjs from 'dayjs';
const MyComponent = () => {
const { t } = useTranslation();
const currentDate = dayjs();
return <div>{t('myMessage', { ts: currentDate })}</div>;
};
import { useTranslation } from 'react-i18next';
const MyComponent = () => {
const { i18n } = useTranslation();
const changeLanguage = lng => {
i18n.changeLanguage(lng);
};
return (
<div>
<button onClick={() => changeLanguage('en')}>English</button>
<button onClick={() => changeLanguage('zh')}>中文</button>
</div>
);
};
Our custom hook combines i18next with our Zustand store:
import useTranslation from '@/hooks/useTranslation';
const MyComponent = () => {
const { t, locale, toggleLocale, isZh, isEn } = useTranslation();
return (
<div>
<h1>{t('title')}</h1>
<button onClick={toggleLocale}>
{isZh ? 'Switch to English' : '切换到中文'}
</button>
</div>
);
};
src/locales/en.js and src/locales/zh.jst('yourNewKey')The language switcher component is available at src/components/LanguageSwitcher/index.tsx and can be imported and used anywhere in the application.
import LanguageSwitcher from '@/components/LanguageSwitcher';
const MyComponent = () => {
return (
<div>
<h1>My Page</h1>
<LanguageSwitcher />
</div>
);
};