Docs(locales): add chinese locale support (#2772)
This commit is contained in:
60
docs/.vitepress/locales/en_US.ts
Normal file
60
docs/.vitepress/locales/en_US.ts
Normal file
@ -0,0 +1,60 @@
|
||||
import { createRequire } from 'module'
|
||||
import { defineConfig } from 'vitepress'
|
||||
import { generateSidebarChapter } from './side_bar.js'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
|
||||
const chapters = generateSidebarChapter('en_US', new Map([
|
||||
['introduction', 'Introduction'],
|
||||
['configuration', 'Configuration'],
|
||||
['premium', 'Premium'],
|
||||
['runtime', 'Runtime'],
|
||||
['advanced-usages', 'Advanced Usages'],
|
||||
]))
|
||||
|
||||
export default defineConfig({
|
||||
lang: 'en-US',
|
||||
|
||||
description: 'A rule-based tunnel in Go.',
|
||||
|
||||
themeConfig: {
|
||||
nav: nav(),
|
||||
|
||||
logo: '/logo.png',
|
||||
|
||||
lastUpdatedText: 'Last updated at',
|
||||
|
||||
sidebar: chapters,
|
||||
|
||||
socialLinks: [
|
||||
{ icon: 'github', link: 'https://github.com/Dreamacro/clash' },
|
||||
],
|
||||
|
||||
editLink: {
|
||||
pattern: 'https://github.com/Dreamacro/clash/edit/master/docs/:path',
|
||||
text: 'Edit this page on GitHub'
|
||||
},
|
||||
|
||||
outline: {
|
||||
level: 'deep',
|
||||
label: 'On this page',
|
||||
},
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
function nav() {
|
||||
return [
|
||||
{ text: 'Home', link: '/' },
|
||||
{ text: 'Configuration', link: '/configuration/configuration-reference' },
|
||||
{
|
||||
text: 'Download',
|
||||
items: [
|
||||
{ text: 'Open-source Edition', link: 'https://github.com/Dreamacro/clash/releases/' },
|
||||
{ text: 'Premium Edition', link: 'https://github.com/Dreamacro/clash/releases/tag/premium' },
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
20
docs/.vitepress/locales/index.ts
Normal file
20
docs/.vitepress/locales/index.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { defineConfig } from 'vitepress'
|
||||
import en_US from './en_US'
|
||||
import zh_CN from './zh_CN'
|
||||
|
||||
export default defineConfig({
|
||||
locales: {
|
||||
root: {
|
||||
label: 'English',
|
||||
lang: en_US.lang,
|
||||
themeConfig: en_US.themeConfig,
|
||||
description: en_US.description
|
||||
},
|
||||
zh_CN: {
|
||||
label: '简体中文',
|
||||
lang: zh_CN.lang,
|
||||
themeConfig: zh_CN.themeConfig,
|
||||
description: zh_CN.description
|
||||
}
|
||||
}
|
||||
})
|
78
docs/.vitepress/locales/side_bar.ts
Normal file
78
docs/.vitepress/locales/side_bar.ts
Normal file
@ -0,0 +1,78 @@
|
||||
import directoryTree from 'directory-tree'
|
||||
import fs from 'fs'
|
||||
import metadataParser from 'markdown-yaml-metadata-parser'
|
||||
|
||||
function getMetadataFromDoc(path: string): { sidebarTitle?: string, sidebarOrder?: number } {
|
||||
const fileContents = fs.readFileSync(path, 'utf8')
|
||||
|
||||
return metadataParser(fileContents).metadata
|
||||
}
|
||||
|
||||
export function generateSidebarChapter(locale:string, chapterDirName: Map<string,string>): any[] {
|
||||
if (chapterDirName.size < 1) {
|
||||
console.error(chapterDirName)
|
||||
throw new Error(`Could not genereate sidebar: chapterDirName is empty`)
|
||||
}
|
||||
|
||||
var chapterPath = ''
|
||||
var sidebar: any[] = []
|
||||
|
||||
for (const chapterDirKey of chapterDirName.keys()) {
|
||||
if (locale !== 'en_US') {
|
||||
chapterPath = `./${locale}/${chapterDirKey}`
|
||||
} else {
|
||||
chapterPath = `./${chapterDirKey}`
|
||||
}
|
||||
|
||||
const tree = directoryTree(chapterPath)
|
||||
|
||||
if (!tree || !tree.children) {
|
||||
console.error(tree)
|
||||
throw new Error(`Could not genereate sidebar: invalid chapter at ${chapterPath}`)
|
||||
}
|
||||
|
||||
let items: { sidebarOrder: number, text: string, link: string }[] = []
|
||||
|
||||
// Look into files in the chapter
|
||||
for (const doc of tree.children) {
|
||||
// make sure it's a .md file
|
||||
if (doc.children || !doc.name.endsWith('.md'))
|
||||
continue
|
||||
|
||||
const { sidebarOrder, sidebarTitle } = getMetadataFromDoc(doc.path)
|
||||
|
||||
if (!sidebarOrder)
|
||||
throw new Error('Cannot find sidebarOrder in doc metadata: ' + doc.path)
|
||||
|
||||
if (!sidebarTitle)
|
||||
throw new Error('Cannot find sidebarTitle in doc metadata: ' + doc.path)
|
||||
|
||||
if (chapterDirKey === 'introduction' && doc.name === '_dummy-index.md') {
|
||||
// Override index page link
|
||||
items.push({
|
||||
sidebarOrder,
|
||||
text: sidebarTitle,
|
||||
link: '/' + (locale === 'en_US' ? '' : locale + '/')
|
||||
})
|
||||
} else {
|
||||
items.push({
|
||||
sidebarOrder,
|
||||
text: sidebarTitle,
|
||||
link: "/" + doc.path
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
items = items.sort((a, b) => a.sidebarOrder - b.sidebarOrder)
|
||||
|
||||
// remove dash and capitalize first character of each word as chapter title
|
||||
const text = chapterDirName.get(chapterDirKey) || chapterDirKey.split('-').join(' ').replace(/\b\w/g, l => l.toUpperCase())
|
||||
sidebar.push({
|
||||
text,
|
||||
collapsed: false,
|
||||
items,
|
||||
})
|
||||
}
|
||||
|
||||
return sidebar
|
||||
}
|
60
docs/.vitepress/locales/zh_CN.ts
Normal file
60
docs/.vitepress/locales/zh_CN.ts
Normal file
@ -0,0 +1,60 @@
|
||||
import { createRequire } from 'module'
|
||||
import { defineConfig } from 'vitepress'
|
||||
import { generateSidebarChapter } from './side_bar.js'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
|
||||
const chapters = generateSidebarChapter('zh_CN', new Map([
|
||||
['introduction', '简介'],
|
||||
['configuration', '配置'],
|
||||
['premium', 'Premium 版本'],
|
||||
['runtime', '运行时'],
|
||||
['advanced-usages', '高级用法'],
|
||||
]))
|
||||
|
||||
export default defineConfig({
|
||||
lang: 'zh-CN',
|
||||
|
||||
description: '基于规则的 Go 网络隧道. ',
|
||||
|
||||
themeConfig: {
|
||||
nav: nav(),
|
||||
|
||||
logo: '/logo.png',
|
||||
|
||||
lastUpdatedText: '最后更新于',
|
||||
|
||||
sidebar: chapters,
|
||||
|
||||
socialLinks: [
|
||||
{ icon: 'github', link: 'https://github.com/Dreamacro/clash' },
|
||||
],
|
||||
|
||||
editLink: {
|
||||
pattern: 'https://github.com/Dreamacro/clash/edit/master/docs/:path',
|
||||
text: '在 GitHub 中编辑此页面'
|
||||
},
|
||||
|
||||
docFooter: { prev: '上一篇', next: '下一篇' },
|
||||
|
||||
outline: {
|
||||
level: 'deep',
|
||||
label: '页面导航',
|
||||
},
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
function nav() {
|
||||
return [
|
||||
{ text: '主页', link: '/zh_CN/' },
|
||||
{ text: '配置', link: '/zh_CN/configuration/configuration-reference' },
|
||||
{
|
||||
text: '下载',
|
||||
items: [
|
||||
{ text: 'GitHub 开源版', link: 'https://github.com/Dreamacro/clash/releases/' },
|
||||
{ text: 'Premium 版本', link: 'https://github.com/Dreamacro/clash/releases/tag/premium' },
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user