Google Groups

Re: [jetpack] HTML page localization proposal


Alexandre poirot Mar 20, 2012 11:02 AM
Posted in group: mozilla-labs-jetpack
Hey guys,

I finally jumped into this feature. And I come with some concrete experience about how we could implement this.

== network layer implementation ==
I first tried to hack around protocols and channels, in order to be able to operate in the network layer.
But I faced bug 736046, that prevents us for implement this feature today. A patch is submitted and waiting for review,
but at the very least, it will slow down when we are going to be able to use this new feature. (i.e. not until FF 14)
We might be able to workaround it by writting localized HTML files on disk, but we will have to do it synchronously
on the main thread, or, ship localized files in the xpi. Both options are quite bad. The first one may introduce some hangs in FF UI
and the second one will increase XPI size and complexify locales updates.

== DOM/dynamic/gaia approach ==
At the end, I'm not really confident that hacking in network layer is a good idea.
It may introduce some slowdown. Reading a file from JS will most likely be slower than letting C++ HTML parser (or whatever c++ component) read it by itself.
As template base solution depends on using such practice, I started looking at a DOM based solution.
Especially the one already implemented in Gaia.
They basically wait for DOMContentLoaded event to fire and do a querySelector matching all nodes with a 'data-l10n-id' attribute.
Then textContent of these nodes is replaced with translated strings.
Here is an example:
# index.html
... <span data-l10n-id="foo">Hello</span> ...
# fr-FR.properties
foo = Bonjour

And I was really surprised how simple it is to implement!
(https://github.com/ochameau/addon-sdk/blob/c9549010b44ee0da122b89fbe51333833d3dd1c0/packages/api-utils/lib/l10n/html.js)

== Blink effect ==
Then, you may face some blinking content switching from original text to translated one,
but it seems that in most cases you won't ever see such effect.
I gave a try on Collusion addon, and except if you keep the F5 key down, you will most likely never see strings being updated!
Here is two versions of Collusion addon, with same localization algorithm than Gaia, but with fake values.
All strings will be translated to "Translated value".
1/ This first version use a CSS trick in order to hide the document until we finished the translation
http://dl.free.fr/mvdUBrcjc
2/ A alternative version without such trick.
http://dl.free.fr/qbtj2GuMw
You will quickly see that it is quite hard to see strings being updated.
And even if we want to be sure that it never happen, we can hide the document until it is translated.


So if we are confortable with this approach, I'd like to update wiki page, open a related bug
and discuss about particular choices we can make around this approach.
The good points are:
- I think it will end up being more efficient,
- We do not hit any platform issue that would prevent us from using this feature in current firefox releases,
- Closer to l20n, gaia and the DOM.


++
Alex