DEV: make verbose localization work without mutating I18n.t (#30205)

We now extensively reference the `{ i18n }` named export of the `discourse-i18n` package, instead of calling `I18n.t` directly. That means that mutations of `I18n.t` no longer have any impact on most of the app.

This commit updates the verbose localisation logic to be switched by a boolean instead of a method mutation.
This commit is contained in:
David Taylor 2024-12-10 15:46:36 +00:00 committed by GitHub
parent 0affb5c0bc
commit e5a2ed596c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -21,10 +21,16 @@ export class I18n {
extras = null;
noFallbacks = false;
testing = false;
verbose = false;
verboseIndicies = new Map();
pluralizationRules = Cardinals;
translate = (scope, options) => this._translate(scope, options);
translate = (scope, options) => {
return this.verbose
? this._verboseTranslate(scope, options)
: this._translate(scope, options);
};
// shortcut
t = this.translate;
@ -45,25 +51,8 @@ export class I18n {
}
enableVerboseLocalization() {
let counter = 0;
let keys = {};
this.noFallbacks = true;
this.t = this.translate = (scope, options) => {
let current = keys[scope];
if (!current) {
current = keys[scope] = ++counter;
let message = "Translation #" + current + ": " + scope;
if (options && Object.keys(options).length > 0) {
message += ", parameters: " + JSON.stringify(options);
}
// eslint-disable-next-line no-console
console.info(message);
}
return this._translate(scope, options) + " (#" + current + ")";
};
this.verbose = true;
}
enableVerboseLocalizationSession() {
@ -397,6 +386,22 @@ export class I18n {
return err.message;
}
}
verboseTranslate(scope, options) {
const result = this._translate(scope, options);
let i = this.verboseIndicies.get(scope);
if (!i) {
i = this.verboseIndicies.size + 1;
this.verboseIndicies.set(scope, i);
}
let message = `Translation #${i}: ${scope}`;
if (options && Object.keys(options).length > 0) {
message += `, parameters: ${JSON.stringify(options)}`;
}
// eslint-disable-next-line no-console
console.info(message);
return `${result} (#${i})`;
}
}
export class I18nMissingInterpolationArgument extends Error {