Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update html10n.js #1841

Merged
merged 2 commits into from Jul 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/locales/en.json
Expand Up @@ -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",
Expand Down
6 changes: 1 addition & 5 deletions src/static/js/broadcast_slider.js
Expand Up @@ -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);
Expand Down
147 changes: 84 additions & 63 deletions src/static/js/html10n.js
Expand Up @@ -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";
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 macros
var str = translations[id]

str = substMacros(id, str, args)

// apply args
var str = substArguments(translations[id], args)
str = substArguments(str, args)

// apply macros
return substMacros(id, 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

// 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)
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
}
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
}

/**
Expand Down