Update html10n.js

pull/1841/head
Marcel Klehr 2013-07-23 12:42:55 +02:00
parent 100c4627ec
commit dabba91152
1 changed files with 84 additions and 63 deletions

View File

@ -46,7 +46,20 @@ window.html10n = (function(window, document, undefined) {
, consoleError = interceptConsole('warn') , consoleError = interceptConsole('warn')
// fix Array.prototype.instanceOf in, guess what, IE! <3 // fix Array#forEach in IE
// taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(fn, scope) {
for(var i = 0, len = this.length; i < len; ++i) {
if (i in this) {
fn.call(scope, this[i], i, this);
}
}
};
}
// fix Array#indexOf in, guess what, IE! <3
// taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
if (!Array.prototype.indexOf) { if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) { Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
"use strict"; "use strict";
@ -80,15 +93,6 @@ window.html10n = (function(window, document, undefined) {
} }
} }
// fix Array.prototype.forEach in IE
if (!('forEach' in Array.prototype)) {
Array.prototype.forEach= function(action, that /*opt*/) {
for (var i= 0, n= this.length; i<n; i++)
if (i in this)
action.call(that, this[i], i, this);
};
}
/** /**
* MicroEvent - to make any js object an event emitter (server or browser) * MicroEvent - to make any js object an event emitter (server or browser)
*/ */
@ -643,25 +647,26 @@ window.html10n = (function(window, document, undefined) {
/** /**
* pre-defined 'plural' macro * pre-defined 'plural' macro
*/ */
html10n.macros.plural = function(translations, key, str, param) { html10n.macros.plural = function(key, param, opts) {
var n = parseFloat(param); var str
, n = parseFloat(param);
if (isNaN(n)) if (isNaN(n))
return str; return;
// initialize _pluralRules // initialize _pluralRules
if (!this._pluralRules) if (!this._pluralRules)
this._pluralRules = getPluralRules(html10n.language); this._pluralRules = getPluralRules(html10n.language);
var index = '[' + this._pluralRules(n) + ']'; var index = this._pluralRules(n);
// try to find a [zero|one|two] key if it's defined // try to find a [zero|one|two] key if it's defined
if (n === 0 && (key + '[zero]') in translations) { if (n === 0 && ('zero') in opts) {
str = translations[key + '[zero]']; str = opts['zero'];
} else if (n == 1 && (key + '[one]') in translations) { } else if (n == 1 && ('one') in opts) {
str = translations[key + '[one]']; str = opts['one'];
} else if (n == 2 && (key + '[two]') in translations) { } else if (n == 2 && ('two') in opts) {
str = translations[key + '[two]']; str = opts['two'];
} else if ((key + index) in translations) { } else if (index in opts) {
str = translations[key + index][prop]; str = opts[index];
} }
return str; return str;
@ -739,11 +744,16 @@ window.html10n = (function(window, document, undefined) {
if(!translations) return consoleWarn('No translations available (yet)') if(!translations) return consoleWarn('No translations available (yet)')
if(!translations[id]) return consoleWarn('Could not find string '+id) if(!translations[id]) return consoleWarn('Could not find string '+id)
// apply args
var str = substArguments(translations[id], args)
// apply macros // apply macros
return substMacros(id, str, args) var str = translations[id]
str = substMacros(id, str, args)
// apply args
str = substArguments(str, args)
return str
}
// 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)
@ -774,30 +784,41 @@ window.html10n = (function(window, document, undefined) {
// replace {[macros]} with their values // replace {[macros]} with their values
function substMacros(key, str, args) { function substMacros(key, str, args) {
var regex = /\{\[\s*([a-zA-Z]+):([a-zA-Z]+)\s*\]\}/ var regex = /\{\[\s*([a-zA-Z]+)\(([a-zA-Z]+)\)((\s*([a-zA-Z]+)\: ?([ a-zA-Z{}]+),?)+)*\s*\]\}/ //.exec('{[ plural(n) other: are {{n}}, one: is ]}')
, match = regex.exec(str); , match
if (!match || !match.length)
return str;
while(match = regex.exec(str)) {
// a macro has been found // a macro has been found
// Note: at the moment, only one parameter is supported // Note: at the moment, only one parameter is supported
var macroName = reMatch[1] var macroName = match[1]
, paramName = reMatch[2] , paramName = match[2]
, optv = match[3]
, opts = {}
if (!(macroName in gMacros)) return str if (!(macroName in html10n.macros)) continue
if(optv) {
optv.match(/(?=\s*)([a-zA-Z]+)\: ?([ a-zA-Z{}]+)(?=,?)/g).forEach(function(arg) {
var parts = arg.split(':')
, name = parts[0]
, value = parts[1].trim()
opts[name] = value
})
}
var param var param
if (args && paramName in args) { if (args && paramName in args) {
param = args[paramName] param = args[paramName]
} else if (paramName in translations) { } else if (paramName in html10n.translations) {
param = translations[paramName] param = translations[paramName]
} }
// there's no macro parser yet: it has to be defined in gMacros // there's no macro parser: it has to be defined in html10n.macros
var macro = html10n.macros[macroName] var macro = html10n.macros[macroName]
str = macro(translations, key, str, param) str = str.substr(0, match.index) + macro(key, param, opts) + str.substr(match.index+match[0].length)
return str
} }
return str
} }
/** /**