From 1520e420700fc631a6b873650197b8c0bd6fd344 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 23 May 2021 15:55:18 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Highlight=20missing=20localizations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nest_frontend/LocalizationStrings.js | 2 +- .../components/providers/GlobalLanguage.js | 27 +++++++++++++++++-- .../providers/GlobalLanguage.module.css | 10 +++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 nest_frontend/components/providers/GlobalLanguage.module.css diff --git a/nest_frontend/LocalizationStrings.js b/nest_frontend/LocalizationStrings.js index 7734d2c..62c06f9 100644 --- a/nest_frontend/LocalizationStrings.js +++ b/nest_frontend/LocalizationStrings.js @@ -9,7 +9,7 @@ * "{number} km radius" * ``` */ -const LOCALIZATION = { +export default { // 🇮🇹 it: { appName: "N.E.S.T.", diff --git a/nest_frontend/components/providers/GlobalLanguage.js b/nest_frontend/components/providers/GlobalLanguage.js index 3414ded..b219dea 100644 --- a/nest_frontend/components/providers/GlobalLanguage.js +++ b/nest_frontend/components/providers/GlobalLanguage.js @@ -1,7 +1,8 @@ -import React from "react" +import React, { useCallback } from "react" import useLocalStorageState from "../../hooks/useLocalStorageState" import ContextLanguage from "../../contexts/ContextLanguage" import LocalizationStrings from "../../LocalizationStrings" +import Style from "./GlobalLanguage.module.css" /** @@ -16,7 +17,29 @@ import LocalizationStrings from "../../LocalizationStrings" export default function GlobalLanguage({ children }) { const [lang, setLang] = useLocalStorageState("language", "it") - const strings = LocalizationStrings[lang] + const getString = useCallback( + (target, name) => { + const languageStrings = LocalizationStrings[lang] + const defaultStrings = LocalizationStrings["it"] + + if(languageStrings.hasOwnProperty(name)) { + return languageStrings[name] + } + else if(defaultStrings.hasOwnProperty(name)) { + console.warn("Missing ", lang, " localization for string ", name) + return {defaultStrings[name]} + + } + else { + console.warn("Missing string ", name) + return MISSING STRING + } + }, + [lang] + ) + + const strings = new Proxy({}, {get: getString}) + return ( diff --git a/nest_frontend/components/providers/GlobalLanguage.module.css b/nest_frontend/components/providers/GlobalLanguage.module.css new file mode 100644 index 0000000..a258ea0 --- /dev/null +++ b/nest_frontend/components/providers/GlobalLanguage.module.css @@ -0,0 +1,10 @@ +.MissingLocalization { + background-color: darkorange; + color: white; +} + +.MissingString { + background-color: red; + color: white; + font-weight: bold; +} \ No newline at end of file