User:Mike Dillon/Scripts/i18n.js
From Wikipedia, the free encyclopedia
If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
/* <pre><nowiki> */ 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); } /* </nowiki></pre> */