mirror of
https://github.com/System-End/github-readme-stats.git
synced 2026-04-19 21:05:16 +00:00
i18n: set locale only once instead of on each call (#3200)
This commit is contained in:
parent
8879c7fe2e
commit
b611018476
1 changed files with 8 additions and 7 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
const FALLBACK_LOCALE = "en";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* I18n translation class.
|
* I18n translation class.
|
||||||
*/
|
*/
|
||||||
|
|
@ -10,9 +12,8 @@ class I18n {
|
||||||
* @param {Object} options.translations Translations.
|
* @param {Object} options.translations Translations.
|
||||||
*/
|
*/
|
||||||
constructor({ locale, translations }) {
|
constructor({ locale, translations }) {
|
||||||
this.locale = locale;
|
this.locale = locale || FALLBACK_LOCALE;
|
||||||
this.translations = translations;
|
this.translations = translations;
|
||||||
this.fallbackLocale = "en";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -22,17 +23,17 @@ class I18n {
|
||||||
* @returns {string} Translated string.
|
* @returns {string} Translated string.
|
||||||
*/
|
*/
|
||||||
t(str) {
|
t(str) {
|
||||||
const locale = this.locale || this.fallbackLocale;
|
|
||||||
|
|
||||||
if (!this.translations[str]) {
|
if (!this.translations[str]) {
|
||||||
throw new Error(`${str} Translation string not found`);
|
throw new Error(`${str} Translation string not found`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.translations[str][locale]) {
|
if (!this.translations[str][this.locale]) {
|
||||||
throw new Error(`'${str}' translation not found for locale '${locale}'`);
|
throw new Error(
|
||||||
|
`'${str}' translation not found for locale '${this.locale}'`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.translations[str][locale];
|
return this.translations[str][this.locale];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue