add i18n registry for JS utils

This commit is contained in:
Felix Hamann 2019-04-06 14:45:01 +02:00
parent ffef0b94bc
commit c2d01e9489
3 changed files with 42 additions and 7 deletions

View File

@ -1067,6 +1067,7 @@ siteLayout' headingOverride widget = do
-- JavaScript services
addScript $ StaticR js_services_utilRegistry_js
addScript $ StaticR js_services_httpClient_js
addScript $ StaticR js_services_i18n_js
-- addScript $ StaticR js_utils_alerts_js
-- addScript $ StaticR js_utils_asidenav_js
-- addScript $ StaticR js_utils_asyncForm_js

View File

@ -0,0 +1,31 @@
(function() {
'use strict';
// Global I18n registry that stores and serves translations.
// Each translation must have a unique ID.
window.I18n = (function() {
var translations = {};
function addTranslation(id, translation) {
translations[id] = translation;
}
function addManyTranslations(manyTranslations) {
Object.keys(manyTranslations).forEach(function(key) {
addTranslation(key, manyTranslations[key]);
});
}
function getTranslation(id) {
return translations[id];
}
// public API
return {
add: addTranslation,
addMany: addManyTranslations,
get: getTranslation,
};
})();
})();

View File

@ -35,13 +35,16 @@ function setupDatepicker(wrapper) {
});
}
// this global I18N object will be picked up automatically by the setup util
window.I18N = {
filesSelected: 'Dateien ausgewählt', // TODO: interpolate these to be translated
selectFile: 'Datei auswählen',
selectFiles: 'Datei(en) auswählen',
asyncFormFailure: 'Da ist etwas schief gelaufen, das tut uns Leid.<br>Falls das erneut passiert schicke uns gerne eine kurze Beschreibung dieses Ereignisses über das Hilfe-Widget rechts oben.<br><br>Vielen Dank für deine Hilfe',
};
if (I18n) {
I18n.addMany({
filesSelected: 'Dateien ausgewählt',
selectFile: 'Datei auswählen',
selectFiles: 'Datei(en) auswählen',
asyncFormFailure: 'Da ist etwas schief gelaufen, das tut uns Leid.<br>Falls das erneut passiert schicke uns gerne eine kurze Beschreibung dieses Ereignisses über das Hilfe-Widget rechts oben.<br><br>Vielen Dank für deine Hilfe',
});
} else {
throw new Error('I18n JavaScript service is missing!');
}
document.addEventListener('DOMContentLoaded', function() {
window.utils.setup('flatpickr', document.body, { setupFunction: setupDatepicker });