Merge pull request #1841 from ether/update/html10n.js

Update html10n.js
This commit is contained in:
John McLear 2013-07-23 03:52:40 -07:00
commit 78820b8572
3 changed files with 86 additions and 70 deletions

View File

@ -96,8 +96,7 @@
"timeslider.month.october": "October",
"timeslider.month.november": "November",
"timeslider.month.december": "December",
"timeslider.unnamedauthor": "{{num}} unnamed author",
"timeslider.unnamedauthors": "{{num}} unnamed authors",
"timeslider.unnamedauthors": "{{num}} unnamed {[plural(num) one: author, other: authors ]}",
"pad.savedrevs.marked": "This revision is now marked as a saved revision",
"pad.userlist.entername": "Enter your name",
"pad.userlist.unnamed": "unnamed",

View File

@ -198,11 +198,7 @@ function loadBroadcastSliderJS(fireWhenAllScriptsAreLoaded)
});
if (numAnonymous > 0)
{
var anonymousAuthorString;
if(numAnonymous == 1)
anonymousAuthorString = html10n.get("timeslider.unnamedauthor", { num: numAnonymous });
else
anonymousAuthorString = html10n.get("timeslider.unnamedauthors", { num: numAnonymous });
var anonymousAuthorString = html10n.get("timeslider.unnamedauthors", { num: numAnonymous });
if (numNamed !== 0){
authorsList.append(' + ' + anonymousAuthorString);

View File

@ -46,7 +46,20 @@ window.html10n = (function(window, document, undefined) {
, 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) {
Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
"use strict";
@ -79,15 +92,6 @@ window.html10n = (function(window, document, undefined) {
return -1;
}
}
// 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)
@ -643,25 +647,26 @@ window.html10n = (function(window, document, undefined) {
/**
* pre-defined 'plural' macro
*/
html10n.macros.plural = function(translations, key, str, param) {
var n = parseFloat(param);
html10n.macros.plural = function(key, param, opts) {
var str
, n = parseFloat(param);
if (isNaN(n))
return str;
return;
// initialize _pluralRules
if (!this._pluralRules)
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
if (n === 0 && (key + '[zero]') in translations) {
str = translations[key + '[zero]'];
} else if (n == 1 && (key + '[one]') in translations) {
str = translations[key + '[one]'];
} else if (n == 2 && (key + '[two]') in translations) {
str = translations[key + '[two]'];
} else if ((key + index) in translations) {
str = translations[key + index][prop];
if (n === 0 && ('zero') in opts) {
str = opts['zero'];
} else if (n == 1 && ('one') in opts) {
str = opts['one'];
} else if (n == 2 && ('two') in opts) {
str = opts['two'];
} else if (index in opts) {
str = opts[index];
}
return str;
@ -739,65 +744,81 @@ window.html10n = (function(window, document, undefined) {
if(!translations) return consoleWarn('No translations available (yet)')
if(!translations[id]) return consoleWarn('Could not find string '+id)
// apply args
var str = substArguments(translations[id], args)
// apply macros
return substMacros(id, str, args)
var str = translations[id]
// replace {{arguments}} with their values or the
// associated translation string (based on its key)
function substArguments(str, args) {
var reArgs = /\{\{\s*([a-zA-Z\.]+)\s*\}\}/
, match
while (match = reArgs.exec(str)) {
if (!match || match.length < 2)
return str // argument key not found
str = substMacros(id, str, args)
// apply args
str = substArguments(str, args)
return str
}
// replace {{arguments}} with their values or the
// associated translation string (based on its key)
function substArguments(str, args) {
var reArgs = /\{\{\s*([a-zA-Z\.]+)\s*\}\}/
, match
while (match = reArgs.exec(str)) {
if (!match || match.length < 2)
return str // argument key not found
var arg = match[1]
, sub = ''
if (arg in args) {
sub = args[arg]
} else if (arg in translations) {
sub = translations[arg]
} else {
consoleWarn('Could not find argument {{' + arg + '}}')
return str
}
str = str.substring(0, match.index) + sub + str.substr(match.index + match[0].length)
var arg = match[1]
, sub = ''
if (arg in args) {
sub = args[arg]
} else if (arg in translations) {
sub = translations[arg]
} else {
consoleWarn('Could not find argument {{' + arg + '}}')
return str
}
return str
str = str.substring(0, match.index) + sub + str.substr(match.index + match[0].length)
}
// replace {[macros]} with their values
function substMacros(key, str, args) {
var regex = /\{\[\s*([a-zA-Z]+):([a-zA-Z]+)\s*\]\}/
, match = regex.exec(str);
if (!match || !match.length)
return str;
return str
}
// replace {[macros]} with their values
function substMacros(key, str, args) {
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
while(match = regex.exec(str)) {
// a macro has been found
// Note: at the moment, only one parameter is supported
var macroName = reMatch[1]
, paramName = reMatch[2]
var macroName = match[1]
, 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
if (args && paramName in args) {
param = args[paramName]
} else if (paramName in translations) {
} else if (paramName in html10n.translations) {
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]
str = macro(translations, key, str, param)
return str
str = str.substr(0, match.index) + macro(key, param, opts) + str.substr(match.index+match[0].length)
}
return str
}
/**