MediaWiki:ReferrerWikiUselang.js: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
added language list, better to cache it in the script then issue another request to the api |
expanded persistent language support to all mediawiki languages ( revert if it causes issues ) |
||
Line 10: | Line 10: | ||
// The supported list of projects that include language codes at the start of their url |
// The supported list of projects that include language codes at the start of their url |
||
'projectList' : [ 'wikipedia' , 'wiktionary' , 'wikinews' , 'wikibooks' , 'wikisource' , 'wikiversity' , 'wikiquote' ], |
'projectList' : [ 'wikipedia' , 'wiktionary' , 'wikinews' , 'wikibooks' , 'wikisource' , 'wikiversity' , 'wikiquote' ], |
||
// The supported list of language codes |
|||
'rewriteLanguageList' : ['nl', 'fr'], |
|||
/* |
|||
all wikimedia supported languages: This can also be grabbed via the api: |
|||
⚫ | |||
Once tested, we can replace rewriteLanguageList with this list: |
|||
// The supported list of language codes, We list out the languages here to avoid an extra api request. |
|||
⚫ | |||
// Api request to get this list: |
|||
⚫ | |||
⚫ | |||
"arn", "arz", "as", "ast", "av", "avk", "ay", "az", "ba", "bar", "bat-smg", "bcc", "bcl", "be", |
"arn", "arz", "as", "ast", "av", "avk", "ay", "az", "ba", "bar", "bat-smg", "bcc", "bcl", "be", |
||
"be-tarask", "be-x-old", "bg", "bh", "bi", "bm", "bn", "bo", "bpy", "bqi", "br", "bs", "bug", "bxr", |
"be-tarask", "be-x-old", "bg", "bh", "bi", "bm", "bn", "bo", "bpy", "bqi", "br", "bs", "bug", "bxr", |
||
Line 44: | Line 42: | ||
"vo", "vro", "wa", "war", "wo", "wuu", "xal", "xh", "xmf", "yi", "yo", "yue", "za", "zea", "zh", |
"vo", "vro", "wa", "war", "wo", "wuu", "xal", "xh", "xmf", "yi", "yo", "yue", "za", "zea", "zh", |
||
"zh-classical", "zh-cn", "zh-hans", "zh-hant", "zh-hk", "zh-min-nan", "zh-mo", "zh-my", "zh-sg", |
"zh-classical", "zh-cn", "zh-hans", "zh-hant", "zh-hk", "zh-min-nan", "zh-mo", "zh-my", "zh-sg", |
||
"zh-tw", "zh-yue", "zu" ] |
"zh-tw", "zh-yue", "zu" ], |
||
*/ |
|||
/** |
/** |
Revision as of 17:09, 4 December 2010
//<source lang="javascript">
/**
* Makes a best guess at the user language.
*
* Refreshes the page to the best guess language
* Makes the uselang parameter persistent among page links to commons
*/
var persistentUselang ={
// The supported list of projects that include language codes at the start of their url
'projectList' : [ 'wikipedia' , 'wiktionary' , 'wikinews' , 'wikibooks' , 'wikisource' , 'wikiversity' , 'wikiquote' ],
// The supported list of language codes, We list out the languages here to avoid an extra api request.
// Api request to get this list:
// http://commons.wikimedia.org/w/api.php?action=query&meta=siteinfo&siprop=languages&format=jsonfm
'rewriteLanguageList' : ["aa", "ab", "ace", "af", "ak", "aln", "als", "am", "an", "ang", "ar", "arc",
"arn", "arz", "as", "ast", "av", "avk", "ay", "az", "ba", "bar", "bat-smg", "bcc", "bcl", "be",
"be-tarask", "be-x-old", "bg", "bh", "bi", "bm", "bn", "bo", "bpy", "bqi", "br", "bs", "bug", "bxr",
"ca", "cbk-zam", "cdo", "ce", "ceb", "ch", "cho", "chr", "chy", "ckb", "ckb-latn", "ckb-arab", "co",
"cr", "crh", "crh-latn", "crh-cyrl", "cs", "csb", "cu", "cv", "cy", "da", "de", "de-at", "de-ch",
"de-formal", "diq", "dk", "dsb", "dv", "dz", "ee", "el", "eml", "en", "en-gb", "eo", "es", "et",
"eu", "ext", "fa", "ff", "fi", "fiu-vro", "fj", "fo", "fr", "frc", "frp", "fur", "fy", "ga", "gag",
"gan", "gan-hans", "gan-hant", "gd", "gl", "glk", "gn", "got", "grc", "gsw", "gu", "gv", "ha",
"hak", "haw", "he", "hi", "hif", "hif-deva", "hif-latn", "hil", "ho", "hr", "hsb", "ht", "hu",
"hy", "hz", "ia", "id", "ie", "ig", "ii", "ik", "ike-cans", "ike-latn", "ilo", "inh", "io", "is",
"it", "iu", "ja", "jbo", "jut", "jv", "ka", "kaa", "kab", "kg", "ki", "kiu", "kj", "kk", "kk-arab",
"kk-cyrl", "kk-latn", "kk-cn", "kk-kz", "kk-tr", "kl", "km", "kn", "ko", "ko-kp", "kr", "kri",
"krj", "ks", "ksh", "ku", "ku-latn", "ku-arab", "kv", "kw", "ky", "la", "lad", "lb", "lbe", "lez",
"lfn", "lg", "li", "lij", "lmo", "ln", "lo", "loz", "lt", "lv", "lzh", "mai", "map-bms", "mdf",
"mg", "mh", "mhr", "mi", "mk", "ml", "mn", "mo", "mr", "ms", "mt", "mus", "mwl", "my", "myv",
"mzn", "na", "nah", "nan", "nap", "nb", "nds", "nds-nl", "ne", "new", "ng", "niu", "nl", "nn",
"no", "nov", "nrm", "nso", "nv", "ny", "oc", "om", "or", "os", "pa", "pag", "pam", "pap", "pcd",
"pdc", "pdt", "pfl", "pi", "pih", "pl", "pms", "pnb", "pnt", "ps", "pt", "pt-br", "qu", "rif",
"rm", "rmy", "rn", "ro", "roa-rup", "roa-tara", "ru", "ruq", "ruq-cyrl", "ruq-latn", "rw", "sa",
"sah", "sc", "scn", "sco", "sd", "sdc", "se", "sei", "sg", "sh", "shi", "si", "simple", "sk",
"sl", "sli", "sm", "sma", "sn", "so", "sq", "sr", "sr-ec", "sr-el", "srn", "ss", "st", "stq",
"su", "sv", "sw", "szl", "ta", "tcy", "te", "tet", "tg", "tg-cyrl", "tg-latn", "th", "ti", "tk",
"tl", "tn", "to", "tokipona", "tp", "tpi", "tr", "ts", "tt", "tt-cyrl", "tt-latn", "tum", "tw",
"ty", "tyv", "udm", "ug", "ug-arab", "ug-latn", "uk", "ur", "uz", "ve", "vec", "vep", "vi", "vls",
"vo", "vro", "wa", "war", "wo", "wuu", "xal", "xh", "xmf", "yi", "yo", "yue", "za", "zea", "zh",
"zh-classical", "zh-cn", "zh-hans", "zh-hant", "zh-hk", "zh-min-nan", "zh-mo", "zh-my", "zh-sg",
"zh-tw", "zh-yue", "zu" ],
/**
* Run the page link rewrites and or refresh the page
*/
'setPageLang': function(){
// Check if we need to refresh the page
if( this.checkLanguageRefresh() ){
// page is being refreshed ( end all script execution )
return true;
}
// try to rewrite all the links in the page to keep uselang persistent
return this.rewritePageLinks();
},
/**
* Checks the rewrite page link mode and calls the doRewritePageLinks() link
*/
'rewritePageLinks': function(){
var _this = this;
var useLang = this.getUseLang();
// Check if we have a uselang to rewrite links for:
if( !useLang ){
// @@TODO We could do an ajax call to the geoIP service here
/*$j.getScript('http://geoiplookup.wikimedia.org/', function(){
// NOTE geoIP should guess language from location.
_this.doRewritePageLinks( useLang );
})
*/
return false;
}
this.doRewritePageLinks( useLang );
},
/**
* Rewrites all the links in the page appending uselang argument
* @param {String} useLang
* The language code to rewrite links for
*/
'doRewritePageLinks': function( useLang ){
if( ! this.isSupportedRewriteLanguage( useLang ) ){
return false;
}
// Wait until the document is ready before checking links in the page:
$j(document).ready(function(){
// Note uselang hacks should not be affected because you have to be logged in to the upload files and hit uselang="ownwork"
$j('a').each(function( inx, node ){
var linkUrl = $j( node ).attr('href');
// Only rewrite commons and relative urls:
if( linkUrl && linkUrl.match( /^http\:\/\/commons.wikimedia.org\/|^\// ) ){
// Only rewrite urls that don't already include uselang
if( linkUrl.indexOf( 'uselang=' == -1 ) ) {
// Question marks should be escaped in urls except for the arguments separator
// check if question mark is in the url:
var paramType = ( linkUrl.indexOf( '?' ) == -1 )? '?' : '&';
$j( node ).attr('href', linkUrl + paramType + 'uselang=' + useLang );
};
};
});
});
},
/**
* Checks if the language is supported
* @param {string} uselang
* The language code to check
* @return {boolean}
* true if language is supported
* false if the language is not supported
*/
'isSupportedRewriteLanguage': function( useLang ){
// Check that the language is supported:
if( $j.inArray( useLang, this.rewriteLanguageList ) == -1 ){
return false;
}
// Just in case someone did not see documentation above ( don't rewrite en )
if( useLang == 'en' ){
return false;
}
return true;
},
/**
* Checks if the page should be refreshed to include the uselang param
*/
'checkLanguageRefresh': function(){
var useLang = this.getUseLang();
// Check if we need to refresh the browser url ( browser language or document.referrer )
if( useLang && !getParamValue( 'uselang' ) && this.isSupportedRewriteLanguage( useLang ) ){
var paramType = ( document.URL.indexOf( '?' ) == -1 ) ? '?' : '&';
window.location.href = document.URL + paramType + 'uselang=' + useLang;
return true;
}
return false;
},
/**
* Master function to get the user language code.
*/
'getUseLang': function(){
// Don't do anything for logged in users ( respect the user preference )
if( wgUserName ){
this.useLang = false;
}
// Check if the page already has a uselang parameter in the url use that.
if( getParamValue( 'uselang' ) ){
return getParamValue( 'uselang' )
}
// Check the referrer url to set the use lang.
if( this.getReferreLang() ){
return this.getReferreLang();
}
// Check for the user browser language
if( this.getBrowserLang() ){
return this.getBrowserLang();
}
return false;
},
/**
* Get the language code from wikimedia site the user came from
* @return {String}
* the referrer language code
*/
'getReferreLang': function(){
if( this.referrerLang ){
return this.referrerLang;
}
if( document.referrer ){
// We should use mw.parseUri ( for now regEx )
var matches = document.referrer.match(/^http\:\/\/([^\.]*)\.([^\.]*)/);
if( matches && jQuery.inArray( matches[2], this.projectList) != -1 ){
// FIXME add in refresh url to add uselang on links from other wikipedias
this.referrerLang = matches[1];
}
}
return this.referrerLang;
},
/**
* Get the user Browser language code
* @return {String}
* the browser language code
*/
'getBrowserLang': function(){
if( this.browserLang ){
return this.browserLang;
}
if( window.navigator.language ){
// fix en-US -> en while keeping all lower case language keys zh-hans
// NOTE: this assumes country codes are always upper case while language codes are all lower case .
var browserLang = window.navigator.language;
var matches = browserLang.match( /([a-z]+)(\-[a-z]+)?/ );
//return window.navigator.language;
this.browserLang = matches[1] + ( matches[2] )? matches[2] : '';
}
return this.browserLang;
}
}
// Run the persistentUselang set page lang function
persistentUselang.setPageLang();
// </source>