From c2d01e9489f6ac41443cc2244e86e831a682f29a Mon Sep 17 00:00:00 2001 From: Felix Hamann Date: Sat, 6 Apr 2019 14:45:01 +0200 Subject: [PATCH] add i18n registry for JS utils --- src/Foundation.hs | 1 + static/js/services/i18n.js | 31 +++++++++++++++++++++++++++++++ templates/default-layout.julius | 17 ++++++++++------- 3 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 static/js/services/i18n.js diff --git a/src/Foundation.hs b/src/Foundation.hs index fe0df0b26..cc5e85bcf 100644 --- a/src/Foundation.hs +++ b/src/Foundation.hs @@ -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 diff --git a/static/js/services/i18n.js b/static/js/services/i18n.js new file mode 100644 index 000000000..440b1aca5 --- /dev/null +++ b/static/js/services/i18n.js @@ -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, + }; + })(); +})(); diff --git a/templates/default-layout.julius b/templates/default-layout.julius index d83daccd0..78e911914 100644 --- a/templates/default-layout.julius +++ b/templates/default-layout.julius @@ -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.
Falls das erneut passiert schicke uns gerne eine kurze Beschreibung dieses Ereignisses über das Hilfe-Widget rechts oben.

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.
Falls das erneut passiert schicke uns gerne eine kurze Beschreibung dieses Ereignisses über das Hilfe-Widget rechts oben.

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 });