refactor: added i18n class to manage translation logic & improved code

This commit is contained in:
Anurag
2020-10-02 14:07:59 +05:30
parent dc430d8ea7
commit dc067e6f2b
15 changed files with 297 additions and 199 deletions
+21
View File
@@ -0,0 +1,21 @@
class I18n {
constructor({ locale, translations }) {
this.locale = locale;
this.translations = translations;
this.fallbackLocale = "en";
}
t(str) {
if (!this.translations[str]) {
throw new Error(`${str} Translation string not found`);
}
if (!this.translations[str][this.locale || this.fallbackLocale]) {
throw new Error(`${str} Translation locale not found`);
}
return this.translations[str][this.locale || this.fallbackLocale];
}
}
module.exports = I18n;
+7
View File
@@ -188,6 +188,12 @@ class CustomError extends Error {
static USER_NOT_FOUND = "USER_NOT_FOUND";
}
function isLocaleAvailable(locale) {
return ["cn", "de", "en", "es", "fr", "it", "ja", "kr", "pt-br"].includes(
locale.toLowerCase(),
);
}
module.exports = {
renderError,
kFormatter,
@@ -201,6 +207,7 @@ module.exports = {
getCardColors,
clampValue,
wrapTextMultiline,
isLocaleAvailable,
logger,
CONSTANTS,
CustomError,