Skip to content

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

FIX: Ensure translation overrides are always available (#38113)

David Taylor7e8461fce899
diff --git a/frontend/discourse-i18n/src/index.js b/frontend/discourse-i18n/src/index.js
index 2f35166b395..840a55af57b 100644
--- a/frontend/discourse-i18n/src/index.js
+++ b/frontend/discourse-i18n/src/index.js
@@ -470,7 +470,7 @@ export class I18n {
this.#loadMessageFormatData(localeData.messageFormatData);
if (localeData.overrides) {
- this._overrides = localeData.overrides;
+ this.#applyOverrides(localeData.overrides);
}
if (localeData.extra?.admin_js) {
@@ -486,6 +486,28 @@ export class I18n {
}
}
+ #applyOverrides(overrides) {
+ for (const [locale, overridesForLocale] of Object.entries(
+ overrides || {}
+ )) {
+ for (const [key, value] of Object.entries(overridesForLocale)) {
+ const segs = key.replace(/^admin_js\./, "js.").split(".");
+ let node = this.translations[locale] || {};
+
+ for (let i = 0; i < segs.length - 1; i++) {
+ if (!(segs[i] in node)) {
+ node[segs[i]] = {};
+ }
+ node = node[segs[i]];
+ }
+
+ if (typeof node === "object") {
+ node[segs[segs.length - 1]] = value;
+ }
+ }
+ }
+ }
+
#loadMessageFormatData(callback) {
const msgData = callback({
"@messageformat/runtime/lib/cardinals": MessageFormatRuntimeCardinals,
diff --git a/frontend/discourse/app/instance-initializers/localization.js b/frontend/discourse/app/instance-initializers/localization.js
index 02bb3535703..6d29d269644 100644
--- a/frontend/discourse/app/instance-initializers/localization.js
+++ b/frontend/discourse/app/instance-initializers/localization.js
@@ -20,26 +20,5 @@ export default {
if (this.isVerboseLocalizationEnabled(owner)) {
I18n.enableVerboseLocalization();
}
-
- // Merge any overrides into our object
- for (const [locale, overrides] of Object.entries(I18n._overrides || {})) {
- for (const [key, value] of Object.entries(overrides)) {
- const segs = key.replace(/^admin_js\./, "js.").split(".");
- let node = I18n.translations[locale] || {};
-
- for (let i = 0; i < segs.length - 1; i++) {
- if (!(segs[i] in node)) {
- node[segs[i]] = {};
- }
- node = node[segs[i]];
- }
-
- if (typeof node === "object") {
- node[segs[segs.length - 1]] = value;
- }
- }
-
- delete I18n._overrides;
- }
},
};
diff --git a/frontend/discourse/tests/unit/localization-test.js b/frontend/discourse/tests/unit/localization-test.js
deleted file mode 100644
index 023614ec5e7..00000000000
--- a/frontend/discourse/tests/unit/localization-test.js
+++ /dev/null
@@ -1,168 +0,0 @@
-import { setupTest } from "ember-qunit";
-import { module, test } from "qunit";
-import LocalizationInitializer from "discourse/instance-initializers/localization";
-import I18n, { i18n } from "discourse-i18n";
-
-module("initializer:localization", function (hooks) {
- setupTest(hooks);
-
- hooks.beforeEach(function () {
- this._locale = I18n.locale;
- this._translations = I18n.translations;
- this._extras = I18n.extras;
- this._overrides = I18n._overrides;
-
- I18n.locale = "fr";
-
- I18n.translations = {
- fr: {
- js: {
- composer: {
- both_languages1: "composer.both_languages1 (FR)",
- both_languages2: "composer.both_languages2 (FR)",
- },
- },
- },
- en: {
- js: {
- composer: {
- both_languages1: "composer.both_languages1 (EN)",
- both_languages2: "composer.both_languages2 (EN)",
- only_english1: "composer.only_english1 (EN)",
- only_english2: "composer.only_english2 (EN)",
- },
- },
- },
- };
-
- I18n.extras = {
- fr: {
- admin: {
- api: {
- both_languages1: "admin.api.both_languages1 (FR)",
- both_languages2: "admin.api.both_languages2 (FR)",
- },
- },
- },
- en: {
- admin: {
- api: {
- both_languages1: "admin.api.both_languages1 (EN)",
- both_languages2: "admin.api.both_languages2 (EN)",
- only_english1: "admin.api.only_english1 (EN)",
- only_english2: "admin.api.only_english2 (EN)",
- },
- },
- },
- };
- });
-
- hooks.afterEach(function () {
- I18n.locale = this._locale;
- I18n.translations = this._translations;
- I18n.extras = this._extras;
- I18n._overrides = this._overrides;
- });
-
- test("translation overrides", function (assert) {
- I18n._overrides = {
- fr: {
- "js.composer.both_languages1": "composer.both_languages1 (FR override)",
- "js.composer.only_english2": "composer.only_english2 (FR override)",
- },
- en: {
- "js.composer.both_languages2": "composer.both_languages2 (EN override)",
- "js.composer.only_english1": "composer.only_english1 (EN override)",
- },
- };
- LocalizationInitializer.initialize(this.owner);
-
- assert.strictEqual(
- i18n("composer.both_languages1"),
- "composer.both_languages1 (FR override)",
- "overrides existing translation in current locale"
- );
-
- assert.strictEqual(
- i18n("composer.only_english1"),
- "composer.only_english1 (EN override)",
- "overrides translation in fallback locale"
- );
-
- assert.strictEqual(
- i18n("composer.only_english2"),
- "composer.only_english2 (FR override)",
- "overrides translation that doesn't exist in current locale"
- );
-
- assert.strictEqual(
- i18n("composer.both_languages2"),
- "composer.both_languages2 (FR)",
- "prefers translation in current locale over override in fallback locale"
- );
- });
-
- test("translation overrides (admin_js)", function (assert) {
- I18n._overrides = {
- fr: {
- "admin_js.admin.api.both_languages1":
- "admin.api.both_languages1 (FR override)",
- "admin_js.admin.api.only_english2":
- "admin.api.only_english2 (FR override)",
- "admin_js.type_to_filter": "type_to_filter (FR override)",
- },
- en: {
- "admin_js.admin.api.both_languages2":
- "admin.api.both_languages2 (EN override)",
- "admin_js.admin.api.only_english1":
- "admin.api.only_english1 (EN override)",
- },
- };
- LocalizationInitializer.initialize(this.owner);
-
- assert.strictEqual(
- i18n("admin.api.both_languages1"),
- "admin.api.both_languages1 (FR override)",
- "overrides existing translation in current locale"
- );
-
- assert.strictEqual(
- i18n("admin.api.only_english1"),
- "admin.api.only_english1 (EN override)",
- "overrides translation in fallback locale"
- );
-
- assert.strictEqual(
- i18n("admin.api.only_english2"),
- "admin.api.only_english2 (FR override)",
- "overrides translation that doesn't exist in current locale"
- );
-
- assert.strictEqual(
- i18n("admin.api.both_languages2"),
- "admin.api.both_languages2 (FR)",
- "prefers translation in current locale over override in fallback locale"
- );
-
- assert.strictEqual(
- i18n("type_to_filter"),
- "type_to_filter (FR override)",
- "correctly changes the translation key by removing `admin_js`"
- );
- });
-
- test("skip translation override if parent node is not an object", function (assert) {
- I18n._overrides = {
- fr: {
- "js.composer.both_languages1.foo":
- "composer.both_languages1.foo (FR override)",
- },
- };
- LocalizationInitializer.initialize(this.owner);
-
- assert.strictEqual(
- i18n("composer.both_languages1.foo"),
- "[fr.composer.both_languages1.foo]"
- );
- });
-});
diff --git a/spec/system/i18n_override_spec.rb b/spec/system/i18n_override_spec.rb
new file mode 100644
index 00000000000..919877b19c4
--- /dev/null
+++ b/spec/system/i18n_override_spec.rb
@@ -0,0 +1,78 @@
+# frozen_string_literal: true
+
+describe "I18n translation overrides", type: :system do
+ describe "module-scope i18n() lookups" do
+ fab!(:theme) { Fabricate(:theme, name: "Test Theme") }
+ fab!(:translation_override) do
+ TranslationOverride.upsert!("en", "js.topic.create", "Overridden at module scope")
+ end
+
+ before do
+ theme.set_field(
+ target: :extra_js,
+ type: :js,
+ name: "discourse/connectors/below-footer/i18n-test-connector.gjs",
+ value: <<~JS,
+ import { i18n } from "discourse-i18n";
+
+ const translatedString = i18n("topic.create");
+
+ export default <template>
+ <div class="i18n-test-output">{{translatedString}}</div>
+ </template>
+ JS
+ )
+ theme.save!
+ SiteSetting.default_theme_id = theme.id
+ end
+
+ it "applies translation overrides to module-scope i18n() calls" do
+ visit("/latest")
+
+ expect(page).to have_css(".i18n-test-output", text: "Overridden at module scope")
+ end
+ end
+
+ describe "overrides" do
+ before do
+ TranslationOverride.upsert!("en", "js.dates.tiny.half_a_minute", "override for js key")
+ TranslationOverride.upsert!("en", "admin_js.admin.title", "override for admin_js key")
+ end
+
+ it "applies overrides to js and admin_js keys" do
+ visit("/latest")
+ expect(page).to have_css("#site-logo")
+
+ js_result = page.evaluate_script("I18n.t('dates.tiny.half_a_minute')")
+ expect(js_result).to eq("override for js key")
+
+ admin_js_result = page.evaluate_script("I18n.t('admin.title')")
+ expect(admin_js_result).to eq("override for admin_js key")
+ end
+ end
+
+ describe "fallback locale behavior" do
+ fab!(:user) { Fabricate(:user, locale: "de") }
+
+ before do
+ TranslationOverride.upsert!("de", "js.dates.tiny.half_a_minute", "de override")
+ # Override in fallback locale (en) for a key that exists in both
+ TranslationOverride.upsert!("en", "js.dates.tiny.x_minutes.one", "en override")
+ end
+
+ it "applies override from user locale and prefers user locale translation over fallback override" do
+ sign_in(user)
+ visit("/latest")
+ expect(page).to have_css("#site-logo")
+
+ # Override in user's locale is applied
+ de_override = page.evaluate_script("I18n.t('dates.tiny.half_a_minute')")
+ expect(de_override).to eq("de override")
+
+ # User locale translation is preferred over fallback locale override
+ # (de has this key, so en override should not be used)
+ fallback_result = page.evaluate_script("I18n.t('dates.tiny.x_minutes', { count: 1 })")
+ expect(fallback_result).not_to eq("en override")
+ end
+ end
+end