Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #3048 from dmose/negotiateLocales
Browse files Browse the repository at this point in the history
Fix(l10n): closes #3003, makes locale fallback work
  • Loading branch information
Dan Mosedale committed Jul 31, 2017
2 parents 2445903 + 0ef253c commit c51a689
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
9 changes: 8 additions & 1 deletion system-addon/lib/LocalizationFeed.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ this.LocalizationFeed = class LocalizationFeed {
}

updateLocale() {
let locale = Services.locale.getRequestedLocale() || DEFAULT_LOCALE;
// Just take the first element in the result array, as it should be
// the best locale
let locale = Services.locale.negotiateLanguages(
Services.locale.getAppLocalesAsLangTags(), // desired locales
Object.keys(this.allStrings), // available locales
DEFAULT_LOCALE // fallback
)[0];

let strings = this.allStrings[locale];

// Use the default strings for any that are missing
Expand Down
14 changes: 8 additions & 6 deletions system-addon/test/unit/lib/LocalizationFeed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ describe("Localization Feed", () => {
sandbox = globals.sandbox;
feed = new LocalizationFeed();
feed.store = {dispatch: sinon.spy()};

sandbox.stub(global.Services.locale, "getRequestedLocale");
});
afterEach(() => {
globals.restore();
Expand All @@ -50,6 +48,8 @@ describe("Localization Feed", () => {

it("should dispatch with locale and strings for default", () => {
const locale = DEFAULT_LOCALE;
sandbox.stub(global.Services.locale, "negotiateLanguages")
.returns([locale]);
feed.updateLocale();

assert.calledOnce(feed.store.dispatch);
Expand All @@ -60,7 +60,8 @@ describe("Localization Feed", () => {
});
it("should use strings for other locale", () => {
const locale = "it";
global.Services.locale.getRequestedLocale.returns(locale);
sandbox.stub(global.Services.locale, "negotiateLanguages")
.returns([locale]);

feed.updateLocale();

Expand All @@ -72,7 +73,8 @@ describe("Localization Feed", () => {
});
it("should use some fallback strings for partial locale", () => {
const locale = "ru";
global.Services.locale.getRequestedLocale.returns(locale);
sandbox.stub(global.Services.locale, "negotiateLanguages")
.returns([locale]);

feed.updateLocale();

Expand All @@ -87,8 +89,8 @@ describe("Localization Feed", () => {
});
it("should use all default strings for unknown locale", () => {
const locale = "xyz";
global.Services.locale.getRequestedLocale.returns(locale);

sandbox.stub(global.Services.locale, "negotiateLanguages")
.returns([locale]);
feed.updateLocale();

assert.calledOnce(feed.store.dispatch);
Expand Down
6 changes: 5 additions & 1 deletion system-addon/test/unit/unit-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ overrider.set({
fetch() {},
Preferences: FakePrefs,
Services: {
locale: {getRequestedLocale() {}},
locale: {
getAppLocalesAsLangTags() {},
getRequestedLocale() {},
negotiateLanguages() {}
},
urlFormatter: {formatURL: str => str},
mm: {
addMessageListener: (msg, cb) => cb(),
Expand Down

0 comments on commit c51a689

Please sign in to comment.