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
125
126
127
128
129
130
131
132
133
|
import {createGettext, LanguageData} from 'vue3-gettext';
import * as defaultTranslations from '../../../../locale/de/LC_MESSAGES/js-resources.json';
import eventBus from './event-bus';
interface StringDict {
[key: string]: string;
}
interface InstalledLanguage {
name: string;
selected: boolean;
}
interface InstalledLanguages {
[key: string]: InstalledLanguage;
}
type Translation = LanguageData;
type Translations = {
[language: string]: LanguageData;
};
const DEFAULT_LANG = 'de_DE';
const DEFAULT_LANG_NAME = 'Deutsch';
const state = getInitialState();
const gettext = createGettext({
availableLanguages: getAvailableLanguages(),
defaultLanguage: state.locale,
silent: false,
translations: {
[DEFAULT_LANG]: {}
},
mutedLanguages: [DEFAULT_LANG],
setGlobalProperties: true,
globalProperties: {
language: ['$language'],
gettext: ['$gettext'],
pgettext: ['$pgettext'],
ngettext: ['$ngettext'],
npgettext: ['$npgettext'],
interpolate: ['$gettextInterpolate'],
},
provideDirective: true,
provideComponent: true,
});
setLocale(state.locale);
export default gettext;
async function updateTranslations() {
const translations: Translations = {};
for (const key of Object.keys(getAvailableLanguages())) {
if (state.locale === key) {
translations[key] = await getTranslations(key);
}
}
gettext.translations = translations;
}
export function getLocale() {
return state.locale;
}
export 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);
}
updateTranslations();
eventBus.emit('studip:set-locale', state.locale);
}
function getAvailableLanguages() {
return Object.entries(getInstalledLanguages()).reduce((memo, [lang, { name }]) => {
memo[lang] = name;
return memo;
}, {} as StringDict);
}
function getInitialState() {
const translations: Translations = Object.entries(getInstalledLanguages()).reduce((memo, [lang]) => {
memo[lang] = lang === DEFAULT_LANG ? defaultTranslations : '';
return memo;
}, {} as Translations);
return {
locale: getInitialLocale(),
translations,
};
}
function getInitialLocale() {
for (const [lang, { selected }] of Object.entries(getInstalledLanguages())) {
if (selected) {
return lang;
}
}
return DEFAULT_LANG;
}
function getInstalledLanguages(): InstalledLanguages {
return window?.STUDIP?.INSTALLED_LANGUAGES ?? { [DEFAULT_LANG]: { name: DEFAULT_LANG_NAME, selected: true } };
}
async function getTranslations(locale: string): Promise<Translation> {
try {
const language = locale.split(/[_-]/)[0];
return await import(`../../../../locale/${language}/LC_MESSAGES/js-resources.json`);
} catch (exception) {
console.error('Could not load locale: "' + locale + '"', exception);
return {};
}
}
export const $gettext = gettext.$gettext;
export const $ngettext = gettext.$ngettext;
export const $gettextInterpolate = gettext.interpolate;
|