-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
124 lines (118 loc) · 3.73 KB
/
index.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Reexport your entry components here
import { page } from '$app/stores';
import lodash from 'lodash';
export { default } from './I18n.svelte';
export { default as LangRouter } from './Wrapper.svelte';
export { default as rollupI18N } from './rollup';
import config from 'virtual:i18n-config';
import type { ParamMatcher } from '@sveltejs/kit';
import { derived } from 'svelte/store';
const { get } = lodash;
export async function init({
lang,
pathname,
pathDel = '_',
layout = true,
page
}: {
lang: string | undefined;
pathname: string;
pathDel?: string;
layout?: boolean;
page?: boolean;
}) {
function stripEnd(s: string) {
return s.split('/').slice(0, -1).join('/');
}
function stripBegin(s: string, del: string) {
return s.split(del).slice(1).join(del);
}
const strippedLangPath = pathname.replace(`/${lang}`, '') + '/page';
const strippedPath = stripEnd(strippedLangPath);
const l = lang || config.defaultLang;
const pageKey = (l + strippedLangPath).replace('//', '/').replaceAll('/', pathDel);
const contents: Record<string, unknown> = {};
try {
const allContents: Record<string, unknown> = await import('virtual:merge/contents/**/*.mdx');
const pageContents = allContents[pageKey];
if (layout) {
Object.entries(allContents)
.filter(
(e) =>
e[0].startsWith(l + pathDel) &&
e[0].endsWith(pathDel + 'layout') &&
strippedPath.includes(stripEnd(e[0]))
)
.forEach((e) => {
contents[stripBegin(e[0], pathDel)] = e[1];
});
}
if (page || page === undefined) {
contents.page = pageContents;
}
} catch (error) {
console.error("Couldn't import content .mdx files, error:");
console.error(error);
}
return contents;
}
export const i18n = derived(page, ($page) => {
const lang = $page.params.lang || config.defaultLang;
const route = $page.route.id || '';
return {
get: function (id: string, pathDel = '_') {
const layout = id.includes('_layout.') || id.startsWith('layout.');
const result = get($page.data.contents, layout ? id : 'page.' + id);
if (!result) {
if (layout) {
const filePath = `.../contents/${lang}/${id.split('.')[0].replace(pathDel, '/')}`;
const key = id.split('.').slice(1).join('.');
console.error(
`couldn't get content for '${id}'.\n\nDoes the path '${filePath}' point to an existing file?\n\nDoes the key '${key}' exist in the file '${filePath}'?`
);
} else {
const filePath = `.../contents${route.replace('[[lang=lang]]', lang)}/page`;
const key = id;
console.error(
`couldn't get content for '${id}'.\n\nDoes the path '${filePath}' point to an existing file?\n\nDoes the key '${key}' exist in the file '${filePath}'?`
);
}
}
return result;
},
lang: lang,
redirect: async function () {
if (window) {
const { goto } = await import('$app/navigation');
const langPref = getLangPref();
const nextBestLang =
config.langs.find((l) => langPref.includes(l) || l.includes(langPref)) ??
config.defaultLang;
const newPathname = window.location.pathname.replace(
/^\/(.)*?((?=\/)|$)/,
`/${nextBestLang}`
);
const newUrl = newPathname + window.location.search + window.location.hash;
if (lang !== nextBestLang) {
goto(newUrl);
}
}
}
};
});
export function getLangPref() {
if (window) {
const lS = window.localStorage.getItem('i18n-lang-preference');
const b = window.navigator.language;
return lS ?? b;
} else throw new Error('window is not defined.');
}
export function setLangPref(lang: string) {
if (window) {
window.localStorage.setItem('i18n-lang-preference', lang);
} else throw new Error('window is not defined.');
}
export const match = ((param) => {
const locales = config.langs;
return locales.includes(param);
}) satisfies ParamMatcher;