Skip to content

Commit 52d6339

Browse files
committed
fix:单个文件一种语言
1 parent 5485735 commit 52d6339

File tree

9 files changed

+36
-34
lines changed

9 files changed

+36
-34
lines changed

website/src/Layout.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ import GitHubCorners from '@uiw/react-github-corners';
44
import '@wcj/dark-mode';
55
import styles from './Layout.module.less';
66
import Language from './language/Select';
7+
import { useTranslation } from 'react-i18next';
8+
79
export function Layout() {
10+
const i18next = useTranslation();
811
return (
912
<Fragment>
1013
<GitHubCorners target="__blank" fixed href="https://github.com/kktjs/markdown-react-code-preview-loader" />
1114
<header className={styles.header}>
1215
<nav>
1316
<NavLink to="/" replace>
14-
Docuemnt
17+
{i18next.t('docuemnt')}
1518
</NavLink>
1619
<NavLink to="/example" replace>
17-
Example
20+
{i18next.t('example')}
1821
</NavLink>
1922
<dark-mode permanent></dark-mode>
2023
<Language />

website/src/components/useMdData/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useEffect, useState } from 'react';
22
import { CodeBlockData } from 'markdown-react-code-preview-loader';
33
import { useTranslation } from 'react-i18next';
44

5-
const useMdData = (path: (lang: string) => Promise<{ default: CodeBlockData }>, name: string = 'language') => {
5+
const useMdData = (path: (lang: string) => Promise<{ default: CodeBlockData }>, name: string = 'md-language-name') => {
66
const init = useTranslation();
77
const [mdData, setMdData] = useState<CodeBlockData>({
88
source: '',

website/src/language/Select.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import { Select } from 'uiw';
44
import { useTranslation } from 'react-i18next';
55

66
const Language = () => {
7-
const { i18n } = useTranslation();
7+
const { i18n, t } = useTranslation();
88
const Options = React.useMemo(() => {
9-
return Object.entries(languageData).map(([key, item]) => {
9+
return (languageData || []).map((key) => {
1010
return (
1111
<Select.Option key={key} value={key}>
12-
{item.label}
12+
{t(key)}
1313
</Select.Option>
1414
);
1515
});
1616
// eslint-disable-next-line react-hooks/exhaustive-deps
17-
}, [JSON.stringify(languageData)]);
17+
}, [JSON.stringify(languageData), i18n.language]);
1818

1919
return (
2020
<div style={{ width: 100 }}>

website/src/language/i18n.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
import i18n from 'i18next';
22
import { initReactI18next } from 'react-i18next';
3-
import LanguageData from './language.json';
3+
4+
import EN from './locale/en.json';
5+
import ZH from './locale/zh.json';
6+
47
// the translations
58
// (tip move them in a JSON file and import them,
69
// or even better, manage them separated from your code: https://react.i18next.com/guides/multiple-translation-files)
710
const resources: Record<string, { translation: { language: string } }> = {};
8-
Object.entries(LanguageData).forEach(([key, item]) => {
9-
const { value, label, ...rest } = item;
10-
resources[key] = {
11-
translation: {
12-
language: item.value,
13-
...rest,
14-
},
15-
};
16-
});
1711

1812
i18n
1913
.use(initReactI18next) // passes i18n down to react-i18next
@@ -22,6 +16,10 @@ i18n
2216
lng: 'en', // language to use, more information here: https://www.i18next.com/overview/configuration-options#languages-namespaces-resources
2317
// you can use the i18n.changeLanguage function to change the language manually: https://www.i18next.com/overview/api#changelanguage
2418
// if you're using a language detector, do not define the lng option
19+
lowerCaseLng: true,
2520
});
2621

22+
i18n.addResources('zh', 'translation', ZH);
23+
i18n.addResources('en', 'translation', EN);
24+
2725
export default i18n;

website/src/language/language.json

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1 @@
1-
{
2-
"zh": {
3-
"label": "中文",
4-
"value": "-zh",
5-
"markdown-react-code-preview-loader": "README-zh",
6-
"App": "App-zh"
7-
},
8-
"en": {
9-
"label": "英文",
10-
"value": "",
11-
"markdown-react-code-preview-loader": "README",
12-
"App": "App"
13-
}
14-
}
1+
["zh", "en"]

website/src/language/locale/en.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"md-language-name": "",
3+
"zh": "chinese",
4+
"en": "english",
5+
"docuemnt": "Docuemnt",
6+
"example": "Example"
7+
}

website/src/language/locale/zh.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"md-language-name": "-zh",
3+
"zh": "中文",
4+
"en": "英文",
5+
"docuemnt": "文档",
6+
"example": "案例"
7+
}

website/src/pages/docs/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import MarkdownPreview from '@uiw/react-markdown-preview';
22
import useMdData from './../../components/useMdData';
33
export function HomePage() {
44
const mdData = useMdData((path) => {
5-
return import(`markdown-react-code-preview-loader/${path}.md`);
6-
}, 'markdown-react-code-preview-loader');
5+
return import(`markdown-react-code-preview-loader/README${path}.md`);
6+
});
77
return <MarkdownPreview source={mdData.source} />;
88
}

website/src/pages/example/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const getMetaData = (meta: string) => {
1414
};
1515

1616
export function ExamplePage() {
17-
const mdData = useMdData((path) => import(`./${path}.md`), 'App');
17+
const mdData = useMdData((path) => import(`./App${path}.md`));
1818
return (
1919
<div>
2020
<MarkdownPreview

0 commit comments

Comments
 (0)