-
Notifications
You must be signed in to change notification settings - Fork 1
/
locale.config.js
58 lines (49 loc) · 1.65 KB
/
locale.config.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
const createI18nConfig = () => {
const config = /** @type {const} */ ({
locales: ['se', 'en'],
localeLabel: {
se: 'Svenska',
en: 'English',
},
stripBase: true,
base: 'se',
})
if (!config.locales.includes(config.base)) throw new Error(`Invalid base locale`)
if (!config.locales.every((l) => !!config.localeLabel[l])) throw new Error(`Invalid localeLabel`)
/** @type {{ [K in typeof config.locales[number]]: K }} */
const LOCALE = config.locales.reduce((p, c) => {
p[c] = c
return p
}, {})
/** @type {{ id: typeof config.locales[number], title: typeof config.localeLabel[typeof config.locales[number]] }[]} */
const languages = Object.entries(config.localeLabel).map(([k, v]) => ({id: k, title: v}))
return {
...config,
languages,
LOCALE,
}
}
const i18nConfig = createI18nConfig()
const localizeRoutes = (routeNamingLocale, routesManifest, translations) => {
const otherLocales = i18nConfig.locales.filter((l) => l !== routeNamingLocale)
const localizedRoutes = Object.fromEntries(
Object.values(routesManifest).flatMap((v) => {
if (!v.path) return [[v.id, v]]
return translations.flatMap((t) => {
if (v.file.startsWith(t.matchAll.replace('{{slug}}', t.slugs[routeNamingLocale]))) {
return otherLocales.map((l) => [
v.id,
{
...v,
path: v.path.replace(t.slugs[routeNamingLocale], t.slugs[l]),
id: v.id.replace(t.slugs[routeNamingLocale], t.slugs[l]),
},
])
}
return [[v.id, v]]
})
})
)
return localizedRoutes
}
module.exports = {i18nConfig, localizeRoutes}