aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/lib/gettext.js
blob: 0e3eb9aab4f2cf3a7720f323035a734df6c8762f (plain)
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
import { translate } from 'vue-gettext';
import defaultTranslations from '../../../../locale/de/LC_MESSAGES/js-resources.json';
import eventBus from './event-bus.js';

const DEFAULT_LANG = 'de_DE';
const DEFAULT_LANG_NAME = 'Deutsch';

const state = getInitialState();

const $gettext = translate.gettext.bind(translate);
const $ngettext = translate.ngettext.bind(translate);
const $gettextInterpolate = translate.gettextInterpolate.bind(translate);

export { $gettext, $ngettext, $gettextInterpolate, translate, getLocale, setLocale, getVueConfig };

function getLocale() {
    return state.locale;
}

async function setLocale(locale = getInitialLocale()) {
    if (!(locale in getInstalledLanguages())) {
        throw new Error('Invalid locale: ' + locale);
    }

    state.locale = locale;
    if (state.translations[state.locale] === null) {
        const translations = await getTranslations(state.locale);
        state.translations[state.locale] = translations;
    }

    translate.initTranslations(state.translations, {
        getTextPluginMuteLanguages: [DEFAULT_LANG],
        getTextPluginSilent: false,
        language: state.locale,
        silent: false,
    });

    eventBus.emit('studip:set-locale', state.locale);
}

function getVueConfig() {
    const availableLanguages = Object.entries(getInstalledLanguages()).reduce((memo, [lang, { name }]) => {
        memo[lang] = name;

        return memo;
    }, {});

    return {
        availableLanguages,
        defaultLanguage: DEFAULT_LANG,
        muteLanguages: [DEFAULT_LANG],
        silent: false,
        translations: state.translations,
    };
}

function getInitialState() {
    const translations = Object.entries(getInstalledLanguages()).reduce((memo, [lang]) => {
        memo[lang] = lang === DEFAULT_LANG ? defaultTranslations : null;

        return memo;
    }, {});

    return {
        locale: DEFAULT_LANG,
        translations,
    };
}

function getInitialLocale() {
    for (const [lang, { selected }] of Object.entries(getInstalledLanguages())) {
        if (selected) {
            return lang;
        }
    }

    return DEFAULT_LANG;
}

function getInstalledLanguages() {
    return window?.STUDIP?.INSTALLED_LANGUAGES ?? { [DEFAULT_LANG]: { name: DEFAULT_LANG_NAME, selected: true } };
}

async function getTranslations(locale) {
    try {
        const language = locale.split(/[_-]/)[0];
        const translation = await import(`../../../../locale/${language}/LC_MESSAGES/js-resources.json`);

        return translation;
    } catch (exception) {
        console.error('Could not load locale: "' + locale + '"', exception);

        return {};
    }
}