Quoting Axel Hecht (2014-09-22 22:56:39)
> That's a lot of data, but I'll cherry pick one.
> 
> Looking at coding patterns in js land, making navigator.mozL10n.ctx 
> immutable doesn't feel right. It's just to popular to short-hand that, 
> to the extent that there'll be a
> 
> var _ = navigator.mozL10n.ctx.get.bind(navigator.mozL10n.ctx)
> 
> Breaking that code pattern wouldseem to be sad-facing.

I've come to believe that this is one of the long-standing patterns 
that we'll need to break in order to take the API to the next level.

In the async world, having that shortcut around is not very useful 
anymore.  In the vast majority of cases, you should not be using the 
get() method at all.  Instead, just assign data-l10n-id.  Consider the 
following code:

    
https://github.com/mozilla-b2g/gaia/blob/f911b47f1acfb8e229851d443692115caf8369e0/apps/music/js/music.js#L46-L59
    
https://github.com/mozilla-b2g/gaia/blob/f911b47f1acfb8e229851d443692115caf8369e0/apps/ftu/js/navigation.js#L175-L299

Both of these files make extensive use of the get() method with 
a single goal of assigning the value to textContent or innerHTML.  We 
want to phase out such use completely.

Next up, we have cases where get() is used deeper in the abstraction 
layer and then used by other code.  Consider this example:

    
https://github.com/mozilla-b2g/gaia/blob/f911b47f1acfb8e229851d443692115caf8369e0/apps/communications/contacts/js/tag_options.js

This should probably define "l10nIds" for later resolution instead of 
"values" which are resolved when this code is run.  Both Gandalf and 
I have been advocating for using l10n ids as much as possible and only 
resolving them to actual translations at the last possible moment (i.e.  
never, just set the data-l10n-id attribute :).

Last but not least, in some rare cases you actually might need to do 
a few consecutive get() calls.  I think the following code illustrates 
this well:

    
https://github.com/mozilla-b2g/gaia/blob/f911b47f1acfb8e229851d443692115caf8369e0/apps/communications/dialer/js/mmi.js#L137-L145

Here we are definining a set of known possible messages from the 
carrier which will be shown to the user once.  The translations live in 
the Dialer app and they are transmitted via window.postMessage to 
somewhere else (which coincidentally already moves us into the async 
world, so we're good!).

With an async get() method, it's easy to achieve the same result with 
Promise.all, which refers to the get() method only once:

    var l10nIds = [
      ['call-forwarding-status'],
      ['call-forwarding-voice', { voice: voice || inactive }],
      ['call-forwarding-data', { data: data || inactive }],
      …
    ];

    return Promise.all(
      l10nIds.map(
        Function.prototype.apply.bind(
          navigator.mozL10n.get, navigator.mozL10n)));

-stas


-- 
@stas
_______________________________________________
tools-l10n mailing list
[email protected]
https://lists.mozilla.org/listinfo/tools-l10n

Reply via email to