diff --git a/app/assets/javascripts/discourse-i18n/src/index.js b/app/assets/javascripts/discourse-i18n/src/index.js index a9db4df2ff1..236f0e1e91f 100644 --- a/app/assets/javascripts/discourse-i18n/src/index.js +++ b/app/assets/javascripts/discourse-i18n/src/index.js @@ -379,7 +379,7 @@ export class I18n { } messageFormat(key, options) { - const message = this._mfMessages.hasMessage( + const message = this._mfMessages?.hasMessage( key, this._mfMessages.locale, this._mfMessages.defaultLocale diff --git a/app/assets/javascripts/discourse/tests/unit/lib/i18n-test.js b/app/assets/javascripts/discourse/tests/unit/lib/i18n-test.js index a7314664fe7..4ec3a448b34 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/i18n-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/i18n-test.js @@ -12,6 +12,7 @@ module("Unit | Utility | i18n", function (hooks) { this._translations = I18n.translations; this._extras = I18n.extras; this._pluralizationRules = { ...I18n.pluralizationRules }; + this._mfMessages = I18n._mfMessages; I18n.locale = "fr"; @@ -109,6 +110,7 @@ module("Unit | Utility | i18n", function (hooks) { I18n.translations = this._translations; I18n.extras = this._extras; I18n.pluralizationRules = this._pluralizationRules; + I18n._mfMessages = this._mfMessages; }); test("defaults", function (assert) { @@ -356,4 +358,19 @@ module("Unit | Utility | i18n", function (hooks) { ); }); }); + + test("messageFormat", function (assert) { + assert.ok( + I18n.messageFormat("posts_likes_MF", { count: 2, ratio: "high" }).match( + /2 replies/ + ), + "It works properly" + ); + I18n._mfMessages = null; + assert.strictEqual( + I18n.messageFormat("posts_likes_MF", { count: 2, ratio: "high" }), + "Missing Key: posts_likes_MF", + "Degrades gracefully if MF definitions are not available." + ); + }); });