Skip to content

Commit 2a168f6

Browse files
committed
Bug 1994642 - Use Services.locales.availableLanguages for dynamic newtab.ftl registration for train-hops. r=eemeli,home-newtab-reviewers,thecount
This registers the newtab.ftl Fluent files in a train-hopped XPI _just_ for the base application's availableLocales. If that set changes (for example, if the user installs a new language pack in the base app), then the we update the registration, always using the intersection of the available languages from the base app and the list of locales included with the XPI. Differential Revision: https://phabricator.services.mozilla.com/D269093
1 parent 9981cd6 commit 2a168f6

File tree

1 file changed

+65
-10
lines changed

1 file changed

+65
-10
lines changed

browser/components/newtab/AboutNewTabResourceMapping.sys.mjs

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export const TRAINHOP_SCHEDULED_UPDATE_STATE_DELAY_PREF =
1818
export const TRAINHOP_SCHEDULED_UPDATE_STATE_TIMEOUT_PREF =
1919
"browser.newtabpage.trainhopAddon.scheduledUpdateState.timeout";
2020

21+
const FLUENT_SOURCE_NAME = "newtab";
22+
const TOPIC_LOCALES_CHANGED = "intl:app-locales-changed";
23+
const TOPIC_SHUTDOWN = "profile-before-change";
24+
2125
const lazy = XPCOMUtils.declareLazy({
2226
AddonManager: "resource://gre/modules/AddonManager.sys.mjs",
2327
AddonSettings: "resource://gre/modules/addons/AddonSettings.sys.mjs",
@@ -82,6 +86,7 @@ export var AboutNewTabResourceMapping = {
8286
_addonListener: null,
8387
_builtinVersion: null,
8488
_updateAddonStateDeferredTask: null,
89+
_supportedLocales: null,
8590

8691
/**
8792
* Returns the version string for whichever version of New Tab is currently
@@ -291,17 +296,20 @@ export var AboutNewTabResourceMapping = {
291296
*/
292297
async registerFluentSources(rootURI) {
293298
try {
294-
const SUPPORTED_LOCALES = await fetch(
295-
rootURI.resolve("/locales/supported-locales.json")
296-
).then(r => r.json());
297-
const newtabFileSource = new L10nFileSource(
298-
"newtab",
299-
"app",
300-
SUPPORTED_LOCALES,
301-
`resource://newtab/locales/{locale}/`
299+
// Read in the list of locales included with the XPI. This will prevent
300+
// us from accidentally registering a L10nFileSource that wasn't included.
301+
this._supportedLocales = new Set(
302+
await fetch(rootURI.resolve("/locales/supported-locales.json")).then(
303+
r => r.json()
304+
)
302305
);
303-
this._l10nFileSource = newtabFileSource;
304-
L10nRegistry.getInstance().registerSources([newtabFileSource]);
306+
307+
// Set up observers so that if the user changes the list of available
308+
// locales, we'll re-register.
309+
Services.obs.addObserver(this, TOPIC_LOCALES_CHANGED);
310+
Services.obs.addObserver(this, TOPIC_SHUTDOWN);
311+
// Now actually do the registration.
312+
this._updateFluentSourcesRegistration();
305313
} catch (e) {
306314
// TODO: consider if we should collect this in telemetry.
307315
this.logger.error(
@@ -311,6 +319,53 @@ export var AboutNewTabResourceMapping = {
311319
}
312320
},
313321

322+
/**
323+
* Sets up the L10nFileSource for the newtab Fluent files included in the
324+
* XPI that are in the available locales for the app. If a pre-existing
325+
* registration exists, it will be updated.
326+
*/
327+
_updateFluentSourcesRegistration() {
328+
let availableLocales = new Set(Services.locale.availableLocales);
329+
let availableSupportedLocales =
330+
this._supportedLocales.intersection(availableLocales);
331+
332+
const newtabFileSource = new L10nFileSource(
333+
FLUENT_SOURCE_NAME,
334+
"app",
335+
[...availableSupportedLocales],
336+
`resource://newtab/locales/{locale}/`
337+
);
338+
339+
let registry = L10nRegistry.getInstance();
340+
if (registry.hasSource(FLUENT_SOURCE_NAME)) {
341+
registry.updateSources([newtabFileSource]);
342+
this.logger.debug(
343+
"Newtab strings updated for ",
344+
availableSupportedLocales
345+
);
346+
} else {
347+
registry.registerSources([newtabFileSource]);
348+
this.logger.debug(
349+
"Newtab strings registered for ",
350+
availableSupportedLocales
351+
);
352+
}
353+
},
354+
355+
observe(_subject, topic, _data) {
356+
switch (topic) {
357+
case TOPIC_LOCALES_CHANGED: {
358+
this._updateFluentSourcesRegistration();
359+
break;
360+
}
361+
case TOPIC_SHUTDOWN: {
362+
Services.obs.removeObserver(this, TOPIC_LOCALES_CHANGED);
363+
Services.obs.removeObserver(this, TOPIC_SHUTDOWN);
364+
break;
365+
}
366+
}
367+
},
368+
314369
/**
315370
* Registers any dynamic Glean metrics that have been included with the XPI
316371
* version of the add-on.

0 commit comments

Comments
 (0)