Last active
August 29, 2015 14:24
-
-
Save zbraniecki/edcb6ed502c9ab4508ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); | |
}); | |
} | |
} | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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; | |
} | |
}) | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 thedocument.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 aslet currentLocale = langs[0];
... can be replaced by:
Also, you probably don't need to iterate twice with
Promise.all
here:Since
response.text()
andresponse.json()
are also promises, you can achieve the same thing with a singlePromise.all
: