Skip to content

Commit 49574a9

Browse files
authored
feat: i18n link (#2137)
1 parent 041cb0a commit 49574a9

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ For an example, please refer to the [gRPC-SDK dictionary](docs/user-guide/ingest
214214
## Build and preview the docs locally
215215

216216
We highly encourage you to preview your changes locally before submitting a pull request.
217-
This project requires Node.js version 18.0.0 or higher.
217+
This project requires Node.js version 20.x or higher.
218218
Use `npm install -g pnpm` to install package manager, and start a local server with the following commands:
219219

220220
```shell
@@ -230,7 +230,7 @@ To preview the documentation in a specific language, use command `pnpm run start
230230
For example:
231231

232232
```cmd
233-
pnpm run start --locale zh
233+
DOC_LANG=zh pnpm start
234234
```
235235

236236
## PR title check

docusaurus.config.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,15 @@ const config: Config = {
160160
sidebarPath: './sidebars.ts',
161161
// Please change this to your repo.
162162
// Remove this to remove the "edit this page" links.
163-
editUrl:
164-
'https://github.com/GreptimeTeam/docs/blob/main',
163+
editUrl: ({ locale, version, versionDocsDirPath, docPath }) => {
164+
if (locale === 'zh') {
165+
// Chinese locale: i18n folder, versioned by current|version-<x>
166+
const versionDir = version === 'current' ? 'current' : `version-${version}`;
167+
return `https://github.com/GreptimeTeam/docs/edit/main/i18n/zh/docusaurus-plugin-content-docs/${versionDir}/${docPath}`;
168+
}
169+
// English locale: use Docusaurus-provided versionDocsDirPath
170+
return `https://github.com/GreptimeTeam/docs/edit/main/${versionDocsDirPath}/${docPath}`;
171+
},
165172
routeBasePath: '/',
166173
exclude: [
167174
'db-cloud-shared/**',
@@ -231,6 +238,21 @@ const config: Config = {
231238
trailingSlash: true,
232239
plugins: [
233240
['docusaurus-biel', bielMetaMap[locale]],
241+
function injectLocaleSwitchScript() {
242+
return {
243+
name: 'inject-locale-switch-script',
244+
injectHtmlTags() {
245+
return {
246+
headTags: [
247+
{
248+
tagName: 'script',
249+
attributes: { src: '/js/locale-switch.js' }
250+
}
251+
]
252+
};
253+
}
254+
};
255+
}
234256
],
235257

236258
themeConfig: {
@@ -266,7 +288,7 @@ const config: Config = {
266288
{
267289
label: locale === 'en' ? '中文' : 'English',
268290
to: locale === 'en' ? 'https://docs.greptime.cn' : 'https://docs.greptime.com',
269-
className: 'dropdown__link',
291+
className: 'dropdown__link locale-switch-link',
270292
},
271293
],
272294
},

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"scripts": {
66
"docusaurus": "docusaurus",
77
"start": "docusaurus start",
8+
"start:zh": "DOC_LANG=zh pnpm start",
89
"build": "docusaurus build",
910
"swizzle": "docusaurus swizzle",
1011
"deploy": "docusaurus deploy",

static/js/locale-switch.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
(function() {
2+
if (typeof window === 'undefined') return;
3+
4+
function updateLocaleSwitchHref() {
5+
var link = document.querySelector('.locale-switch-link');
6+
if (!link) return;
7+
try {
8+
var current = new URL(window.location.href);
9+
var target = new URL(link.href);
10+
link.href = current.protocol + '//' + target.host + current.pathname + current.search + current.hash;
11+
} catch (e) {
12+
// ignore
13+
}
14+
}
15+
16+
17+
// Intercept clicks on the locale switch link to rewrite URL immediately
18+
function handleLocaleSwitchClick(event) {
19+
var link = event.target.closest('.locale-switch-link');
20+
if (!link) return;
21+
22+
try {
23+
updateLocaleSwitchHref();
24+
} catch (e) {}
25+
}
26+
27+
// Set up click listener on document to catch all clicks
28+
document.addEventListener('click', handleLocaleSwitchClick, true);
29+
30+
// Initial update on load
31+
setTimeout(updateLocaleSwitchHref, 1000);
32+
33+
if ('navigation' in window && window.navigation) {
34+
// Update when URL changes due to SPA navigation
35+
window.navigation.addEventListener('navigate', function () {
36+
// run after route has settled
37+
setTimeout(updateLocaleSwitchHref, 0);
38+
});
39+
}
40+
41+
42+
})();

0 commit comments

Comments
 (0)