File tree Expand file tree Collapse file tree 9 files changed +36
-34
lines changed
Expand file tree Collapse file tree 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';
44import '@wcj/dark-mode' ;
55import styles from './Layout.module.less' ;
66import Language from './language/Select' ;
7+ import { useTranslation } from 'react-i18next' ;
8+
79export 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 />
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import { useEffect, useState } from 'react';
22import { CodeBlockData } from 'markdown-react-code-preview-loader' ;
33import { 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 : '' ,
Original file line number Diff line number Diff line change @@ -4,17 +4,17 @@ import { Select } from 'uiw';
44import { useTranslation } from 'react-i18next' ;
55
66const 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 } } >
Original file line number Diff line number Diff line change 11import i18n from 'i18next' ;
22import { 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)
710const 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
1812i18n
1913 . use ( initReactI18next ) // passes i18n down to react-i18next
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+
2725export 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';
22import useMdData from './../../components/useMdData' ;
33export 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}
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ const getMetaData = (meta: string) => {
1414} ;
1515
1616export 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
You can’t perform that action at this time.
0 commit comments