Fix html10n (finally)

pull/1297/head
Marcel Klehr 2012-12-19 16:29:38 +01:00
parent 67c893aaca
commit 64b176f4de
1 changed files with 24 additions and 18 deletions

View File

@ -79,10 +79,10 @@ window.html10n = (function(window, document, undefined) {
for (var i=0, n=this.resources.length; i < n; i++) {
this.fetch(this.resources[i], lang, function(e) {
reqs++;
if(e) setTimeout(function(){ throw e }, 0)
if(e) return setTimeout(function(){ throw e }, 0)
if (reqs < n) return;// Call back once all reqs are completed
cb()
cb && cb()
})
}
}
@ -108,7 +108,7 @@ window.html10n = (function(window, document, undefined) {
var data = JSON.parse(xhr.responseText)
that.cache[href] = data
// Pass on the contents for parsing
this.parse(lang, data, cb)
that.parse(lang, data, cb)
} else {
cb(new Error('Failed to load '+href))
}
@ -619,18 +619,18 @@ window.html10n = (function(window, document, undefined) {
var children = element? getTranslatableChildren(element) : document.childNodes;
for (var i=0, n=children.length; i < n; i++) {
translateNode(translations, children[i])
this.translateNode(translations, children[i])
}
// translate element itself if necessary
translateNode(translations, element)
this.translateNode(translations, element)
}
function asyncForEach(list, iterator, cb) {
function asyncForEach(list, iterator, cb) {
var i = 0
, n = list.length
iterator(list[i], i, function each(err) {
consoleLog(err)
if(err) consoleLog(err)
i++
if (i < n) return iterator(list[i],i, each);
cb()
@ -656,10 +656,10 @@ window.html10n = (function(window, document, undefined) {
if(!translations[id]) return consoleWarn('Could not find string '+id)
// apply args
str = substArguments(translations[id], args)
var str = substArguments(translations[id], args)
// apply macros
return substMacros(translations, id, str, args)
return substMacros(id, str, args)
// replace {{arguments}} with their values or the
// associated translation string (based on its key)
@ -759,7 +759,7 @@ window.html10n = (function(window, document, undefined) {
if (node.children.length === 0) {
node[prop] = str.str
} else {
var children = element.childNodes,
var children = node.childNodes,
found = false
for (var i=0, n=children.length; i < n; i++) {
if (children[i].nodeType === 3 && /\S/.test(children[i].textContent)) {
@ -772,7 +772,7 @@ window.html10n = (function(window, document, undefined) {
}
}
if (!found) {
consoleWarn('Unexpected error: could not translate element content')
consoleWarn('Unexpected error: could not translate element content for key '+str.id, node)
}
}
}
@ -786,20 +786,26 @@ window.html10n = (function(window, document, undefined) {
, build = {}
asyncForEach(langs, function (lang, i, next) {
html10n.loader.load(lang, next)
if(!lang) return next();
that.loader.load(lang, next)
}, function() {
var lang
langs.reverse()
// loop through priority array...
for (var i=0, n=langs.length; i < n; i++) {
// apply all strings of the current lang in the list
lang = langs[i]
if(!lang) continue;
// ... and apply all strings of the current lang in the list
// to our build object
for (var string in this.loader.langs[i]) {
build[string] = this.loader.langs[lang][string]
for (var string in that.loader.langs[lang]) {
build[string] = that.loader.langs[lang][string]
}
// the last applied lang will be exposed as the
// lang the page was translated to
that.language = langs[i]
that.language = langs[lang]
}
cb(null, build)
})
@ -840,7 +846,7 @@ window.html10n = (function(window, document, undefined) {
// gettext-like shortcut
if (window._ === undefined)
var _ = html10n.get;
window._ = html10n.get;
return html10n
})(window, document)