From 6a2ffe6aaf7599ed5b15e820829c49856ee4e14a Mon Sep 17 00:00:00 2001 From: SamTV12345 <40429738+SamTV12345@users.noreply.github.com> Date: Sat, 13 Jan 2024 16:23:21 +0100 Subject: [PATCH] Added error handling if an unknown language code is entered. (#6102) --- src/node/hooks/i18n.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/node/hooks/i18n.js b/src/node/hooks/i18n.js index c54348867..fc9f00c72 100644 --- a/src/node/hooks/i18n.js +++ b/src/node/hooks/i18n.js @@ -74,6 +74,24 @@ const getAllLocales = () => { if (typeof overrides !== 'object') throw wrongFormatErr; _.each(overrides, (localeString, key) => { if (typeof localeString !== 'string') throw wrongFormatErr; + const locale = locales[langcode]; + + // Handles the error if an unknown language code is entered + if (locale === undefined) { + const possibleMatches = []; + let strippedLangcode = ''; + if (langcode.includes('-')) { + strippedLangcode = langcode.split('-')[0]; + } + for (const localeInEtherPad of Object.keys(locales)) { + if (localeInEtherPad.includes(strippedLangcode)) { + possibleMatches.push(localeInEtherPad); + } + } + throw new Error(`Language code ${langcode} is unknown. ` + + `Maybe you meant: ${possibleMatches}`); + } + locales[langcode][key] = localeString; }); });