Closed Bug 1315811 Opened 8 years ago Closed 8 years ago

Add ability to detect when DOM nodes are localized

Categories

(L20n :: JS Library, defect)

defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: mathjazz, Assigned: stas)

References

Details

Attachments

(1 file)

40 bytes, text/x-github-pull-request
Pike
: review+
Details | Review
https://github.com/l20n/l20n.js/blob/master/docs/html.md#the-javascript-api says:
"The first time all DOM nodes are localized, the document.l10n.ready promise will resolve."

It turns out no such promise exsists.
I remember you had an opinion on this :)
Flags: needinfo?(stas)
First of all, sorry about the inaccurate docs.  The reason why that promise doesn't currently exist requires some explanation.

There might be many Localization objects in a single document and we don't know how many there are up-front (XBL or web components might add more).  For this reason we can't have a single global promise resolving when all Localizations finish doing their thing — we don't know what "all" means.

It is still possible to check the 'main' Localization status with document.l10n.get('main').interactive but that resolves as soon as the first language has loaded and not when the DOM has been translated into it. Also, the API for this could be nicer.

Good news is, I'm currently almost done with a refactor of document.l10n (bug 1316083).  After it lands, document.l10n *will be* the main Localization object, so the equivalent of the example above will be document.l10n.interactive.then(..) and it should be easier now to add document.l10n.ready with the semantics described in comment 0.
Flags: needinfo?(stas)
Attached file Pull request
Here's a very straight-forward way of doing this.  It's a bit manual.  An alternative would be to pass a runtime-specific function to the constructor and  set this.ready to the return value of the function.

document.l10n = new DocumentLocalization(requestBundles, createContext, function() {
  this.ready = documentReady().then(() => { 
    this.connectRoot(document.documentElement);
    this.translateDocument();
    window.addEventListener('languagechange', this);
});

Would you prefer that?
Assignee: nobody → stas
Status: NEW → ASSIGNED
Attachment #8813346 - Flags: review?(l10n)
Comment on attachment 8813346 [details] [review]
Pull request

We need to return the promises from translateDocument so that we only resolve .ready once all language fallbacks are done.

There's an interesting race condition, should we start observing language change right away, or after the last fallback?

I think it'd be good to do it right away to make sure we're not leaving old languages behind. But should we cancel fallbacks if we're seeing a language change in the middle? Or does that not matter?
Attachment #8813346 - Flags: review?(l10n) → review-
(In reply to Axel Hecht [:Pike] from comment #4)
> Comment on attachment 8813346 [details] [review]
> Pull request
> 
> We need to return the promises from translateDocument so that we only
> resolve .ready once all language fallbacks are done.

My bad, thanks for catching this.

> There's an interesting race condition, should we start observing language
> change right away, or after the last fallback?
> 
> I think it'd be good to do it right away to make sure we're not leaving old
> languages behind. But should we cancel fallbacks if we're seeing a language
> change in the middle? Or does that not matter?

We don't have means right now to cancel fallbacks.  As soon as we call translateDocument(), it will localize all element using the full array of language bundles from the initial requestBundles.  If a language changes before this initial translateDocument finishes and we intercept it, it will start a new translation using the updated array of bundles.  In the worst case scenario (with many fallbacks in both cases), these two translation runs might alternate and step on each other's shoes.

I think we should start listening for languagechange only when ready() resolves in order to avoid even a remote risk of non-deterministic behavior.  I assume it's rather rare for the language to change at all anyways.
Attachment #8813346 - Flags: review- → review?(l10n)
Comment on attachment 8813346 [details] [review]
Pull request

r=me.

I think we want a follow-up bug to avoid being deaf for language changes while we translate the first time around. And in that bug figure out how to block concurrent language switch requests.
Attachment #8813346 - Flags: review?(l10n) → review+
(In reply to Axel Hecht [:Pike] from comment #6)

> I think we want a follow-up bug to avoid being deaf for language changes
> while we translate the first time around. And in that bug figure out how to
> block concurrent language switch requests.

Filed bug 1319797.  Marking this bug as fixed.
Status: ASSIGNED → RESOLVED
Closed: 8 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: