DEV: Add support to Ember CLI for running message format in tests (#14271)

DEV: Add support to Ember CLI for running message format in tests (#14271)

diff --git a/app/assets/javascripts/discourse/lib/translation-plugin.js b/app/assets/javascripts/discourse/lib/translation-plugin.js
index cc44893..ca907b6 100644
--- a/app/assets/javascripts/discourse/lib/translation-plugin.js
+++ b/app/assets/javascripts/discourse/lib/translation-plugin.js
@@ -3,6 +3,7 @@ const Yaml = require("js-yaml");
 const fs = require("fs");
 const concat = require("broccoli-concat");
 const mergeTrees = require("broccoli-merge-trees");
+const MessageFormat = require("messageformat");
 
 let built = false;
 
@@ -16,6 +17,21 @@ class TranslationPlugin extends Plugin {
     this.inputFile = inputFile;
   }
 
+  replaceMF(formats, input, path = []) {
+    Object.keys(input).forEach((key) => {
+      let value = input[key];
+
+      let subpath = path.concat(key);
+      if (typeof value === "object") {
+        this.replaceMF(formats, value, subpath);
+      } else if (key.endsWith("_MF")) {
+        // omit locale.js
+        let mfPath = subpath.slice(2).join(".");
+        formats[mfPath] = this.mf.precompile(this.mf.parse(value));
+      }
+    });
+  }
+
   build() {
     // We could get fancy eventually and do this based on whether the yaml
     // or vendor files change but in practice we shouldn't need exact up to date
@@ -38,11 +54,19 @@ class TranslationPlugin extends Plugin {
     delete parsed.en.admin_js;
     delete parsed.en.wizard_js;
 
+    let formats = {};
+    this.mf = new MessageFormat("en");
+    this.replaceMF(formats, parsed);
+    this.replaceMF(formats, extras);
+
+    formats = Object.keys(formats).map((k) => `"${k}": ${formats[k]}`);
+
     let contents = `
       I18n.locale = 'en';
       I18n.translations = ${JSON.stringify(parsed)};
       I18n.extras = ${JSON.stringify(extras)};
-      I18n._compiledMFs = {};
+      MessageFormat = { locale: {} };
+      I18n._compiledMFs = { ${formats.join(",\n")} };
     `;
 
     fs.writeFileSync(
@@ -76,6 +100,7 @@ module.exports.createI18nTree = function (discourseRoot, vendorJs) {
         "moment.js",
         "moment-timezone-with-data.js",
         "messageformat-lookup.js",
+        "locale/en.js",
         "client.en.js",
       ],
       headerFiles: [
@@ -84,7 +109,7 @@ module.exports.createI18nTree = function (discourseRoot, vendorJs) {
         "moment-timezone-with-data.js",
         "messageformat-lookup.js",
       ],
-      footerFiles: ["client.en.js"],
+      footerFiles: ["client.en.js", "locale/en.js"],
       outputFile: `assets/test-i18n.js`,
     }
   );
diff --git a/app/assets/javascripts/discourse/package.json b/app/assets/javascripts/discourse/package.json
index 07da58e..5d88080 100644
--- a/app/assets/javascripts/discourse/package.json
+++ b/app/assets/javascripts/discourse/package.json
@@ -21,9 +21,9 @@
     "@ember/test-helpers": "^2.2.0",
     "@glimmer/component": "^1.0.0",
     "@popperjs/core": "2.9.3",
-    "@uppy/core": "^2.0.1",
     "@uppy/aws-s3": "^2.0.1",
     "@uppy/aws-s3-multipart": "^2.0.2",
+    "@uppy/core": "^2.0.1",
     "@uppy/drop-target": "^1.0.1",
     "@uppy/xhr-upload": "^2.0.1",
     "admin": "^1.0.0",
@@ -52,6 +52,7 @@
     "js-yaml": "^4.0.0",
     "loader.js": "^4.7.0",
     "message-bus-client": "^3.3.0",
+    "messageformat": "0.1.5",
     "mousetrap": "^1.6.5",
     "mousetrap-global-bind": "^1.1.0",
     "pretender": "^3.4.7",
diff --git a/app/assets/javascripts/yarn.lock b/app/assets/javascripts/yarn.lock
index 1110338..ffb5e3c 100644
--- a/app/assets/javascripts/yarn.lock
+++ b/app/assets/javascripts/yarn.lock
@@ -1997,6 +1997,11 @@ async@^2.4.1, async@^2.6.2:
   dependencies:
     lodash "^4.17.14"
 
+async@~0.1.18:
+  version "0.1.22"
+  resolved "https://registry.yarnpkg.com/async/-/async-0.1.22.tgz#0fc1aaa088a0e3ef0ebe2d8831bab0dcf8845061"
+  integrity sha1-D8GqoIig4+8Ovi2IMbqw3PiEUGE=
+
 async@~0.2.9:
   version "0.2.10"
   resolved "https://registry.yarnpkg.com/async/-/async-0.2.10.tgz#b6bbe0b0674b9d719708ca38de8c237cb526c3d1"
@@ -3968,6 +3973,11 @@ code-point-at@^1.0.0:
   resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77"
   integrity sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=
 
+coffee-script@~1.2.0:
+  version "1.2.0"
+  resolved "https://registry.yarnpkg.com/coffee-script/-/coffee-script-1.2.0.tgz#b5e61e55f1ca8c4a9eb87d53aa0657ea43125b91"
+  integrity sha1-teYeVfHKjEqeuH1TqgZX6kMSW5E=
+
 collection-visit@^1.0.0:
   version "1.0.0"
   resolved "https://registry.yarnpkg.com/collection-visit/-/collection-visit-1.0.0.tgz#4bc0373c164bc3291b4d368c829cf1a80a59dca0"
@@ -6640,6 +6650,15 @@ glob@^7.0.4, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6:
     once "^1.3.0"
     path-is-absolute "^1.0.0"
 
+glob@~3.1.9:
+  version "3.1.21"
+  resolved "https://registry.yarnpkg.com/glob/-/glob-3.1.21.tgz#d29e0a055dea5138f4d07ed40e8982e83c2066cd"
+  integrity sha1-0p4KBV3qUTj00H7UDomC6DwgZs0=
+  dependencies:
+    graceful-fs "~1.2.0"
+    inherits "1"
+    minimatch "~0.2.11"
+
 global-modules@^1.0.0:
   version "1.0.0"
   resolved "https://registry.yarnpkg.com/global-modules/-/global-modules-1.0.0.tgz#6d770f0eb523ac78164d72b5e71a8877265cc3ea"
@@ -6775,6 +6794,11 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.3
   resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.6.tgz#ff040b2b0853b23c3d31027523706f1885d76bee"
   integrity sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ==
 
+graceful-fs@~1.2.0:
+  version "1.2.3"
+  resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-1.2.3.tgz#15a4806a57547cb2d2dbf27f42e89a8c3451b364"
+  integrity sha1-FaSAaldUfLLS2/J/QuiajDRRs2Q=
+
 "graceful-readlink@>= 1.0.0":
   version "1.0.1"
   resolved "https://registry.yarnpkg.com/graceful-readlink/-/graceful-readlink-1.0.1.tgz#4cafad76bc62f02fa039b2f94e9a3dd3a391a725"
@@ -7157,6 +7181,11 @@ inflight@^1.0.4:
     once "^1.3.0"
     wrappy "1"
 
+inherits@1:
+  version "1.0.2"
+  resolved "https://registry.yarnpkg.com/inherits/-/inherits-1.0.2.tgz#ca4309dadee6b54cc0b8d247e8d7c7a0975bdc9b"
+  integrity sha1-ykMJ2t7mtUzAuNJH6NfHoJdb3Js=
+
 inherits@2, inherits@2.0.4, inherits@^2.0.1, inherits@^2.0.3, inherits@^2.0.4, inherits@~2.0.1, inherits@~2.0.3:
   version "2.0.4"
   resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
@@ -8167,6 +8196,11 @@ lowercase-keys@^1.0.0:
   resolved "https://registry.yarnpkg.com/lowercase-keys/-/lowercase-keys-1.0.1.tgz#6f9e30b47084d971a7c820ff15a6c5167b74c26f"
   integrity sha512-G2Lj61tXDnVFFOi8VZds+SoQjtQC3dgokKdDG2mTm1tx4m50NUHBOZSBwQQHyy0V12A0JTG4icfZQH+xPyh8VA==
 
+lru-cache@2:
+  version "2.7.3"
+  resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-2.7.3.tgz#6d4524e8b955f95d4f5b58851ce21dd72fb4e952"
+  integrity sha1-bUUk6LlV+V1PW1iFHOId1y+06VI=
+
 lru-cache@^5.1.1:
   version "5.1.1"
   resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920"
@@ -8364,6 +8398,18 @@ message-bus-client@^3.3.0:
   resolved "https://registry.yarnpkg.com/message-bus-client/-/message-bus-client-3.3.0.tgz#ee38ad26fa54f14dce17e65cc466be4e1aa7028b"
   integrity sha512-dPfGLsxTkw1fNaQ+9nwbB1TXH5NnLXapjGkJtW9fZVsq7fNHaSomiTHZfXJHBibePZ10RH6AuHGemWKqgMtlIA==
 
+messageformat@0.1.5:
+  version "0.1.5"
+  resolved "https://registry.yarnpkg.com/messageformat/-/messageformat-0.1.5.tgz#c7c561de181b04ef0fad36ca89c5cb942e5bb75c"
+  integrity sha1-x8Vh3hgbBO8PrTbKicXLlC5bt1w=
+  dependencies:
+    async "~0.1.18"
+    coffee-script "~1.2.0"
+    glob "~3.1.9"
+    nopt "~2.0.0"
+    underscore "~1.3.1"
+    watchr "~1.0.0"
+
 methods@~1.1.2:
   version "1.1.2"
   resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
@@ -8467,6 +8513,14 @@ minimalistic-crypto-utils@^1.0.1:
   dependencies:
     brace-expansion "^1.1.7"
 
+minimatch@~0.2.11:
+  version "0.2.14"

[... diff too long, it was truncated ...]

GitHub sha: 943f0dcfa0d3af65cc03900dc569edc5cae871e5

This commit appears in #14271 which was approved by CvX. It was merged by jjaffeux.