Skip to content

A platform for community discussion. Free, open, simple.

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

David Taylore5a2ed596cc4
diff --git a/app/assets/javascripts/discourse-i18n/src/index.js b/app/assets/javascripts/discourse-i18n/src/index.js
index d30570d01e4..f2bf46364d1 100644
--- a/app/assets/javascripts/discourse-i18n/src/index.js
+++ b/app/assets/javascripts/discourse-i18n/src/index.js
@@ -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 {