Difference between revisions of "MediaWiki:LanguageHandler.js"
Jump to navigation
Jump to search
Line 13: | Line 13: | ||
languages : new Array ( | languages : new Array ( | ||
// Sorted by language name, not language code! Except for strange scripts... | // Sorted by language name, not language code! Except for strange scripts... | ||
− | + | {text : 'English', lang : 'en'}, | |
+ | {text : 'Español', lang : 'es'} | ||
), | ), | ||
Latest revision as of 10:40, 9 June 2011
// <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 : 'English', lang : 'en'}, {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>