From 8d883f89ebb635beeb189f9bf97b552e49ffc3d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Glatzl?= Date: Wed, 11 Nov 2020 20:07:36 +0100 Subject: [PATCH] Finished 1st MVP of i18n for outline --- app/index.js | 4 +- shared/translations/de_DE.json | 2 +- shared/translations/default.json | 2 +- shared/translations/i18n.js | 48 +++++++++------- shared/translations/i18n.test.js | 97 ++++++++++++++++++++++++++++++++ shared/translations/pt_PT.json | 20 +++++++ 6 files changed, 150 insertions(+), 23 deletions(-) create mode 100644 shared/translations/i18n.test.js create mode 100644 shared/translations/pt_PT.json diff --git a/app/index.js b/app/index.js index 9519fee336..55d9b8388f 100644 --- a/app/index.js +++ b/app/index.js @@ -14,7 +14,9 @@ import Toasts from "components/Toasts"; import Routes from "./routes"; import env from "env"; -import "shared/translations/i18n"; +import { outlineTranslation } from "shared/translations/i18n"; + +outlineTranslation.init(); const element = document.getElementById("root"); diff --git a/shared/translations/de_DE.json b/shared/translations/de_DE.json index b4ee254e2a..8a3fe16fde 100644 --- a/shared/translations/de_DE.json +++ b/shared/translations/de_DE.json @@ -5,7 +5,7 @@ "Created by me": "Von mir erstellt", "Profile saved": "Profil gespeichert", "Profile picture updated": "Profilfoto wurde aktualisiert", - "Unable to upload new avatar": "Neuer Avatar kann nicht hochgeladen werden", + "Unable to upload new profile picture": "Neuer Avatar kann nicht hochgeladen werden", "Profile": "Profil", "Photo": "Foto", "Upload": "Hochladen", diff --git a/shared/translations/default.json b/shared/translations/default.json index a1d8afec60..c5f0343cdd 100644 --- a/shared/translations/default.json +++ b/shared/translations/default.json @@ -5,7 +5,7 @@ "Created by me": "Created by me", "Profile saved": "Profile saved", "Profile picture updated": "Profile picture updated", - "Unable to upload new avatar": "Unable to upload new avatar", + "Unable to upload new profile picture": "Unable to upload new profile picture", "Profile": "Profile", "Photo": "Photo", "Upload": "Upload", diff --git a/shared/translations/i18n.js b/shared/translations/i18n.js index c3f18e5590..4c49924418 100644 --- a/shared/translations/i18n.js +++ b/shared/translations/i18n.js @@ -2,26 +2,34 @@ import i18n from "i18next"; import { initReactI18next } from "react-i18next"; -import de_DE from "./de_DE.json"; import en_US from "./default.json"; +import de_DE from "./de_DE.json"; +import pt_PT from "./pt_PT.json"; -i18n.use(initReactI18next).init({ - interpolation: { - escapeValue: false, - }, - react: { - useSuspense: false, - }, - lng: process.env.DEFAULT_LANGUAGE, - debug: process.env.NODE_ENV !== "production", - resources: { - en_US: { - translation: en_US, - }, - de_DE: { - translation: de_DE, - }, - }, -}); +const outlineTranslation = { + init: () => { + i18n.use(initReactI18next).init({ + interpolation: { + escapeValue: false, + }, + react: { + useSuspense: false, + }, + lng: "DEFAULT_LANGUAGE" in process.env ? process.env.DEFAULT_LANGUAGE : "en_US", + debug: process.env.NODE_ENV !== "production", + resources: { + en_US: { + translation: en_US, + }, + de_DE: { + translation: de_DE, + }, + pt_PT: { + translation: pt_PT + } + }, + }); + } +} -export default i18n; +export { outlineTranslation, i18n, en_US, de_DE, pt_PT }; diff --git a/shared/translations/i18n.test.js b/shared/translations/i18n.test.js new file mode 100644 index 0000000000..079e586214 --- /dev/null +++ b/shared/translations/i18n.test.js @@ -0,0 +1,97 @@ +/* eslint-disable flowtype/require-valid-file-annotation */ +import { outlineTranslation, i18n, en_US, de_DE, pt_PT } from "./i18n"; +import { isEqual } from "lodash"; + +describe("i18n configuration", () => { + beforeEach(() => { + outlineTranslation.init(); + }); + it("all languages should have same keys", () => { + const en_US_Keys = Object.keys(en_US); + const de_DE_Keys = Object.keys(de_DE); + const pt_PT_Keys = Object.keys(pt_PT); + + expect(isEqual(en_US_Keys, de_DE_Keys)).toBe(true); + expect(isEqual(en_US_Keys, pt_PT_Keys)).toBe(true); + }); +}); + +describe("i18n process.env is unset", () => { + beforeEach(() => { + delete process.env.DEFAULT_LANGUAGE; + outlineTranslation.init(); + }); + + it("translation of key should match", () => expect(i18n.t("Saving…")).toBe("Saving…")); + + it ("translation if changed to de_DE", () => { + i18n.changeLanguage("de_DE"); + expect(i18n.t("Saving…")).toBe("Am speichern…"); + }); + + it ("translation if changed to pt_PT", () => { + i18n.changeLanguage("pt_PT"); + expect(i18n.t("Saving…")).toBe("A guardar…"); + }); + +}); + +describe("i18n process.env is en_US", () => { + beforeEach(() => { + process.env.DEFAULT_LANGUAGE = "en_US"; + outlineTranslation.init(); + }); + + it("translation of key should match", () => expect(i18n.t("Saving…")).toBe("Saving…")); + + it ("translation if changed to de_DE", () => { + i18n.changeLanguage("de_DE"); + expect(i18n.t("Saving…")).toBe("Am speichern…"); + }); + + it ("translation if changed to pt_PT", () => { + i18n.changeLanguage("pt_PT"); + expect(i18n.t("Saving…")).toBe("A guardar…"); + }); + +}); + +describe("i18n process.env is de_DE", () => { + beforeEach(() => { + process.env.DEFAULT_LANGUAGE = "de_DE"; + outlineTranslation.init(); + }); + + it("translation of key should match", () => expect(i18n.t("Saving…")).toBe("Am speichern…")); + + it ("translation if changed to en_US", () => { + i18n.changeLanguage("en_US"); + expect(i18n.t("Saving…")).toBe("Saving…"); + }); + + it ("translation if changed to pt_PT", () => { + i18n.changeLanguage("pt_PT"); + expect(i18n.t("Saving…")).toBe("A guardar…"); + }); + +}); + +describe("i18n process.env is pt_PT", () => { + beforeEach(() => { + process.env.DEFAULT_LANGUAGE = "pt_PT"; + outlineTranslation.init(); + }); + + it("translation of key should match", () => expect(i18n.t("Saving…")).toBe("A guardar…")); + + it ("translation if changed to en_US", () => { + i18n.changeLanguage("en_US"); + expect(i18n.t("Saving…")).toBe("Saving…"); + }); + + it ("translation if changed to de_DE", () => { + i18n.changeLanguage("de_DE"); + expect(i18n.t("Saving…")).toBe("Am speichern…"); + }); + +}); \ No newline at end of file diff --git a/shared/translations/pt_PT.json b/shared/translations/pt_PT.json new file mode 100644 index 0000000000..d02f47d337 --- /dev/null +++ b/shared/translations/pt_PT.json @@ -0,0 +1,20 @@ +{ + "Home": "Home", + "Recently updated": "Recently updated", + "Recently viewed": "Recently viewed", + "Created by me": "Created by me", + "Profile saved": "Profile saved", + "Profile picture updated": "Profile picture updated", + "Unable to upload new profile picture": "Unable to upload new avatar", + "Profile": "Profile", + "Photo": "Photo", + "Upload": "Upload", + "Full name": "Full name", + "Language": "Language", + "Saving…": "A guardar…", + "Save": "Save", + "Delete Account": "Delete Account", + "You may delete your account at any time, note that this is unrecoverable": "You may delete your account at any time, note that this is unrecoverable", + "Delete account": "Delete account" + } + \ No newline at end of file