-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(v2): Add localeDropdown navbar item type + i18n localeConfigs fi…
…eld (#3916) * Add localeDropdown navbar item type * fix type + add localeConfigs test
- Loading branch information
Showing
12 changed files
with
189 additions
and
4 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
74 changes: 74 additions & 0 deletions
74
packages/docusaurus-theme-classic/src/theme/NavbarItem/LocaleDropdownNavbarItem.tsx
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,74 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import DefaultNavbarItem from './DefaultNavbarItem'; | ||
import type {Props} from '@theme/NavbarItem/LocaleDropdownNavbarItem'; | ||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; | ||
import {useLocation} from '@docusaurus/router'; | ||
|
||
export default function LocaleDropdownNavbarItem({ | ||
mobile, | ||
...props | ||
}: Props): JSX.Element { | ||
const { | ||
siteConfig: {baseUrl}, | ||
i18n: {defaultLocale, currentLocale, locales, localeConfigs}, | ||
} = useDocusaurusContext(); | ||
const {pathname} = useLocation(); | ||
|
||
function getLocaleLabel(locale) { | ||
return localeConfigs[locale].label; | ||
} | ||
|
||
// TODO Docusaurus could offer some APIs to we should probably | ||
const baseUrlUnlocalized = | ||
currentLocale === defaultLocale | ||
? baseUrl | ||
: baseUrl.replace(`/${currentLocale}/`, '/'); | ||
|
||
const pathnameSuffix = pathname.replace(baseUrl, ''); | ||
|
||
function getLocalizedBaseUrl(locale) { | ||
return locale === defaultLocale | ||
? `${baseUrlUnlocalized}` | ||
: `${baseUrlUnlocalized}${locale}/`; | ||
} | ||
|
||
const items = locales.map((locale) => { | ||
const to = `${getLocalizedBaseUrl(locale)}${pathnameSuffix}`; | ||
console.log({ | ||
locale, | ||
to, | ||
pathname, | ||
baseUrl, | ||
baseUrlUnlocalized, | ||
pathnameSuffix, | ||
}); | ||
|
||
return { | ||
isNavLink: true, | ||
label: getLocaleLabel(locale), | ||
to: `pathname://${to}`, | ||
target: '_self', | ||
autoAddBaseUrl: false, | ||
className: locale === currentLocale ? 'dropdown__link--active' : '', | ||
}; | ||
}); | ||
|
||
// Mobile is handled a bit differently | ||
const dropdownLabel = mobile ? 'Languages' : getLocaleLabel(currentLocale); | ||
|
||
return ( | ||
<DefaultNavbarItem | ||
{...props} | ||
mobile={mobile} | ||
label={dropdownLabel} | ||
items={items} | ||
/> | ||
); | ||
} |
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
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
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
Oops, something went wrong.