Skip to content

Commit

Permalink
Bug 1759577 - Create logic to check if live reloading is supported; r…
Browse files Browse the repository at this point in the history
…=platform-i18n-reviewers,dminor

Depends on D141003

Differential Revision: https://phabricator.services.mozilla.com/D141004
  • Loading branch information
gregtatum committed Mar 22, 2022
1 parent 9ace670 commit 85f57e0
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
15 changes: 15 additions & 0 deletions intl/locale/LangPackMatcher.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,28 @@ function getAppAndSystemLocaleInfo() {
type: "language",
});

// Live reloading with bidi switching may not be supported.
let canLiveReload = null;
if (systemLocale && appLocale) {
const systemDirection = Services.intl.getScriptDirection(
systemLocale.language
);
const appDirection = Services.intl.getScriptDirection(appLocale.language);
const supportsBidiSwitching = Services.prefs.getBoolPref(
"intl.multilingual.liveReloadBidirectional",
false
);
canLiveReload = systemDirection === appDirection || supportsBidiSwitching;
}

return {
// Return the Intl.Locale in a serializable form.
systemLocaleRaw,
systemLocale,
appLocaleRaw,
appLocale,
matchType,
canLiveReload,

// These can be used as Fluent message args.
displayNames: {
Expand Down
59 changes: 59 additions & 0 deletions intl/locale/tests/unit/test_langPackMatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* 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 { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { getAddonAndLocalAPIsMocker } = ChromeUtils.import(
"resource://testing-common/LangPackMatcherTestUtils.jsm"
);
Expand Down Expand Up @@ -32,6 +33,7 @@ add_task(function test_appLocaleLanguageMismatch() {
appLocaleRaw: "en-US",
appLocale: { baseName: "en-US", language: "en", region: "US" },
matchType: "language-mismatch",
canLiveReload: true,
displayNames: {
systemLanguage: "European Spanish",
appLanguage: "American English",
Expand All @@ -53,6 +55,7 @@ add_task(function test_appLocaleRegionMismatch() {
appLocaleRaw: "en-US",
appLocale: { baseName: "en-US", language: "en", region: "US" },
matchType: "region-mismatch",
canLiveReload: true,
displayNames: {
systemLanguage: "Canadian English",
appLanguage: "American English",
Expand All @@ -75,6 +78,7 @@ add_task(function test_appLocaleScriptMismatch() {
appLocaleRaw: "zh-CN",
appLocale: { baseName: "zh-CN", language: "zh", region: "CN" },
matchType: "match",
canLiveReload: true,
displayNames: {
systemLanguage: "简体中文(中国)",
appLanguage: "中文(中国)",
Expand All @@ -97,10 +101,65 @@ add_task(function test_appLocaleInvalidSystem() {
appLocaleRaw: "en-US",
appLocale: { baseName: "en-US", language: "en", region: "US" },
matchType: "unknown",
canLiveReload: null,
displayNames: { systemLanguage: null, appLanguage: "American English" },
});
});

add_task(function test_bidiSwitchDisabled() {
Services.prefs.setBoolPref(
"intl.multilingual.liveReloadBidirectional",
false
);
sandbox.restore();
// Script mismatch:
mockAddonAndLocaleAPIs({
sandbox,
systemLocale: "ar-EG",
appLocale: "en-US",
});

deepEqual(LangPackMatcher.getAppAndSystemLocaleInfo(), {
systemLocaleRaw: "ar-EG",
systemLocale: { baseName: "ar-EG", language: "ar", region: "EG" },
appLocaleRaw: "en-US",
appLocale: { baseName: "en-US", language: "en", region: "US" },
matchType: "language-mismatch",
canLiveReload: false,
displayNames: {
systemLanguage: "Arabic (Egypt)",
appLanguage: "American English",
},
});
Services.prefs.clearUserPref("intl.multilingual.liveReloadBidirectional");
});

add_task(async function test_bidiSwitchEnabled() {
Services.prefs.setBoolPref("intl.multilingual.liveReloadBidirectional", true);
sandbox.restore();
// Script mismatch:
mockAddonAndLocaleAPIs({
sandbox,
systemLocale: "ar-EG",
appLocale: "en-US",
});

deepEqual(LangPackMatcher.getAppAndSystemLocaleInfo(), {
systemLocaleRaw: "ar-EG",
systemLocale: { baseName: "ar-EG", language: "ar", region: "EG" },
appLocaleRaw: "en-US",
appLocale: { baseName: "en-US", language: "en", region: "US" },
matchType: "language-mismatch",
canLiveReload: true,
displayNames: {
systemLanguage: "Arabic (Egypt)",
appLanguage: "American English",
},
});

Services.prefs.clearUserPref("intl.multilingual.liveReloadBidirectional");
});

function shuffle(array) {
return array
.map(value => ({ value, sort: Math.random() }))
Expand Down

0 comments on commit 85f57e0

Please sign in to comment.