mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 02:08:55 +02:00
feat(theme): add queryString option to localeDropdown (#8915)
Co-authored-by: Sébastien Lorber <slorber@users.noreply.github.com>
This commit is contained in:
parent
4a4a8be062
commit
10f161d578
5 changed files with 13 additions and 1 deletions
|
@ -200,6 +200,7 @@ const LocaleDropdownNavbarItemSchema = NavbarItemBaseSchema.append({
|
||||||
type: Joi.string().equal('localeDropdown').required(),
|
type: Joi.string().equal('localeDropdown').required(),
|
||||||
dropdownItemsBefore: Joi.array().items(DropdownSubitemSchema).default([]),
|
dropdownItemsBefore: Joi.array().items(DropdownSubitemSchema).default([]),
|
||||||
dropdownItemsAfter: Joi.array().items(DropdownSubitemSchema).default([]),
|
dropdownItemsAfter: Joi.array().items(DropdownSubitemSchema).default([]),
|
||||||
|
queryString: Joi.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const SearchItemSchema = Joi.object({
|
const SearchItemSchema = Joi.object({
|
||||||
|
|
|
@ -1072,6 +1072,7 @@ declare module '@theme/NavbarItem/LocaleDropdownNavbarItem' {
|
||||||
export interface Props extends DropdownNavbarItemProps {
|
export interface Props extends DropdownNavbarItemProps {
|
||||||
readonly dropdownItemsBefore: LinkLikeNavbarItemProps[];
|
readonly dropdownItemsBefore: LinkLikeNavbarItemProps[];
|
||||||
readonly dropdownItemsAfter: LinkLikeNavbarItemProps[];
|
readonly dropdownItemsAfter: LinkLikeNavbarItemProps[];
|
||||||
|
readonly queryString?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function LocaleDropdownNavbarItem(props: Props): JSX.Element;
|
export default function LocaleDropdownNavbarItem(props: Props): JSX.Element;
|
||||||
|
|
|
@ -21,6 +21,7 @@ export default function LocaleDropdownNavbarItem({
|
||||||
mobile,
|
mobile,
|
||||||
dropdownItemsBefore,
|
dropdownItemsBefore,
|
||||||
dropdownItemsAfter,
|
dropdownItemsAfter,
|
||||||
|
queryString = '',
|
||||||
...props
|
...props
|
||||||
}: Props): JSX.Element {
|
}: Props): JSX.Element {
|
||||||
const {
|
const {
|
||||||
|
@ -35,7 +36,7 @@ export default function LocaleDropdownNavbarItem({
|
||||||
fullyQualified: false,
|
fullyQualified: false,
|
||||||
})}`;
|
})}`;
|
||||||
// preserve ?search#hash suffix on locale switches
|
// preserve ?search#hash suffix on locale switches
|
||||||
const to = `${baseTo}${search}${hash}`;
|
const to = `${baseTo}${search}${hash}${queryString}`;
|
||||||
return {
|
return {
|
||||||
label: localeConfigs[locale]!.label,
|
label: localeConfigs[locale]!.label,
|
||||||
lang: localeConfigs[locale]!.htmlLang,
|
lang: localeConfigs[locale]!.htmlLang,
|
||||||
|
|
|
@ -616,6 +616,7 @@ Accepted fields:
|
||||||
| `position` | <code>'left' \| 'right'</code> | `'left'` | The side of the navbar this item should appear on. |
|
| `position` | <code>'left' \| 'right'</code> | `'left'` | The side of the navbar this item should appear on. |
|
||||||
| `dropdownItemsBefore` | <code>[LinkLikeItem](#navbar-dropdown)[]</code> | `[]` | Add additional dropdown items at the beginning of the dropdown. |
|
| `dropdownItemsBefore` | <code>[LinkLikeItem](#navbar-dropdown)[]</code> | `[]` | Add additional dropdown items at the beginning of the dropdown. |
|
||||||
| `dropdownItemsAfter` | <code>[LinkLikeItem](#navbar-dropdown)[]</code> | `[]` | Add additional dropdown items at the end of the dropdown. |
|
| `dropdownItemsAfter` | <code>[LinkLikeItem](#navbar-dropdown)[]</code> | `[]` | Add additional dropdown items at the end of the dropdown. |
|
||||||
|
| `queryString` | `string` | `undefined` | The query string to be appended to the URL. |
|
||||||
|
|
||||||
```mdx-code-block
|
```mdx-code-block
|
||||||
</APITable>
|
</APITable>
|
||||||
|
|
|
@ -68,6 +68,14 @@ module.exports = {
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
:::tip
|
||||||
|
|
||||||
|
You can pass a query parameter that will be appended to the URL when a user changes the locale using the dropdown (e.g. `queryString: '?persistLocale=true'`).
|
||||||
|
|
||||||
|
This is useful for implementing an automatic locale detection on your server. For example, you can use this parameter to store the user's preferred locale in a cookie.
|
||||||
|
|
||||||
|
:::
|
||||||
|
|
||||||
### Start your site {#start-your-site}
|
### Start your site {#start-your-site}
|
||||||
|
|
||||||
Start your localized site in dev mode, using the locale of your choice:
|
Start your localized site in dev mode, using the locale of your choice:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue