File tree 9 files changed +36
-34
lines changed
9 files changed +36
-34
lines changed Original file line number Diff line number Diff line change @@ -4,17 +4,20 @@ import GitHubCorners from '@uiw/react-github-corners';
4
4
import '@wcj/dark-mode' ;
5
5
import styles from './Layout.module.less' ;
6
6
import Language from './language/Select' ;
7
+ import { useTranslation } from 'react-i18next' ;
8
+
7
9
export function Layout ( ) {
10
+ const i18next = useTranslation ( ) ;
8
11
return (
9
12
< Fragment >
10
13
< GitHubCorners target = "__blank" fixed href = "https://github.com/kktjs/markdown-react-code-preview-loader" />
11
14
< header className = { styles . header } >
12
15
< nav >
13
16
< NavLink to = "/" replace >
14
- Docuemnt
17
+ { i18next . t ( 'docuemnt' ) }
15
18
</ NavLink >
16
19
< NavLink to = "/example" replace >
17
- Example
20
+ { i18next . t ( 'example' ) }
18
21
</ NavLink >
19
22
< dark-mode permanent > </ dark-mode >
20
23
< Language />
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import { useEffect, useState } from 'react';
2
2
import { CodeBlockData } from 'markdown-react-code-preview-loader' ;
3
3
import { useTranslation } from 'react-i18next' ;
4
4
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 ' ) => {
6
6
const init = useTranslation ( ) ;
7
7
const [ mdData , setMdData ] = useState < CodeBlockData > ( {
8
8
source : '' ,
Original file line number Diff line number Diff line change @@ -4,17 +4,17 @@ import { Select } from 'uiw';
4
4
import { useTranslation } from 'react-i18next' ;
5
5
6
6
const Language = ( ) => {
7
- const { i18n } = useTranslation ( ) ;
7
+ const { i18n, t } = useTranslation ( ) ;
8
8
const Options = React . useMemo ( ( ) => {
9
- return Object . entries ( languageData ) . map ( ( [ key , item ] ) => {
9
+ return ( languageData || [ ] ) . map ( ( key ) => {
10
10
return (
11
11
< Select . Option key = { key } value = { key } >
12
- { item . label }
12
+ { t ( key ) }
13
13
</ Select . Option >
14
14
) ;
15
15
} ) ;
16
16
// eslint-disable-next-line react-hooks/exhaustive-deps
17
- } , [ JSON . stringify ( languageData ) ] ) ;
17
+ } , [ JSON . stringify ( languageData ) , i18n . language ] ) ;
18
18
19
19
return (
20
20
< div style = { { width : 100 } } >
Original file line number Diff line number Diff line change 1
1
import i18n from 'i18next' ;
2
2
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
+
4
7
// the translations
5
8
// (tip move them in a JSON file and import them,
6
9
// or even better, manage them separated from your code: https://react.i18next.com/guides/multiple-translation-files)
7
10
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
- } ) ;
17
11
18
12
i18n
19
13
. use ( initReactI18next ) // passes i18n down to react-i18next
22
16
lng : 'en' , // language to use, more information here: https://www.i18next.com/overview/configuration-options#languages-namespaces-resources
23
17
// you can use the i18n.changeLanguage function to change the language manually: https://www.i18next.com/overview/api#changelanguage
24
18
// if you're using a language detector, do not define the lng option
19
+ lowerCaseLng : true ,
25
20
} ) ;
26
21
22
+ i18n . addResources ( 'zh' , 'translation' , ZH ) ;
23
+ i18n . addResources ( 'en' , 'translation' , EN ) ;
24
+
27
25
export default i18n ;
Original file line number Diff line number Diff line change 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" ]
Original file line number Diff line number Diff line change
1
+ {
2
+ "md-language-name" : " " ,
3
+ "zh" : " chinese" ,
4
+ "en" : " english" ,
5
+ "docuemnt" : " Docuemnt" ,
6
+ "example" : " Example"
7
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "md-language-name" : " -zh" ,
3
+ "zh" : " 中文" ,
4
+ "en" : " 英文" ,
5
+ "docuemnt" : " 文档" ,
6
+ "example" : " 案例"
7
+ }
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import MarkdownPreview from '@uiw/react-markdown-preview';
2
2
import useMdData from './../../components/useMdData' ;
3
3
export function HomePage ( ) {
4
4
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
+ } ) ;
7
7
return < MarkdownPreview source = { mdData . source } /> ;
8
8
}
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ const getMetaData = (meta: string) => {
14
14
} ;
15
15
16
16
export function ExamplePage ( ) {
17
- const mdData = useMdData ( ( path ) => import ( `./${ path } .md` ) , 'App' ) ;
17
+ const mdData = useMdData ( ( path ) => import ( `./App ${ path } .md` ) ) ;
18
18
return (
19
19
< div >
20
20
< MarkdownPreview
You can’t perform that action at this time.
0 commit comments