提交 53fa41da 编写于 作者: O oasis-cloud

chore: 本地多语言切换

上级 fb15e8e9
import React, { useState } from 'react'
import React, { useEffect, useState } from 'react'
import './demo-preview.scss'
import { useHistory } from 'react-router-dom'
import { useHistory, useLocation } from 'react-router-dom'
const DemoPreview = () => {
const history = useHistory()
const location = useLocation()
const [URL, setURL] = useState(history.location.pathname)
history.listen((location) => {
useEffect(() => {
setURL(location.pathname)
})
}, [location])
return (
<div className="doc-demo-preview">
......
......@@ -4,16 +4,20 @@ import { nav } from '@/config.json'
import { version } from '/package.json'
import config from '@/sites/config/env'
import './header.scss'
import { useHistory } from 'react-router-dom'
import { useHistory, useLocation } from 'react-router-dom'
import '@/packages/popover/popover.scss'
import Popover from '@/packages/popover'
const Header = () => {
const history = useHistory()
const location = useLocation()
const [currLang, setCurrLang] = useState({})
const toHome = () => {
history.replace('/')
}
console.log(history.location.pathname)
useEffect(() => {
let packages = [] as any[]
nav.forEach((item) => {
......@@ -21,6 +25,11 @@ const Header = () => {
})
}, [])
useEffect(() => {
const lang = langs.filter((l) => location.pathname.indexOf(l.locale) > -1)[0]
setCurrLang(lang)
}, [location])
const langs = [
{ name: '中文', locale: 'zh-CN' },
{ name: '中文(繁体)', locale: 'zh-TW' },
......@@ -32,8 +41,7 @@ const Header = () => {
const handleSwitchLocale = (e: any) => {
const classList: string[] = [].slice.call(e.target.classList)
if (classList.indexOf('curr-lang') > -1) {
setVisible(!visible)
return
return setVisible(!visible)
}
const name = e.target.innerText
setVisible(!visible)
......@@ -48,8 +56,6 @@ const Header = () => {
window.location.href = link
}
const currLang = langs.filter((l) => history.location.pathname.indexOf(l.locale) > -1)[0]
return (
<div className="doc-header doc-header-black">
<div className="header-logo">
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册