fix: #398
This commit is contained in:
@@ -13,7 +13,7 @@ import AFFiNETextLogo from './affine-text-logo.png';
|
|||||||
import { HoverMenu } from './HoverMenu';
|
import { HoverMenu } from './HoverMenu';
|
||||||
import { MobileHeader } from './MobileHeader';
|
import { MobileHeader } from './MobileHeader';
|
||||||
|
|
||||||
import { options } from '../i18n';
|
import { LOCALES } from '../i18n';
|
||||||
|
|
||||||
export const AFFiNEHeader = () => {
|
export const AFFiNEHeader = () => {
|
||||||
const matches = useMediaQuery('(max-width: 1024px)');
|
const matches = useMediaQuery('(max-width: 1024px)');
|
||||||
@@ -140,15 +140,15 @@ export const AFFiNEHeader = () => {
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<Select
|
<Select
|
||||||
defaultValue="en"
|
defaultValue={i18n.language}
|
||||||
sx={{ display: matchesIPAD ? 'none' : 'intial' }}
|
sx={{ display: matchesIPAD ? 'none' : 'intial' }}
|
||||||
onChange={changeLanguage}
|
onChange={changeLanguage}
|
||||||
size="md"
|
size="md"
|
||||||
variant="plain"
|
variant="plain"
|
||||||
>
|
>
|
||||||
{options.map(option => (
|
{LOCALES.map(lang => (
|
||||||
<Option key={option.value} value={option.value}>
|
<Option key={lang.tag} value={lang.tag}>
|
||||||
{option.text}
|
{lang.originalName}
|
||||||
</Option>
|
</Option>
|
||||||
))}
|
))}
|
||||||
</Select>
|
</Select>
|
||||||
|
|||||||
@@ -1,25 +1,63 @@
|
|||||||
import i18next from 'i18next';
|
import i18next, { Resource } from 'i18next';
|
||||||
import { initReactI18next } from 'react-i18next';
|
import {
|
||||||
import en_US from './resources/en.json';
|
I18nextProvider,
|
||||||
import zh_CN from './resources/zh.json';
|
initReactI18next,
|
||||||
|
useTranslation,
|
||||||
|
} from 'react-i18next';
|
||||||
|
import { LOCALES } from './resources';
|
||||||
|
import type en_US from './resources/en.json';
|
||||||
|
|
||||||
const resources = {
|
// See https://react.i18next.com/latest/typescript
|
||||||
en: en_US,
|
declare module 'react-i18next' {
|
||||||
zh: zh_CN,
|
interface CustomTypeOptions {
|
||||||
} as const;
|
// custom namespace type if you changed it
|
||||||
|
// defaultNS: 'ns1';
|
||||||
|
// custom resources type
|
||||||
|
resources: {
|
||||||
|
en: typeof en_US;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'i18n_lng';
|
||||||
|
|
||||||
|
export { i18n, useTranslation, I18nProvider, LOCALES };
|
||||||
|
|
||||||
|
const resources = LOCALES.reduce<Resource>(
|
||||||
|
(acc, { tag, res }) => ({ ...acc, [tag]: { translation: res } }),
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
|
||||||
|
const fallbackLng = LOCALES[0].tag;
|
||||||
|
const standardizeLocale = (language: string) => {
|
||||||
|
if (LOCALES.find(locale => locale.tag === language)) return language;
|
||||||
|
if (
|
||||||
|
LOCALES.find(
|
||||||
|
locale => locale.tag === language.slice(0, 2).toLowerCase()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return language;
|
||||||
|
return fallbackLng;
|
||||||
|
};
|
||||||
|
|
||||||
|
const language = standardizeLocale(
|
||||||
|
localStorage.getItem(STORAGE_KEY) ?? navigator.language
|
||||||
|
);
|
||||||
|
|
||||||
|
const i18n = i18next.createInstance();
|
||||||
|
i18n.use(initReactI18next).init({
|
||||||
|
lng: language,
|
||||||
|
fallbackLng,
|
||||||
|
debug: process.env['NODE_ENV'] === 'development',
|
||||||
|
|
||||||
i18next.use(initReactI18next).init({
|
|
||||||
lng: 'en',
|
|
||||||
fallbackLng: 'en',
|
|
||||||
resources,
|
resources,
|
||||||
interpolation: {
|
interpolation: {
|
||||||
escapeValue: false, // not needed for react as it escapes by default
|
escapeValue: false, // not needed for react as it escapes by default
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export const options = [
|
i18n.on('languageChanged', lng => {
|
||||||
{ value: 'en', text: 'English' },
|
localStorage.setItem(STORAGE_KEY, lng);
|
||||||
{ value: 'zh', text: '简体中文' },
|
});
|
||||||
];
|
|
||||||
|
|
||||||
export { i18next };
|
const I18nProvider = I18nextProvider;
|
||||||
|
|||||||
Reference in New Issue
Block a user