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
|
import { translate } from 'vue-gettext';
import defaultTranslations from '../../../locales/de_DE.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);
export { $gettext, 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) {
state.translations[state.locale] = await getTranslations(state.locale);
}
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 translation = await import(`../../../locales/${locale}.json`);
return translation;
} catch (exception) {
console.error('Could not load locale: "' + locale + '"', exception);
return {};
}
}
|