forked from Sonolus/sonolus-home
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.js
66 lines (55 loc) · 2.33 KB
/
render.js
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
56
57
58
59
60
61
62
63
64
65
66
import { readFileSync, readdirSync, writeFileSync } from 'node:fs'
const i18n = readdirSync('./i18n').map((locale) => ({
locale,
localization: JSON.parse(readFileSync(`./i18n/${locale}/index.json`, 'utf8')),
}))
const fallbackLocalization = i18n.find(({ locale }) => locale === 'en').localization
const html = readFileSync('./dist/index.html', 'utf8')
const altLinks = [
...i18n.map(
({ locale, localization }) =>
`<link rel="alternate" hreflang="${localization['meta.lang']}" href="https://sonolus.com/${locale}" />`,
),
`<link rel="alternate" hreflang="x-default" href="https://sonolus.com/" />`,
]
const translationMatches = [...html.matchAll(/<inject-translation>(.*)<\/inject-translation>/g)]
const localizationMatch = /<inject-localization>(.*)<\/inject-localization>/gs.exec(html)
for (const { locale, localization } of i18n) {
const t = (key) => localization[key] ?? fallbackLocalization[key]
const wikiLocale = locale === 'en' ? '' : `/${locale}`
let out = html
.replace('<html>', `<html lang="${localization['meta.lang']}">`)
.replace(
'</head>',
[
...altLinks,
`<title>${t('meta.title')}</title>`,
`<meta name="description" content="${t('app.description')}" />`,
'</head>',
].join(''),
)
.replace(
'href="dl-ios"',
`href="https://wiki.sonolus.com/getting-started${wikiLocale}/installing/ios.html"`,
)
.replace(
'href="dl-android"',
`href="https://wiki.sonolus.com/getting-started${wikiLocale}/installing/android.html"`,
)
.replace('href="wiki"', `href="https://wiki.sonolus.com${wikiLocale}"`)
.replace(
localizationMatch[0],
i18n
.map(({ locale, localization }) =>
localizationMatch[1]
.replace('href="localization"', `href="/${locale}"`)
.replace('<inject-name></inject-name>', localization['meta.name']),
)
.join(''),
)
for (const [search, key] of translationMatches) {
out = out.replace(search, t(key))
}
writeFileSync(`./dist/${locale}.html`, out)
if (locale === 'en') writeFileSync('./dist/index.html', out)
}