From 29829c701b212c23ef7acb7bb4806821d7b2938c Mon Sep 17 00:00:00 2001 From: Michael Kaply Date: Wed, 2 Mar 2016 15:04:38 -0600 Subject: [PATCH] Bug 782924 - Allow locale specific preferences in distribution.ini; r=mixedpuppy --- browser/components/distribution.js | 57 ++++++++++++++++--- .../components/tests/unit/distribution.ini | 24 +++++++- .../tests/unit/test_distribution.js | 22 ++++++- 3 files changed, 92 insertions(+), 11 deletions(-) diff --git a/browser/components/distribution.js b/browser/components/distribution.js index 19f278f4208..49f100efafb 100644 --- a/browser/components/distribution.js +++ b/browser/components/distribution.js @@ -346,11 +346,47 @@ DistributionCustomizer.prototype = { Cu.reportError(e); } + var usedPreferences = []; + + if (sections["Preferences-" + this._locale]) { + for (let key of enumerate(this._ini.getKeys("Preferences-" + this._locale))) { + try { + let value = this._ini.getString("Preferences-" + this._locale, key); + if (value) { + Preferences.set(key, parseValue(value)); + } + usedPreferences.push(key); + } catch (e) { /* ignore bad prefs and move on */ } + } + } + + if (sections["Preferences-" + this._language]) { + for (let key of enumerate(this._ini.getKeys("Preferences-" + this._language))) { + if (usedPreferences.indexOf(key) > -1) { + continue; + } + try { + let value = this._ini.getString("Preferences-" + this._language, key); + if (value) { + Preferences.set(key, parseValue(value)); + } + usedPreferences.push(key); + } catch (e) { /* ignore bad prefs and move on */ } + } + } + if (sections["Preferences"]) { for (let key of enumerate(this._ini.getKeys("Preferences"))) { + if (usedPreferences.indexOf(key) > -1) { + continue; + } try { - let value = parseValue(this._ini.getString("Preferences", key)); - Preferences.set(key, value); + let value = this._ini.getString("Preferences", key); + if (value) { + value = value.replace(/%LOCALE%/g, this._locale); + value = value.replace(/%LANGUAGE%/g, this._language); + Preferences.set(key, parseValue(value)); + } } catch (e) { /* ignore bad prefs and move on */ } } } @@ -363,8 +399,9 @@ DistributionCustomizer.prototype = { if (sections["LocalizablePreferences-" + this._locale]) { for (let key of enumerate(this._ini.getKeys("LocalizablePreferences-" + this._locale))) { try { - let value = parseValue(this._ini.getString("LocalizablePreferences-" + this._locale, key)); - if (value !== undefined) { + let value = this._ini.getString("LocalizablePreferences-" + this._locale, key); + if (value) { + value = parseValue(value); localizedStr.data = "data:text/plain," + key + "=" + value; defaults._prefBranch.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr); } @@ -379,8 +416,9 @@ DistributionCustomizer.prototype = { continue; } try { - let value = parseValue(this._ini.getString("LocalizablePreferences-" + this._language, key)); - if (value !== undefined) { + let value = this._ini.getString("LocalizablePreferences-" + this._language, key); + if (value) { + value = parseValue(value); localizedStr.data = "data:text/plain," + key + "=" + value; defaults._prefBranch.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr); } @@ -395,13 +433,14 @@ DistributionCustomizer.prototype = { continue; } try { - let value = parseValue(this._ini.getString("LocalizablePreferences", key)); - if (value !== undefined) { + let value = this._ini.getString("LocalizablePreferences", key); + if (value) { + value = parseValue(value); value = value.replace(/%LOCALE%/g, this._locale); value = value.replace(/%LANGUAGE%/g, this._language); localizedStr.data = "data:text/plain," + key + "=" + value; - defaults._prefBranch.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr); } + defaults._prefBranch.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr); } catch (e) { /* ignore bad prefs and move on */ } } } diff --git a/browser/components/tests/unit/distribution.ini b/browser/components/tests/unit/distribution.ini index 618fa1ebfa7..d7d29880833 100644 --- a/browser/components/tests/unit/distribution.ini +++ b/browser/components/tests/unit/distribution.ini @@ -13,6 +13,28 @@ distribution.test.string.noquotes=Test String distribution.test.int=777 distribution.test.bool.true=true distribution.test.bool.false=false +distribution.test.empty= + +distribution.test.pref.locale="%LOCALE%" +distribution.test.pref.language.reset="Preference Set" +distribution.test.pref.locale.reset="Preference Set" +distribution.test.pref.locale.set="Preference Set" +distribution.test.pref.language.set="Preference Set" + +[Preferences-en] +distribution.test.pref.language.en="en" +distribution.test.pref.language.reset= +distribution.test.pref.language.set="Language Set" +distribution.test.pref.locale.set="Language Set" + +[Preferences-en-US] +distribution.test.pref.locale.en-US="en-US" +distribution.test.pref.locale.reset= +distribution.test.pref.locale.set="Locale Set" + + +[Preferences-de] +distribution.test.pref.language.de="de" [LocalizablePreferences] distribution.test.locale="%LOCALE%" @@ -33,4 +55,4 @@ distribution.test.locale.reset= distribution.test.locale.set="Locale Set" [LocalizablePreferences-de] -distribution.test.locale.de="de" +distribution.test.language.de="de" diff --git a/browser/components/tests/unit/test_distribution.js b/browser/components/tests/unit/test_distribution.js index efb2e6b2a71..15353a4b44d 100644 --- a/browser/components/tests/unit/test_distribution.js +++ b/browser/components/tests/unit/test_distribution.js @@ -71,10 +71,30 @@ add_task(function* () { Assert.equal(Services.prefs.getIntPref("distribution.test.int"), 777); Assert.equal(Services.prefs.getBoolPref("distribution.test.bool.true"), true); Assert.equal(Services.prefs.getBoolPref("distribution.test.bool.false"), false); + + Assert.throws(() => Services.prefs.getCharPref("distribution.test.empty")); + Assert.throws(() => Services.prefs.getIntPref("distribution.test.empty")); + Assert.throws(() => Services.prefs.getBoolPref("distribution.test.empty")); + + Assert.equal(Services.prefs.getCharPref("distribution.test.pref.locale"), "en-US"); + Assert.equal(Services.prefs.getCharPref("distribution.test.pref.language.en"), "en"); + Assert.equal(Services.prefs.getCharPref("distribution.test.pref.locale.en-US"), "en-US"); + Assert.throws(() => Services.prefs.getCharPref("distribution.test.pref.language.de")); + // This value was never set because of the empty language specific pref + Assert.throws(() => Services.prefs.getCharPref("distribution.test.pref.language.reset")); + // This value was never set because of the empty locale specific pref + Assert.throws(() => Services.prefs.getCharPref("distribution.test.pref.locale.reset")); + // This value was overridden by a locale specific setting + Assert.equal(Services.prefs.getCharPref("distribution.test.pref.locale.set"), "Locale Set"); + // This value was overridden by a language specific setting + Assert.equal(Services.prefs.getCharPref("distribution.test.pref.language.set"), "Language Set"); + // Language should not override locale + Assert.notEqual(Services.prefs.getCharPref("distribution.test.pref.locale.set"), "Language Set"); + Assert.equal(Services.prefs.getComplexValue("distribution.test.locale", Ci.nsIPrefLocalizedString).data, "en-US"); Assert.equal(Services.prefs.getComplexValue("distribution.test.language.en", Ci.nsIPrefLocalizedString).data, "en"); Assert.equal(Services.prefs.getComplexValue("distribution.test.locale.en-US", Ci.nsIPrefLocalizedString).data, "en-US"); - Assert.throws(() => Services.prefs.getComplexValue("distribution.test.locale.de", Ci.nsIPrefLocalizedString)); + Assert.throws(() => Services.prefs.getComplexValue("distribution.test.language.de", Ci.nsIPrefLocalizedString)); // This value was never set because of the empty language specific pref Assert.throws(() => Services.prefs.getComplexValue("distribution.test.language.reset", Ci.nsIPrefLocalizedString)); // This value was never set because of the empty locale specific pref