Skip to content

Commit 7da851d

Browse files
authored
feat: Add language items (#2103)
* fix: redirects * fix: redirects * feat: add language items
1 parent cbdd248 commit 7da851d

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

docusaurus.config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const config: Config = {
4848
// may want to replace "en" with "zh-Hans".
4949
i18n: {
5050
defaultLocale: lang,
51-
locales: [lang],
51+
locales: ['en', 'zh'],
5252
localeConfigs: {
5353
en: {
5454
label: "English",
@@ -276,6 +276,10 @@ const config: Config = {
276276
label: "Releases",
277277
position: "right",
278278
},
279+
{
280+
type: 'localeDropdown',
281+
position: 'right',
282+
},
279283
// { to: '/blog', label: 'Blog', position: 'left' }, // or position: 'right'
280284
// {
281285
// to: "/download",
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import React, { type ReactNode } from "react";
2+
import Link from "@docusaurus/Link";
3+
import useBaseUrl from "@docusaurus/useBaseUrl";
4+
import isInternalUrl from "@docusaurus/isInternalUrl";
5+
import { isRegexpStringMatch } from "@docusaurus/theme-common";
6+
import IconExternalLink from "@theme/Icon/ExternalLink";
7+
import type { Props } from "@theme/NavbarItem/NavbarNavLink";
8+
import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
9+
const EN_DOCS_HOME_LINK = "https://docs.databend.com/";
10+
const CN_DOCS_HOME_LINK = "https://docs.databend.cn/";
11+
12+
export default function NavbarNavLink({
13+
activeBasePath,
14+
activeBaseRegex,
15+
to,
16+
href,
17+
label,
18+
html,
19+
isDropdownLink,
20+
prependBaseUrlToHref,
21+
...props
22+
}: Props): ReactNode {
23+
const {
24+
siteConfig: {
25+
customFields: { isChina },
26+
},
27+
} = useDocusaurusContext();
28+
29+
function replacePathname(url: string) {
30+
if (isChina) {
31+
if (url?.startsWith("pathname:///en/")) {
32+
return url.replace("pathname:///en/", EN_DOCS_HOME_LINK);
33+
} else if (url?.startsWith("pathname:///")) {
34+
return url.replace("pathname:///", CN_DOCS_HOME_LINK);
35+
}
36+
} else {
37+
if (url?.startsWith("pathname:///zh/")) {
38+
return url.replace("pathname:///zh/", CN_DOCS_HOME_LINK);
39+
} else if (url?.startsWith("pathname:///")) {
40+
return url.replace("pathname:///", EN_DOCS_HOME_LINK);
41+
}
42+
}
43+
44+
return url;
45+
}
46+
47+
// TODO all this seems hacky
48+
// {to: 'version'} should probably be forbidden, in favor of {to: '/version'}
49+
const toUrl = useBaseUrl(to);
50+
console.log(toUrl);
51+
const activeBaseUrl = useBaseUrl(activeBasePath);
52+
const normalizedHref = useBaseUrl(href, { forcePrependBaseUrl: true });
53+
const isExternalLink = label && href && !isInternalUrl(href);
54+
55+
// Link content is set through html XOR label
56+
const linkContentProps = html
57+
? { dangerouslySetInnerHTML: { __html: html } }
58+
: {
59+
children: (
60+
<>
61+
{label}
62+
{isExternalLink && (
63+
<IconExternalLink
64+
{...(isDropdownLink && { width: 12, height: 12 })}
65+
/>
66+
)}
67+
</>
68+
),
69+
};
70+
71+
if (href) {
72+
return (
73+
<Link
74+
href={prependBaseUrlToHref ? normalizedHref : href}
75+
{...props}
76+
{...linkContentProps}
77+
/>
78+
);
79+
}
80+
81+
return (
82+
<Link
83+
to={replacePathname(toUrl)}
84+
isNavLink
85+
{...((activeBasePath || activeBaseRegex) && {
86+
isActive: (_match, location) =>
87+
activeBaseRegex
88+
? isRegexpStringMatch(activeBaseRegex, location.pathname)
89+
: location.pathname.startsWith(activeBaseUrl),
90+
})}
91+
{...props}
92+
{...linkContentProps}
93+
/>
94+
);
95+
}

0 commit comments

Comments
 (0)