From 94e1b0fb0d730b21bde863f5a8244c254e72eaf7 Mon Sep 17 00:00:00 2001 From: ambar Date: Fri, 29 Mar 2024 16:28:39 +0800 Subject: [PATCH] feat: add `localeRedirect` option to theme config (#865) Co-authored-by: neverland --- .changeset/real-beans-cheer.md | 7 +++++++ .../docs/en/api/config/config-theme.mdx | 17 +++++++++++++++++ .../docs/zh/api/config/config-theme.mdx | 16 ++++++++++++++++ packages/shared/src/types/defaultTheme.ts | 12 +++++++++++- .../src/logic/useRedirect4FirstVisit.ts | 4 ++++ 5 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 .changeset/real-beans-cheer.md diff --git a/.changeset/real-beans-cheer.md b/.changeset/real-beans-cheer.md new file mode 100644 index 000000000..704716c4b --- /dev/null +++ b/.changeset/real-beans-cheer.md @@ -0,0 +1,7 @@ +--- +'@rspress/theme-default': minor +'@rspress/docs': minor +'@rspress/shared': minor +--- + +feat: add `localeRedirect` option to theme config diff --git a/packages/document/docs/en/api/config/config-theme.mdx b/packages/document/docs/en/api/config/config-theme.mdx index 082a40c77..e086854a4 100644 --- a/packages/document/docs/en/api/config/config-theme.mdx +++ b/packages/document/docs/en/api/config/config-theme.mdx @@ -574,3 +574,20 @@ export default defineConfig({ }, }); ``` + +## localeRedirect + +- Type: `'auto' | 'never'` +- Default: `'auto'` + +Whether to redirect to the locale closest to `window.navigator.language` when the user visits the site, the default is `auto`, which means that the user will be redirected on the first visit. If you set it to `never`, the user will not be redirected. For example: + +```ts title="rspress.config.ts" +import { defineConfig } from 'rspress/config'; + +export default defineConfig({ + themeConfig: { + localeRedirect: 'never', + }, +}); +``` diff --git a/packages/document/docs/zh/api/config/config-theme.mdx b/packages/document/docs/zh/api/config/config-theme.mdx index d7096418b..198081396 100644 --- a/packages/document/docs/zh/api/config/config-theme.mdx +++ b/packages/document/docs/zh/api/config/config-theme.mdx @@ -559,3 +559,19 @@ export default defineConfig({ }); ``` +## localeRedirect + +- Type: `'auto' | 'never'` +- Default: `'auto'` + +是否在访问网站时重定向到最接近 `window.navigator.language` 的语言,默认为 `auto` 表示首次访问时将重定向,`never` 表示不重定向。比如: + +```ts title="rspress.config.ts" +import { defineConfig } from 'rspress/config'; + +export default defineConfig({ + themeConfig: { + localeRedirect: 'never', + }, +}); +``` diff --git a/packages/shared/src/types/defaultTheme.ts b/packages/shared/src/types/defaultTheme.ts index f55b269c0..1fa654e80 100644 --- a/packages/shared/src/types/defaultTheme.ts +++ b/packages/shared/src/types/defaultTheme.ts @@ -94,6 +94,11 @@ export interface Config { * @default false */ enableScrollToTop?: boolean; + /** + * Whether to redirect to the closest locale when the user visits the site + * @default 'auto' + */ + localeRedirect?: 'auto' | 'never'; } /** @@ -160,7 +165,12 @@ export type Image = string | { src: string; alt?: string }; // sidebar ------------------------------------------------------------------- export interface Sidebar { - [path: string]: (SidebarGroup | SidebarItem | SidebarDivider | SidebarSectionHeader)[]; + [path: string]: ( + | SidebarGroup + | SidebarItem + | SidebarDivider + | SidebarSectionHeader + )[]; } export interface SidebarGroup { diff --git a/packages/theme-default/src/logic/useRedirect4FirstVisit.ts b/packages/theme-default/src/logic/useRedirect4FirstVisit.ts index 2b7b45492..b870f3b29 100644 --- a/packages/theme-default/src/logic/useRedirect4FirstVisit.ts +++ b/packages/theme-default/src/logic/useRedirect4FirstVisit.ts @@ -11,6 +11,10 @@ export function useRedirect4FirstVisit() { const langs = localeLanguages.map(item => item.lang) || []; const currentLang = page.lang; useEffect(() => { + const localeRedirect = siteData.themeConfig.localeRedirect ?? 'auto'; + if (localeRedirect !== 'auto') { + return; + } if (!defaultLang || process.env.TEST === '1') { // Check the window.navigator.language to determine the default language // If the default language is not the same as the current language, redirect to the default language