Skip to content

Instantly share code, notes, and snippets.

@zbraniecki
Last active August 29, 2015 14:24
Show Gist options
  • Save zbraniecki/edcb6ed502c9ab4508ea to your computer and use it in GitHub Desktop.
Save zbraniecki/edcb6ed502c9ab4508ea to your computer and use it in GitHub Desktop.
(function() {
const observerConfig = {
attributes: false,
characterData: false,
childList: true,
subtree: false,
};
var observer = new MutationObserver(onMutations);
observer.observe(document.head, observerConfig);
function onMutations(mutations) {
for (let mutation of mutations) {
for (let addedNode of mutation.addedNodes) {
if (addedNode.nodeType === Node.ELEMENT_NODE) {
onAddedHeadElement(addedNode);
}
}
}
}
function onAddedHeadElement(element) {
if (element.nodeName === 'LINK' &&
element.getAttribute('rel') === 'localization') {
console.log('element ready set');
element.ready = document.l10n.languages.then(langs => {
let currentLocale = [...langs][0];
let href = element.getAttribute('href');
href = href.replace('{locale}', currentLocale);
return fetch(href);
});
}
}
})();
let promises = [];
let l10nResLinks = document.head.querySelectorAll('link[rel=localization]');
for (link of l10nResLinks) {
console.log(link.ready);
promises.push(link.ready);
};
Promise.all(promises).then(function (resources) {
return Promise.all(resources.map(function(response) {
if (!response.ok) {
console.log('error fetching ' + response.url);
return;
}
if (response.url.endsWith('.json')) {
return response.json();
} else {
return response.text();
}
}).filter(n => n !== undefined));
}).then(function(responses) {
console.log(responses);
});
(function() {
const observerConfig = {
attributes: false,
characterData: false,
childList: true,
subtree: false,
};
var observer = new MutationObserver(onMutations);
observer.observe(document.head, observerConfig);
let l10nMeta = new Map();
let localeChain = new Set();
let languagesResolve;
function onMutations(mutations) {
for (let mutation of mutations) {
for (let addedNode of mutation.addedNodes) {
if (addedNode.nodeType === Node.ELEMENT_NODE) {
onAddedHeadElement(addedNode);
}
}
}
}
function onAddedHeadElement(element) {
if (element.nodeName === 'META') {
let name = element.getAttribute('name');
if (name === 'defaultLanguage' || name === 'availableLanguages') {
l10nMeta.set(name, element.getAttribute('content'));
if (l10nMeta.size === 2) {
observer.disconnect();
negotiateLocales();
}
}
}
}
function negotiateLocales() {
localeChain.add('en-US');
localeChain.add('fr');
languagesResolve(localeChain);
}
document.l10n = {
languages: new Promise(function(resolve, reject) {
if (localeChain.size) {
resolve(localeChain);
} else {
languagesResolve = resolve;
}
})
};
})();
@stasm
Copy link

stasm commented Jul 28, 2015

One general comment would be to figure out when we expect document.l10n.languages to be available. Maybe the shims need to be required in a specific order and the first one creates the document.l10n collection for the other shims to use?

Also a few coding comments which you may find useful:

let currentLocale = [...langs][0]; can also be written as let currentLocale = langs[0];

for (link of l10nResLinks) {
  console.log(link.ready);
  promises.push(link.ready);
};

... can be replaced by:

const promises = Array.prototype.map.call(l10nResLinks, link => link.ready);

Also, you probably don't need to iterate twice with Promise.all here:

Promise.all(promises).then(function (resources) {
  return Promise.all(resources.map(function(response) {

Since response.text() and response.json() are also promises, you can achieve the same thing with a single Promise.all:

Promise.all(
  Array.prototype.map.call(l10nResLinks,
    link => doSomethingWithResponse(link.ready)))).then(..filter..);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment