Skip to content

Commit

Permalink
i18n: set locale only once instead of on each call (anuraghazra#3200)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerty541 committed Dec 22, 2023
1 parent 2fa8d0c commit 2f243f1
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/common/I18n.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const FALLBACK_LOCALE = "en";

/**
* I18n translation class.
*/
Expand All @@ -10,9 +12,8 @@ class I18n {
* @param {Object} options.translations Translations.
*/
constructor({ locale, translations }) {
this.locale = locale;
this.locale = locale || FALLBACK_LOCALE;
this.translations = translations;
this.fallbackLocale = "en";
}

/**
Expand All @@ -22,17 +23,17 @@ class I18n {
* @returns {string} Translated string.
*/
t(str) {
const locale = this.locale || this.fallbackLocale;

if (!this.translations[str]) {
throw new Error(`${str} Translation string not found`);
}

if (!this.translations[str][locale]) {
throw new Error(`'${str}' translation not found for locale '${locale}'`);
if (!this.translations[str][this.locale]) {
throw new Error(
`'${str}' translation not found for locale '${this.locale}'`,
);
}

return this.translations[str][locale];
return this.translations[str][this.locale];
}
}

Expand Down

0 comments on commit 2f243f1

Please sign in to comment.