-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathlangs.ts
55 lines (51 loc) · 1.5 KB
/
langs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { computed } from 'vue'
import { ensureStartingSlash } from '../support/utils'
import { useData } from './data'
export function useLangs({
removeCurrent = true,
correspondingLink = false
} = {}) {
const { site, localeIndex, page, theme, hash } = useData()
const currentLang = computed(() => ({
index: localeIndex.value,
label: site.value.locales[localeIndex.value]?.label,
link:
site.value.locales[localeIndex.value]?.link ||
(localeIndex.value === 'root' ? '/' : `/${localeIndex.value}/`)
}))
const localeLinks = computed(() =>
Object.entries(site.value.locales).flatMap(([key, value]) =>
removeCurrent && currentLang.value.label === value.label
? []
: {
index: key,
text: value.label,
link:
normalizeLink(
value.link || (key === 'root' ? '/' : `/${key}/`),
theme.value.i18nRouting !== false && correspondingLink,
page.value.relativePath.slice(
currentLang.value.link.length - 1
),
!site.value.cleanUrls
) + hash.value
}
)
)
return { localeLinks, currentLang }
}
function normalizeLink(
link: string,
addPath: boolean,
path: string,
addExt: boolean
) {
return addPath
? link.replace(/\/$/, '') +
ensureStartingSlash(
path
.replace(/(^|\/)index\.md$/, '$1')
.replace(/\.md$/, addExt ? '.html' : '')
)
: link
}