diff --git a/app/assets/javascripts/discourse/initializers/localization.js.es6 b/app/assets/javascripts/discourse/initializers/localization.js.es6 index 2603ddaf4b..e4403e0489 100644 --- a/app/assets/javascripts/discourse/initializers/localization.js.es6 +++ b/app/assets/javascripts/discourse/initializers/localization.js.es6 @@ -4,31 +4,21 @@ export default { name: 'localization', after: 'inject-objects', - enableVerboseLocalization() { - let counter = 0; - let keys = {}; - let t = I18n.t; - I18n.noFallbacks = true; + isVerboseLocalizationEnabled(container) { + const siteSettings = container.lookup('site-settings:main'); + if (siteSettings.verbose_localization) return true; - I18n.t = I18n.translate = function(scope, value){ - let current = keys[scope]; - if (!current) { - current = keys[scope] = ++counter; - let message = "Translation #" + current + ": " + scope; - if (!_.isEmpty(value)) { - message += ", parameters: " + JSON.stringify(value); - } - Em.Logger.info(message); - } - return t.apply(I18n, [scope, value]) + " (#" + current + ")"; - }; + try { + return sessionStorage && sessionStorage.getItem("verbose_localization"); + } catch (e) { + return false; + } }, initialize(container) { - const siteSettings = container.lookup('site-settings:main'); - if (siteSettings.verbose_localization) { - this.enableVerboseLocalization(); + if (this.isVerboseLocalizationEnabled(container)) { + I18n.enableVerboseLocalization(); } // Merge any overrides into our object diff --git a/app/assets/javascripts/locales/i18n.js b/app/assets/javascripts/locales/i18n.js index b1efc2e112..0d27ee2fca 100644 --- a/app/assets/javascripts/locales/i18n.js +++ b/app/assets/javascripts/locales/i18n.js @@ -292,5 +292,33 @@ I18n.currentLocale = function() { return I18n.locale || I18n.defaultLocale; }; +I18n.enableVerboseLocalization = function() { + var counter = 0; + var keys = {}; + var t = I18n.t; + + I18n.noFallbacks = true; + + I18n.t = I18n.translate = function(scope, value){ + var current = keys[scope]; + if (!current) { + current = keys[scope] = ++counter; + var message = "Translation #" + current + ": " + scope; + if (!_.isEmpty(value)) { + message += ", parameters: " + JSON.stringify(value); + } + Em.Logger.info(message); + } + return t.apply(I18n, [scope, value]) + " (#" + current + ")"; + }; +}; + +I18n.enableVerboseLocalizationSession = function() { + sessionStorage.setItem("verbose_localization", "true"); + I18n.enableVerboseLocalization(); + + return 'Verbose localization is enabled. Close the browser tab to turn it off. Reload the page to see the translation keys.'; +}; + // shortcuts I18n.t = I18n.translate;