Skip to content

Commit

Permalink
feat: add localeRedirect option to theme config (#865)
Browse files Browse the repository at this point in the history
Co-authored-by: neverland <[email protected]>
  • Loading branch information
ambar and chenjiahan authored Mar 29, 2024
1 parent 0c92bcc commit 94e1b0f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/real-beans-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@rspress/theme-default': minor
'@rspress/docs': minor
'@rspress/shared': minor
---

feat: add `localeRedirect` option to theme config
17 changes: 17 additions & 0 deletions packages/document/docs/en/api/config/config-theme.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
});
```
16 changes: 16 additions & 0 deletions packages/document/docs/zh/api/config/config-theme.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
});
```
12 changes: 11 additions & 1 deletion packages/shared/src/types/defaultTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

/**
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions packages/theme-default/src/logic/useRedirect4FirstVisit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 94e1b0f

Please sign in to comment.