MediaWiki:LanguageHandler.js
Jump to navigation
Jump to search
Note: After saving, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
// <source lang="javascript"> /* Some basic language support, use by the upload form rewrite. Author: [[User:Lupo]], January 2008 License: Quadruple licensed GFDL, GPL, LGPL and Creative Commons Attribution 3.0 (CC-BY-3.0) Choose whichever license of these you like best :-) */ var LanguageHandler = { languages : new Array ( // Sorted by language name, not language code! Except for strange scripts... {text : 'Español', lang : 'es'}, ), getSelect : function (id, preSelect, unknownLabel) { var select = document.createElement ('select'); if (id != null) { select.setAttribute ('id', id); select.setAttribute ('name', id); } // Must have a text node. IE6 doesn't know Node.TEXT_NODE... while (unknownLabel && unknownLabel.nodeType != 3) unknownLabel = unknownLabel.firstChild; if (unknownLabel) { var unknown_option = document.createElement ('option'); unknown_option.setAttribute ('value', 'unknown'); unknown_option.appendChild ( document.createTextNode (unknownLabel.data.replace (/^\s+/, "").replace (/\s+$/, ""))); if (preSelect == null) unknown_option.setAttribute ('selected', 'selected'); select.appendChild (unknown_option); } for (var i = 0; i < LanguageHandler.languages.length; i++) { var option = document.createElement ('option'); option.setAttribute ('value', LanguageHandler.languages[i].lang); if (LanguageHandler.languages[i].lang == preSelect) option.setAttribute ('selected', 'selected'); option.appendChild (document.createTextNode (LanguageHandler.languages[i].text)); select.appendChild (option); } return select; }, closestIndex : function (langCode) { if (typeof (langCode) != 'string' || langCode == null || langCode.length == 0) return -1; // Check if we have it. if (langCode == 'nan' || langCode == 'minnan') langCode = 'zh-min-nan'; for (var i = 0; i < LanguageHandler.languages.length; i++) { if (LanguageHandler.languages[i].lang == langCode) return i; } // Check for prefix var dash = langCode.indexOf ('-'); if (dash >= 0) return LanguageHandler.closestIndex (langCode.substring (0, dash)); return -1; }, closestLanguage : function (langCode) { var idx = LanguageHandler.closestIndex (langCode); if (idx < 0) return null; return LanguageHandler.languages[idx].lang; }, getPrefix : function (langStr) { // Given a string, check if any prefix substring matches one of the language codes. If so, return // the length of the longest matching language code, otherwise, return zero. // Note: if languageHandler.languages were sorted by length of language code, this could be sped up // by stopping on the first match. But the benefits of human-readability of the list in alphabetical // order outweigh this performance consideration. var match_length = 0; for (var i = 0; i < LanguageHandler.languages.length; i++) { var l = LanguageHandler.languages[i].lang.length; if ( l <= langStr.length && l > match_length && langStr.substr (0, l) == LanguageHandler.languages[i].lang) match_length = l; } return match_length; } } // end LanguageHandler // </source>