From 943f0dcfa0d3af65cc03900dc569edc5cae871e5 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 8 Sep 2021 08:00:29 -0400 Subject: [PATCH] DEV: Add support to Ember CLI for running message format in tests (#14271) --- .../discourse/lib/translation-plugin.js | 29 ++++++- app/assets/javascripts/discourse/package.json | 3 +- app/assets/javascripts/yarn.lock | 76 +++++++++++++++++++ 3 files changed, 105 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/translation-plugin.js b/app/assets/javascripts/discourse/lib/translation-plugin.js index cc448934501..ca907b69dd2 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 07da58e6c9f..5d8808047f4 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 1110338971b..ffb5e3c1e14 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" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-0.2.14.tgz#c74e780574f63c6f9a090e90efbe6ef53a6a756a" + integrity sha1-x054BXT2PG+aCQ6Q775u9TpqdWo= + dependencies: + lru-cache "2" + sigmund "~1.0.0" + minimist@^0.2.1: version "0.2.1" resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.2.1.tgz#827ba4e7593464e7c221e8c5bed930904ee2c455" @@ -8743,6 +8797,13 @@ nopt@^3.0.6: dependencies: abbrev "1" +nopt@~2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/nopt/-/nopt-2.0.0.tgz#ca7416f20a5e3f9c3b86180f96295fa3d0b52e0d" + integrity sha1-ynQW8gpeP5w7hhgPlilfo9C1Lg0= + dependencies: + abbrev "1" + normalize-path@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-2.1.1.tgz#1ab28b556e198363a8c1a6f7e6fa20137fe6aed9" @@ -10252,6 +10313,11 @@ side-channel@^1.0.4: get-intrinsic "^1.0.2" object-inspect "^1.9.0" +sigmund@~1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/sigmund/-/sigmund-1.0.1.tgz#3ff21f198cad2175f9f3b781853fd94d0d19b590" + integrity sha1-P/IfGYytIXX587eBhT/ZTQ0ZtZA= + signal-exit@^3.0.0, signal-exit@^3.0.2: version "3.0.3" resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.3.tgz#a1410c2edd8f077b08b4e253c8eacfcaf057461c" @@ -11276,6 +11342,11 @@ underscore@>=1.8.3: resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.13.0.tgz#3ccdcbb824230fc6bf234ad0ddcd83dff4eafe5f" integrity sha512-sCs4H3pCytsb5K7i072FAEC9YlSYFIbosvM0tAKAlpSSUgD7yC1iXSEGdl5XrDKQ1YUB+p/HDzYrSG2H2Vl36g== +underscore@~1.3.1: + version "1.3.3" + resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.3.3.tgz#47ac53683daf832bfa952e1774417da47817ae42" + integrity sha1-R6xTaD2vgyv6lS4XdEF9pHgXrkI= + unicode-canonical-property-names-ecmascript@^1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-1.0.4.tgz#2619800c4c825800efdd8343af7dd9933cbe2818" @@ -11601,6 +11672,11 @@ watchpack@^1.7.4: chokidar "^3.4.1" watchpack-chokidar2 "^2.0.1" +watchr@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/watchr/-/watchr-1.0.0.tgz#ce023fd59edae9430523031915c1812ff2302c27" + integrity sha1-zgI/1Z7a6UMFIwMZFcGBL/IwLCc= + wcwidth@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/wcwidth/-/wcwidth-1.0.1.tgz#f0b0dcf915bc5ff1528afadb2c0e17b532da2fe8"