From 281156eacc98b1b5a14b477c46a6434de5ff3907 Mon Sep 17 00:00:00 2001 From: Kitteh Date: Sun, 18 Apr 2021 11:28:55 +0100 Subject: [PATCH] Start work on i18n. --- package.json | 1 + src/main.js | 109 +++++++++++++++++++++++++---------------- src/translations/de.js | 5 ++ src/translations/en.js | 5 ++ webpack.config.js | 9 ++-- 5 files changed, 83 insertions(+), 46 deletions(-) create mode 100644 src/translations/de.js create mode 100644 src/translations/en.js diff --git a/package.json b/package.json index 6afddfd..db4ae18 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "eslint-plugin-import": "^2.22.1", "file-saver": "^2.0.5", "html-webpack-plugin": "^5.3.1", + "i18next": "^20.2.1", "mini-css-extract-plugin": "^1.4.1", "node-sass": "^5.0.0", "prismjs": "^1.23.0", diff --git a/src/main.js b/src/main.js index f4881ce..04e62af 100644 --- a/src/main.js +++ b/src/main.js @@ -1,5 +1,7 @@ 'use strict'; +// JS & CSS + import "./scss/main.scss"; import UIkit from 'uikit/dist/js/uikit.min.js'; import Icons from 'uikit/dist/js/uikit-icons.min.js'; @@ -17,6 +19,8 @@ import { PageState } from "./PageState.js"; import { makeElement } from "./htmlUtils.js"; import { getSealStatus } from './api.js'; +// Pages + import { HomePage, MePage, @@ -59,6 +63,15 @@ const pages = { PW_GEN: new PwGenPage(), }; +// Translations +import i18next from 'i18next'; +import translation_en from './translations/en.js' +import translation_de from './translations/de.js' + + + +// Globals + var pageState = new PageState(); window.pageState = pageState; window.realPages = pages; @@ -70,53 +83,53 @@ function ListItem(children) { }); } -document.addEventListener('DOMContentLoaded', function () { +function onLoad() { document.body.innerHTML = ""; document.body.appendChild(makeElement({ tag: "nav", class: ["uk-navbar", "uk-navbar-container"], - children: [ + children: [ makeElement({ - tag: "div", - class: "uk-navbar-left", - children: makeElement({ - tag: "ul", - class: "uk-navbar-nav", - children: [ - ListItem(makeElement({ - tag: "a", - text: "Home", - onclick: _ => { changePage("HOME"); } - })), - ListItem(makeElement({ - tag: "a", - text: "Back", - onclick: _ => { pageState.currentPage.goBack(); } - })), - ListItem(makeElement({ - tag: "a", - text: "Refresh", - onclick: _ => { changePage(pageState.currentPage); } - })), - ] + tag: "div", + class: "uk-navbar-left", + children: makeElement({ + tag: "ul", + class: "uk-navbar-nav", + children: [ + ListItem(makeElement({ + tag: "a", + text: i18next.t("home_btn"), + onclick: _ => { changePage("HOME"); } + })), + ListItem(makeElement({ + tag: "a", + text: i18next.t("back_btn"), + onclick: _ => { pageState.currentPage.goBack(); } + })), + ListItem(makeElement({ + tag: "a", + text: i18next.t("refresh_btn"), + onclick: _ => { changePage(pageState.currentPage); } + })), + ] + }) + }), + makeElement({ + tag: "div", + class: "uk-navbar-right", + children: makeElement({ + tag: "ul", + class: "uk-navbar-nav", + children: [ + ListItem(makeElement({ + tag: "a", + text: "Me", + onclick: _ => { changePage("ME"); } + })) + ] + }) }) - }), - makeElement({ - tag: "div", - class: "uk-navbar-right", - children: makeElement({ - tag: "ul", - class: "uk-navbar-nav", - children: [ - ListItem(makeElement({ - tag: "a", - text: "Me", - onclick: _ => { changePage("ME"); } - })) - ] - }) - }) - ] + ] })); document.body.appendChild(makeElement({ tag: "div", @@ -152,4 +165,18 @@ document.addEventListener('DOMContentLoaded', function () { } } }, 5000); +}; + +document.addEventListener('DOMContentLoaded', function () { + i18next.init({ + lng: 'de', + debug: true, + resources: { + en: { translation: translation_en }, + de: { translation: translation_de }, + + } + }).then(function (t) { + onLoad(); + }); }, false); \ No newline at end of file diff --git a/src/translations/de.js b/src/translations/de.js new file mode 100644 index 0000000..4171aec --- /dev/null +++ b/src/translations/de.js @@ -0,0 +1,5 @@ +module.exports = { + "home_btn": "Startseite", + "back_btn": "Zurück", + "refresh_btn": "Neu laden" +} \ No newline at end of file diff --git a/src/translations/en.js b/src/translations/en.js new file mode 100644 index 0000000..c8d6212 --- /dev/null +++ b/src/translations/en.js @@ -0,0 +1,5 @@ +module.exports = { + "home_btn": "Home", + "back_btn": "Back", + "refresh_btn": "Refresh" +} \ No newline at end of file diff --git a/webpack.config.js b/webpack.config.js index d7006cc..ca3aa59 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -21,12 +21,11 @@ module.exports = { { test: /\.(sa|sc|c)ss$/, use: [ - MiniCssExtractPlugin.loader, - "css-loader", - "sass-loader" + MiniCssExtractPlugin.loader, + "css-loader", + "sass-loader" ], - }, - + }, ], }, }; \ No newline at end of file