From f6209b119d9bc7d0bc71f206ffa3d6db63c9c6ea Mon Sep 17 00:00:00 2001 From: Kitteh Date: Mon, 3 May 2021 15:13:30 +0100 Subject: [PATCH] Rework how to import and adding new translations. --- checkTranslations.mjs | 20 ++++---------------- src/main.js | 17 +++-------------- src/pages/SetLanguage.js | 5 +++-- src/translations/index.mjs | 15 +++++++++++++++ 4 files changed, 25 insertions(+), 32 deletions(-) create mode 100644 src/translations/index.mjs diff --git a/checkTranslations.mjs b/checkTranslations.mjs index 16f6fe3..ece84c7 100644 --- a/checkTranslations.mjs +++ b/checkTranslations.mjs @@ -1,20 +1,8 @@ -import translation_en from './src/translations/en.js' -import translation_de from './src/translations/de.js' -import translation_ru from './src/translations/ru.js' -import translation_nl from './src/translations/nl.js' -import translation_fr from './src/translations/fr.js' +import translations from './src/translations/index.mjs' -const langs = ["en", "de", "ru", "nl", "fr"]; +const langs = Object.getOwnPropertyNames(translations); -const translations_dict = { - en: translation_en, - de: translation_de, - ru: translation_ru, - nl: translation_nl, - fr: translation_fr, -} - -let en_keys = [...new Map(Object.entries(translations_dict.en)).keys()]; +let en_keys = [...new Map(Object.entries(translations.en)).keys()]; function arrayDiff(a, b) { return { @@ -24,7 +12,7 @@ function arrayDiff(a, b) { } for (let lang_num in langs) { - let lang = new Map(Object.entries(translations_dict[langs[lang_num]])); + let lang = new Map(Object.entries(translations[langs[lang_num]])); if (lang == "en") continue; let lang_keys = [...lang.keys()]; let di = arrayDiff(en_keys, [...lang.keys()]) diff --git a/src/main.js b/src/main.js index 57e0ac8..a0df0d6 100644 --- a/src/main.js +++ b/src/main.js @@ -68,14 +68,9 @@ const pages = { }; // Translations -import {formatDistance} from './formatDistance.js'; +import { formatDistance } from './formatDistance.js'; import i18next from 'i18next'; -import translation_de from './translations/de.js' -import translation_en from './translations/en.js' -import translation_fr from './translations/fr.js' -import translation_nl from './translations/nl.js' -import translation_ru from './translations/ru.js' - +import translations from './translations/index.mjs' /*import "en-GB" as date_locale_en from 'date-fns/locale' @@ -188,13 +183,7 @@ document.addEventListener('DOMContentLoaded', function () { lng: pageState.language, fallbackLng: 'en', debug: true, - resources: { - en: { translation: translation_en }, - de: { translation: translation_de }, - ru: { translation: translation_ru }, - nl: { translation: translation_nl }, - fr: { translation: translation_fr }, - }, + resources: Object.fromEntries(Object.entries(translations).map(([k, v]) => [k, { translation: v }])), interpolation: { format: function (value, format, _) { if (format === 'until_date' && value instanceof Date) return formatDistance(new Date(), new Date(value)); diff --git a/src/pages/SetLanguage.js b/src/pages/SetLanguage.js index 717e656..4bce5c3 100644 --- a/src/pages/SetLanguage.js +++ b/src/pages/SetLanguage.js @@ -3,8 +3,9 @@ import { Page } from "../types/Page.js"; import { changePage, setPageContent } from "../pageUtils.js"; import { makeElement } from "../htmlUtils.js"; import i18next from 'i18next'; +import translations from "../translations/index.mjs"; -let languages = ["en", "de", "nl", "ru", "fr"]; +let languageIDs = Object.getOwnPropertyNames(translations); export class SetLanguagePage extends Page { constructor() { @@ -21,7 +22,7 @@ export class SetLanguagePage extends Page { attributes: { name: "language" }, - children: languages.map(function (languageID) { + children: languageIDs.map(function (languageID) { return makeElement({ tag: "option", text: i18next.getFixedT(languageID, null)("language_name"), diff --git a/src/translations/index.mjs b/src/translations/index.mjs new file mode 100644 index 0000000..1677c52 --- /dev/null +++ b/src/translations/index.mjs @@ -0,0 +1,15 @@ +import de from './de.js' +import en from './en.js' +import fr from './fr.js' +import nl from './nl.js' +import ru from './ru.js' + +const translations = { + de: de, + en: en, + fr: fr, + nl: nl, + ru: ru, +} + +export default translations; \ No newline at end of file