From 86a8ec763bb7952406d09d4fa77ba5344500dfbe Mon Sep 17 00:00:00 2001 From: Zibi Braniecki Date: Fri, 7 Jul 2017 11:32:53 -0700 Subject: [PATCH] Bug 1373061 - Fallback on en-US in sdk/l10n.get. r=Pike Users of getPreferedLocales method expect to always have `en-US` as a last resort. This patch brings back the behavior which regressed with the landing of bug 1346616. MozReview-Commit-ID: 8bgm7PJnSRJ --HG-- extra : rebase_source : c63ba6e4508117a6d4bd9d2a18a2c8665dd52bbe --- addon-sdk/source/lib/sdk/l10n/locale.js | 10 +++++++++- addon-sdk/source/test/test-l10n-locale.js | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/addon-sdk/source/lib/sdk/l10n/locale.js b/addon-sdk/source/lib/sdk/l10n/locale.js index 634ec08a763e..2d358538e280 100644 --- a/addon-sdk/source/lib/sdk/l10n/locale.js +++ b/addon-sdk/source/lib/sdk/l10n/locale.js @@ -12,7 +12,15 @@ const { Cu, Cc, Ci } = require("chrome"); const { Services } = Cu.import("resource://gre/modules/Services.jsm"); function getPreferedLocales(caseSensitve) { - return Services.locale.getRequestedLocales(); + const locales = Services.locale.getRequestedLocales(); + + // This API expects to always append en-US fallback, so for compatibility + // reasons, we're going to inject it here. + // See bug 1373061 for details. + if (!locales.includes('en-US')) { + locales.push('en-US'); + } + return locales; } exports.getPreferedLocales = getPreferedLocales; diff --git a/addon-sdk/source/test/test-l10n-locale.js b/addon-sdk/source/test/test-l10n-locale.js index e5afe31d501d..e4dbc55deb33 100644 --- a/addon-sdk/source/test/test-l10n-locale.js +++ b/addon-sdk/source/test/test-l10n-locale.js @@ -2,7 +2,10 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -const { findClosestLocale } = require("sdk/l10n/locale"); +const { Cu, Cc, Ci } = require("chrome"); +const { Services } = Cu.import('resource://gre/modules/Services.jsm'); +const { findClosestLocale, + getPreferedLocales } = require("sdk/l10n/locale"); exports.testFindClosestLocale = function(assert) { // Second param of findClosestLocale (aMatchLocales) have to be in lowercase @@ -44,4 +47,14 @@ exports.testFindClosestLocale = function(assert) { "ja", "We accept locale with 1 part from locale with 3 parts"); }; +exports.testGetPreferedLocales = function(assert) { + let currentLocales = Services.locale.getRequestedLocales(); + + Services.locale.setRequestedLocales(['pl']); + + assert.equal(getPreferedLocales().includes('en-US'), true, "en-US should always be in the list"); + + Services.locale.setRequestedLocales(currentLocales); +}; + require('sdk/test').run(exports);