User:Mike Dillon/Scripts/i18n.js

/* */

var wgMessageCache = {};

var wgFallbackLanguage;

function wfAddMsg(lang, key, value) { if (!wgMessageCache[lang]) { wgMessageCache[lang] = {}; }   wgMessageCache[lang][key.toLowerCase] = value; }

function wfMsgReal(key, forContent, args) { function wfMsgGet(lang, key) { if (!wgMessageCache[lang]) return null; return wgMessageCache[lang][key.toLowerCase]; }

// Bail out of the key is null if (key == null) return null;

var msg = null;

// Look for message in user language if (!forContent) { msg = wfMsgGet(wgUserLanguage, key); }

// Else, use content language if (msg == null) { msg = wfMsgGet(wgContentLanguage, key); }

// Else, use fallback language if (msg == null && wgFallbackLanguage) { msg = wfMsgGet(wgFallbackLanguage, key); }

// Else, use fallback text if (msg == null) { msg = "<" + key.toLowerCase + ">"; }

// Replace positional placeholders (e.g. $1 or ${1}) if (args.length > 1) { var re = /\$(?:([1-9]\d*)|\{([1-9]\d*)\})/g;

var m;       var toReplace = []; while (m = re.exec(msg)) { toReplace.push({               'pos': re.lastIndex - m[0].length,                'len': m[0].length,                'arg': m[1] || m[2]            }); }

var adjust = 0; for (var n in toReplace) { var replacement = args[toReplace[n].arg]; if (replacement == null) continue; replacement = replacement.toString;

toReplace[n].pos += adjust;

msg = msg.substring(0, toReplace[n].pos) + replacement + msg.substring(toReplace[n].pos + toReplace[n].len);

adjust += replacement.length - toReplace[n].len; }   }

return msg; }

function wfMsg(key) { return wfMsgReal(key, false, arguments); }

function wfMsgForContent(key) { return wfMsgReal(key, true, arguments); }

/* */