-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: match browser default language error (#291)
close #290
- Loading branch information
1 parent
4881b74
commit b945c45
Showing
2 changed files
with
23 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,42 @@ | ||
import React, {useEffect} from 'react'; | ||
import { useEffect } from 'react'; | ||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; | ||
|
||
const LANGUAGE_PREFERENCE_KEY = '_lang_browser_'; | ||
|
||
export default function BrowserLanguageRedirect() { | ||
const { | ||
siteConfig: { | ||
i18n: {defaultLocale, locales}, | ||
i18n: { defaultLocale }, | ||
}, | ||
} = useDocusaurusContext(); | ||
|
||
useEffect(() => { | ||
window.addEventListener('languagechange', () => { | ||
localStorage.setItem(LANGUAGE_PREFERENCE_KEY, navigator.language); | ||
}) | ||
|
||
const currentPath = window.location.pathname; | ||
const storageLocale = localStorage.getItem('_lang_user_') || localStorage.getItem(LANGUAGE_PREFERENCE_KEY); | ||
const storageLocaleIsDefault = storageLocale === defaultLocale; | ||
|
||
if (storageLocale) { | ||
if (storageLocaleIsDefault) { | ||
if (currentPath === '/') return; | ||
return; | ||
} else { | ||
if (currentPath.startsWith(`/${storageLocale}`)) { | ||
return; | ||
} else { | ||
window.location.pathname = `/${storageLocale}${currentPath}`; | ||
return; | ||
} | ||
|
||
const isZhCN = navigator.language.toLowerCase() === 'zh-cn'; | ||
const isStoredZhCN = storageLocale === 'zh-CN'; | ||
|
||
if ((isZhCN || isStoredZhCN) && !currentPath.startsWith('/zh-CN')) { | ||
if (isZhCN) { | ||
localStorage.setItem(LANGUAGE_PREFERENCE_KEY, 'zh-CN'); | ||
} | ||
} else { | ||
const browserLanguage = navigator.language.toLowerCase(); | ||
const matchedLocale = locales.find(locale => | ||
browserLanguage === locale.toLowerCase() || | ||
browserLanguage.startsWith(locale.toLowerCase() + '-') | ||
); | ||
if (matchedLocale) { | ||
localStorage.setItem(LANGUAGE_PREFERENCE_KEY, matchedLocale); | ||
window.location.pathname = `/${matchedLocale}${currentPath}`; | ||
} else { | ||
window.location.pathname = currentPath; | ||
if (isStoredZhCN || storageLocale === null) { | ||
window.location.pathname = `/zh-CN${currentPath}`; | ||
} | ||
} | ||
|
||
// remove the event listener | ||
const handleLanguageChange = () => { | ||
if (navigator.language.toLowerCase() === 'zh-cn') { | ||
localStorage.setItem(LANGUAGE_PREFERENCE_KEY, 'zh-CN'); | ||
} | ||
}; | ||
window.addEventListener('languagechange', handleLanguageChange); | ||
|
||
return () => { | ||
window.removeEventListener('languagechange', () => { | ||
localStorage.setItem(LANGUAGE_PREFERENCE_KEY, navigator.language); | ||
}) | ||
} | ||
}, []); | ||
window.removeEventListener('languagechange', handleLanguageChange); | ||
}; | ||
}, [defaultLocale]); | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters