This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6590f8
commit 299a9f9
Showing
3 changed files
with
134 additions
and
1 deletion.
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
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
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React from 'react'; | ||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; | ||
import { useAlternatePageUtils } from '@docusaurus/theme-common/internal'; | ||
import { useLocation } from '@docusaurus/router'; | ||
import DropdownNavbarItem from '@theme/NavbarItem/DropdownNavbarItem'; | ||
import type { LinkLikeNavbarItemProps } from '@theme/NavbarItem'; | ||
import type { Props } from '@theme/NavbarItem/LocaleDropdownNavbarItem'; | ||
import classnames from 'classnames'; | ||
|
||
export default function LocaleDropdownNavbarItem({ | ||
dropdownItemsBefore, | ||
dropdownItemsAfter, | ||
...props | ||
}: Props): JSX.Element { | ||
const { | ||
i18n: { currentLocale, locales, localeConfigs }, | ||
} = useDocusaurusContext(); | ||
const alternatePageUtils = useAlternatePageUtils(); | ||
const { search, hash } = useLocation(); | ||
|
||
const localeItems = locales.map((locale): LinkLikeNavbarItemProps => { | ||
const baseTo = `pathname://${alternatePageUtils.createUrl({ | ||
locale, | ||
fullyQualified: false, | ||
})}`; | ||
// preserve ?search#hash suffix on locale switches | ||
const to = `${baseTo}${search}${hash}`; | ||
return { | ||
label: localeConfigs[locale].label, | ||
lang: localeConfigs[locale].htmlLang, | ||
to, | ||
target: '_self', | ||
autoAddBaseUrl: false, | ||
className: classnames({ 'dropdown__link--active': locale === currentLocale }), | ||
}; | ||
}); | ||
|
||
const getShortNames = (locale) => { | ||
switch (locale) { | ||
case 'en': | ||
return 'EN'; | ||
case 'es': | ||
return 'ES'; | ||
case 'zh-Hans': | ||
return '简体'; | ||
case 'zh-Hant': | ||
return '繁體'; | ||
case 'fr': | ||
return 'FR'; | ||
case 'de': | ||
return 'DE'; | ||
case 'vi': | ||
return 'VI'; | ||
case 'th': | ||
return 'TH'; | ||
default: | ||
return 'EN'; | ||
} | ||
}; | ||
|
||
const items = [...dropdownItemsBefore, ...localeItems, ...dropdownItemsAfter]; | ||
const dropdownLabel = getShortNames(currentLocale); | ||
|
||
return <DropdownNavbarItem {...props} label={<>{dropdownLabel}</>} items={items} />; | ||
} |