Skip to content

Commit 5752663

Browse files
committed
add a language switcher
1 parent f3cd7d1 commit 5752663

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

docusaurus.config.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ const config: Config = {
4141
// may want to replace "en" with "zh-Hans".
4242
i18n: {
4343
defaultLocale: 'ja',
44-
locales: ['ja'],
44+
locales: ['ja', 'en'],
45+
localeConfigs: {
46+
en: {
47+
label: 'English',
48+
},
49+
ja: {
50+
label: '日本語',
51+
},
52+
},
4553
},
4654

4755
presets: [
@@ -136,6 +144,10 @@ const config: Config = {
136144
label: 'JDL Studio',
137145
position: 'right',
138146
},
147+
{
148+
type: 'localeDropdown',
149+
position: 'right',
150+
},
139151
],
140152
},
141153
prism: {
@@ -178,6 +190,13 @@ const config: Config = {
178190
}),
179191
},
180192
],
193+
194+
scripts: [
195+
{
196+
src: '/redirect.js',
197+
async: true,
198+
},
199+
],
181200
};
182201

183202
export default config;

redirects.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ const redirectsPlugin = [
201201
{
202202
from: '/tips.html',
203203
to: '/tips/',
204-
},
204+
},
205205
],
206206
},
207207
];

src/redirect.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// https://docusaurus.io/docs/api/docusaurus-config#i18n
2+
// workaround for 'baseUrl—customization of base URL is a work-in-progress.'
3+
export default function onRouteUpdate({ location }) {
4+
const prefix = '/jp/en/';
5+
if (location.pathname.startsWith(prefix)) {
6+
const newPath = location.pathname.replace(prefix, '/');
7+
window.location.replace(newPath);
8+
}
9+
}

src/theme/Navbar/Layout/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import NavbarMobileSidebar from '@theme/Navbar/MobileSidebar';
1010
import type { Props } from '@theme/Navbar/Layout';
1111

1212
import styles from './styles.module.scss';
13+
import onRouteUpdate from '../../../redirect';
1314

1415
function NavbarBackdrop(props: ComponentProps<'div'>) {
1516
return (
@@ -22,6 +23,11 @@ function NavbarBackdrop(props: ComponentProps<'div'>) {
2223
}
2324

2425
export default function NavbarLayout({ children }: Props): JSX.Element {
26+
27+
React.useEffect(() => {
28+
onRouteUpdate({ location: window.location });
29+
}, []);
30+
2531
const {
2632
navbar: { hideOnScroll, style },
2733
} = useThemeConfig();

0 commit comments

Comments
 (0)