File size: 1,654 Bytes
43a06dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import i18n from 'sveltekit-i18n';

import type { Config } from 'sveltekit-i18n';
import type {
    GenericImport,
    StructuredLocfileInfo,
    LocalizationContent
} from '$lib/types/i18n';

import _languages from '$i18n/languages.json';

const locFiles = import.meta.glob('$i18n/*/**/*.json');
const parsedLocfiles: StructuredLocfileInfo = {};

for (const [path, loader] of Object.entries(locFiles)) {
    const [, , lang, ...keyComponents] = path.split('/');
    const key = keyComponents.map(k => k.replace('.json', '')).join('.');
    parsedLocfiles[lang] = {
        ...parsedLocfiles[lang],
        [key]: loader as GenericImport
    };
}

const defaultLocale = 'en';
const languages: Record<string, string> = _languages;

const config: Config<{
    value?: string;
    formats?: string;
    limit?: number;
    service?: string;
}> = {
    fallbackLocale: defaultLocale,
    translations: Object.keys(parsedLocfiles).reduce((obj, lang) => {
        languages[lang] ??= `${lang} (missing name)`;

        return {
            ...obj,
            [lang]: { languages }
        }
    }, {}),
    loaders: Object.entries(parsedLocfiles).map(([lang, keys]) => {
        return Object.entries(keys).map(([key, importer]) => {
            return {
                locale: lang,
                key,
                loader: () => importer().then(
                    l => l.default as LocalizationContent
                )
            }
        });
    }).flat()
};

export { defaultLocale };
export const {
    t, loading, locales, locale: INTERNAL_locale, translations,
    loadTranslations, addTranslations, setLocale, setRoute
} = new i18n(config);