Modular logistics and supply chain operating system (LSOS)
JavaScript driver-appfleet-managementfreight-managementinfrastructureinventory-managementlast-mile-deliverylogisticsondemandopen-sourceorder-managementroute-optimizationsupply-chain-managementtracking-and-monitoringtransport-management-systemtransportationwarehouse-management
fix(intl): default the Intl polyfills to the browser language, not Arabic
Ronald A. Richardson2b853625a4e3
diff --git a/console/app/initializers/load-intl-polyfills.js b/console/app/initializers/load-intl-polyfills.jsindex 947a8a8d..6d49e798 100644--- a/console/app/initializers/load-intl-polyfills.js+++ b/console/app/initializers/load-intl-polyfills.js@@ -11,6 +11,8 @@ function langOf(tag = 'en') {return String(tag).toLowerCase().split('-')[0];}+// The polyfills always replace native Intl: browsers lack data for some of the console's+// languages (Mongolian, for one), and ember-intl refuses to start without it.async function loadBasePolyfills() {await import('@formatjs/intl-numberformat/polyfill-force');await import('@formatjs/intl-pluralrules/polyfill-force');@@ -18,6 +20,32 @@ async function loadBasePolyfills() {await import('@formatjs/intl-relativetimeformat/polyfill-force');}+/**+ * The first of the browser's preferred languages that has locale data loaded, or English.+ */+export function preferredLanguage(langs, browserLanguages = []) {+ const match = browserLanguages.map((tag) => langOf(tag)).find((lang) => langs.includes(lang));++ return match ?? (langs.includes('en') ? 'en' : langs[0]);+}++/**+ * A formatjs polyfill takes its default locale from whichever locale data registers first.+ * Locale data loads for every translation at once, so without this the default becomes an+ * arbitrary language (Arabic, being first alphabetically) and every Intl call made without+ * an explicit locale formats numbers and dates in it. Pins the polyfills to the user's+ * language instead. A native constructor has no __defaultLocale and is left alone.+ */+export function pinDefaultLocale(lang, intl = Intl) {+ for (const name of ['NumberFormat', 'PluralRules', 'DateTimeFormat', 'RelativeTimeFormat']) {+ const constructor = intl[name];++ if (constructor && typeof constructor.__defaultLocale === 'string') {+ constructor.__defaultLocale = lang;+ }+ }+}+async function loadLocaleData(lang) {// initialize() already filters the language list through isValidLang, so this is a// second line of defence that the initializer itself can never trigger.@@ -46,6 +74,7 @@ export function initialize(application) {(async () => {await loadBasePolyfills();await all(langs.map(loadLocaleData));+ pinDefaultLocale(preferredLanguage(langs, window.navigator.languages));application.advanceReadiness();})();}diff --git a/console/tests/unit/initializers/load-intl-polyfills-test.js b/console/tests/unit/initializers/load-intl-polyfills-test.jsindex c72ca012..6c797112 100644--- a/console/tests/unit/initializers/load-intl-polyfills-test.js+++ b/console/tests/unit/initializers/load-intl-polyfills-test.js@@ -1,5 +1,5 @@import { module, test } from 'qunit';-import { initialize } from '@fleetbase/console/initializers/load-intl-polyfills';+import { initialize, pinDefaultLocale, preferredLanguage } from '@fleetbase/console/initializers/load-intl-polyfills';module('Unit | Initializer | load-intl-polyfills', function () {test('it defers readiness while the polyfills load and advances afterwards', async function (assert) {@@ -26,3 +26,44 @@ module('Unit | Initializer | load-intl-polyfills', function () {assert.deepEqual(calls, ['defer', 'advance'], 'readiness advances once the polyfills resolve');});});++module('Unit | Initializer | load-intl-polyfills | default locale', function () {+ test('the preferred language is the first browser language with locale data, else English', function (assert) {+ const langs = ['ar', 'de', 'en', 'fr'];++ assert.strictEqual(preferredLanguage(langs, ['fr-CA', 'en-US']), 'fr');+ assert.strictEqual(preferredLanguage(langs, ['ja-JP', 'en-SG']), 'en');+ assert.strictEqual(preferredLanguage(langs, ['ja-JP']), 'en');+ assert.strictEqual(preferredLanguage(['ar', 'de'], []), 'ar');+ });++ test('a polyfill is pinned to that language instead of the first locale data it loaded', function (assert) {+ // A polyfill records the first locale data it receives as its default ("ar" here); native+ // constructors carry no __defaultLocale and must be left untouched.+ const polyfilled = { __defaultLocale: 'ar' };+ const native = function NativeDateTimeFormat() {};+ const intl = { NumberFormat: polyfilled, PluralRules: { __defaultLocale: 'ar' }, DateTimeFormat: native };++ pinDefaultLocale('en', intl);++ assert.strictEqual(intl.NumberFormat.__defaultLocale, 'en');+ assert.strictEqual(intl.PluralRules.__defaultLocale, 'en');+ assert.strictEqual(native.__defaultLocale, undefined);+ });++ test('after loading, formatting without a locale follows the browser language, not the first locale loaded', async function (assert) {+ let advanced;+ const didAdvance = new Promise((resolve) => (advanced = resolve));++ initialize({ deferReadiness() {}, advanceReadiness: () => advanced() });+ await didAdvance;++ const language = (tag) => String(tag).toLowerCase().split('-')[0];+ const browser = preferredLanguage(['ar', 'bg', 'de', 'en', 'es', 'fa', 'fr', 'it', 'mn', 'pt', 'ru', 'uz', 'vi', 'zh'], navigator.languages);++ assert.strictEqual(language(new Intl.NumberFormat().resolvedOptions().locale), browser);+ assert.strictEqual(language(new Intl.DateTimeFormat().resolvedOptions().locale), browser);+ // Every console language still has data, which ember-intl needs to start.+ assert.strictEqual(Intl.NumberFormat.supportedLocalesOf(['mn']).length, 1);+ });+});