Expose timing of localization completion




6 months ago
4 months ago


(Reporter: timdream, Unassigned)


Firefox Tracking Flags

(Not tracked)


There are at least two use cases (bug 1518786 and bug 1520350) where we would like to measure the size of the newly inserted DOM. Yet the size of the content would depend on its localization string. It would be great if there is a promise or something that resolves when the newly inserted element (or better, the newly inserted DOM with the entire subtree included) is localized (and with their layout ready).

Priority: -- → P3

This will likely have to wait for the C++ migration because doing it in JS would be kind of quirky.

For now, the workaround for cases where you need to ensure l10n is completed is:

await document.l10n.translateElements([elem1, elem2]);
// the translation has been applied

Since bug 1518786 landed, in such case any previous translation will be removed from pendingElements.

In the locale-switching scenario, would we want to get a notification again? I.e., should this rather be an event than a one-off promise?

The two use cases are about measuring the dimensions of the newly inserted DOM. They don't need a notification again.

That said, I can see the possibility where caller gets confused. I wonder if this can be prevented with better naming.

In cases we encountered so far, the user wants to wait with some layout/sizing/painting work until translation is displayed.

That would indicate that they need a one-time promise, not an event, but I may be wrong. Event would be easier, but also more costly to handle:

await document.l10n.setAttributesAndWait(elem1, id);


let promise = new Promise((resolve) => {
  document.addEventListener("pending-l10n-applied", resolve, {once: true});
document.l10n.setAttributes(elem1, id);
await promise;

also, this can still make us miss if the requestAnimationFrame is scheduled right as this code gets executed, but the attribute changed get collected by MutationObserver only after the current rAF gets fired, which means that this element gets added to pendingElements and localized only in the next pending-l10n-applied :/

See Also: → 1532712
You need to log in before you can comment on or make changes to this bug.