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