User:He7d3r/Tools/LanguageConverter.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, 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: Press Ctrl-F5.
// [[File:User:He7d3r/Tools/LanguageConverter.js]] (workaround for [[phab:T35355]])
/*jslint browser: true, white: true, todo: true, continue: true, forin: true, vars: true, devel: true, regexp: true */
/*global jQuery, mediaWiki, LanguageConverter, self */
/**
 * Based on [[oldwikisource:MediaWiki:Modernisation.js]]
 * @author: [[:fr:User:ThomasV]]
 * @author: [[w:fr:User:Phe]]
 * @author: [[s:fr:User:Tpt]] (https://github.com/Tpt)
 * @author: Helder (https://github.com/he7d3r)
 * @license: CC BY-SA 3.0 <https://creativecommons.org/licenses/by-sa/3.0/>
 */
// <nowiki>
/**
 * Setup the Language Converter global:
 */
// <nowiki>
window.LanguageConverter===void 0&&(window.LanguageConverter={}),function(e,n,t){"use strict";t.version="2.37",t.cookie="lang-variant",t.dictionary===void 0&&(t.dictionary={}),t.variant_links={},t.mustReload=!1,t.getLocalMsg=function(n){var i=t.settings.msg,a=i[n];if(typeof a=="string")return a;if(typeof a=="object"){if(a[t.lang])return a[t.lang];if(a[e.config.get("wgContentLanguage")])return a[e.config.get("wgContentLanguage")];if(a.en)return a.en}return!1},t.get_URL_variant=function(){var n=e.util.getParamValue("variant"),i=t.settings.variants_list;return i[n]?n:null},t.get_cookie_variant=function(){var n=e.cookie.get(t.cookie),i=t.settings.variants_list;return i[n]?n:null},t.get_preferred_variant=function(){var n=t.get_URL_variant()||t.get_cookie_variant()||e.config.get("wgContentLanguage");return n},t.toggle_visibility=function(){var e=!t.settings.show_changes;t.settings.show_changes=e,t.$target.toggleClass("show-changes",e),n("#ca-conv-show-hide-changes").find("a").text(e?t.getLocalMsg("hide_changes_link"):t.getLocalMsg("show_changes_link"))},t.createPortlet=function(){var i,a=e.config.get("skin");switch(a){case"vector":n("#p-variants").after(n("#p-cactions").clone().find("ul").empty().end().attr({id:"p-variants-js"}).find("#p-cactions-label").attr({id:"p-variants-js-label"}).find("span").text(t.getLocalMsg("menu_title")).addClass("flag-"+t.lang).end().end());break;default:i=n("#p-tb").clone().attr("id","p-variants-js").find("li").remove().end().find("h3").text(t.getLocalMsg("menu_title")).end().insertBefore("#p-tb")}},t.render_navigation=function(){var i,a,o=t.settings.variants_list,s=t.settings.show_changes,r=e.config.get("skin"),g=function(n){return function(i){i.preventDefault(),n!==t.lang&&(t.mustReload?location.href=e.util.getUrl(null,{variant:n}):t.startConversion(n))}};t.$target.toggleClass("show-changes",s),t.createPortlet();for(i in o)o[i]!==null&&(a=i===t.lang&&r==="vector",n(e.util.addPortletLink("p-variants-js","#",o[i],"ca-conv-"+i)).toggleClass("selected",a).click(g(i)),a&&n("#p-variants-js-label").find("span").text(o[i]));t.settings.help_page&&t.getLocalMsg("help_page_link")&&e.util.addPortletLink("p-variants-js",e.util.getUrl(t.settings.help_page),t.getLocalMsg("help_page_link"),"ca-conv-help-page"),t.getLocalMsg("show_changes_link")&&t.getLocalMsg("hide_changes_link")&&n(e.util.addPortletLink("p-variants-js","#",s?t.getLocalMsg("hide_changes_link"):t.getLocalMsg("show_changes_link"),"ca-conv-show-hide-changes")).click(function(e){e.preventDefault(),t.toggle_visibility()}),n("#ca-conv-show-hide-changes").toggle(t.lang!==e.config.get("wgContentLanguage"))},t.conv_typo_text=function(e){var n,i,a=t.regTypoChanges.length;for(n=0;a>n;n+=1){i=t.regTypoChanges[n];try{e=e.replace(i[0],i[1])}catch(o){alert(t.getLocalMsg("error_typo_processing")+i[0]+": "+i[1]+".\n\n"+o.message)}}return e},t.conv_typo_node=function(e){var n;if(e.nodeType===3)e.data=t.conv_typo_text(e.data);else for(n=0;e.childNodes.length>n;n+=1)e.id==="editform"||e.className.match(t.regClass)||t.conv_typo_node(e.childNodes[n])},t.conv_typo_document=function(){t.isAllowed()&&(document.title=t.conv_typo_text(document.title),t.conv_typo_node(t.$target[0]))},t.reg_split=function(e,n){var t,i,a,o,s=[],r=0,g=(n.ignoreCase?"i":"")+(n.multiline?"m":"")+(n.sticky?"y":"");n=RegExp(n.source,g+"g");var l=/()??/.exec("")[1]===void 0;e+="",l||(t=RegExp("^"+n.source+"$(?!\\s)",g)),i=n.exec(e);var c=function(){var e;for(e=1;arguments.length-2>e;e+=1)arguments[e]===void 0&&(i[e]=void 0)};while(i)a=i.index+i[0].length,a>r&&(s.push(e.slice(r,i.index)),!l&&i.length>1&&i[0].replace(t,c),i.length>1&&e.length>i.index&&Array.prototype.push.apply(s,i.slice(1)),o=i[0].length,r=a),n.lastIndex===i.index&&(n.lastIndex+=1),i=n.exec(e);return r===e.length?(!n.test("")||o)&&s.push(""):s.push(e.slice(r)),s},t.conv_text_from_dic=function(n,i){var a,o,s,r,g,l,c,p,d,_,u,h,f=RegExp("([^"+t.settings.word_chars+"]+)",""),v=t.reg_split(n,f),m=null,w=t.dictionary;n="",i&&(l=['<span class="v-'+t.lang+'" title="',null,'">',null,"</span>"]);e:for(a=0,s=v.length;s>a;a+=1)if(c=v[a])try{for(r=t.settings.max_seq;r>0;r-=1)if(!(a+2*(r-1)>=s)){for(o=1,g=c;r>o;o+=1)g+=" "+v[a+2*o];if(d=w.hasOwnProperty(g)?w[g]:null,d||(_=g.toLowerCase(),u=g[0].toUpperCase()+_.substring(1),h=g.toUpperCase(),h===g?w.hasOwnProperty(u)?d=w[u].toUpperCase():w.hasOwnProperty(_)&&(d=w[_].toUpperCase()):u===g&&w.hasOwnProperty(_)&&(d=w[_][0].toUpperCase()+w[_].substring(1))),d){i?(l[1]=e.html.escape(g),l[3]=e.html.escape(d),n+=l.join("")):n+=e.html.escape(d),m=!0,a+=2*r-2;continue e}}"&"===c&&"fr"===e.config.get("wgContentLanguage")?(p=v[a-2],d=p&&p===p.toUpperCase()?"ET":"et",i?(l[1]=e.html.escape(c),l[3]=e.html.escape(d),n+=l.join("")):n+=e.html.escape(d),m=!0):n+=e.html.escape(c)}catch(y){alert(t.getLocalMsg("error_word_processing")+v[a]+".\n\n"+y.message)}return m?n:null},t.conv_node_from_dic=function(e){var i,a,o=!0;if(e.nodeType===3)i=t.conv_text_from_dic(e.data,o),i&&n(e).replaceWith(i);else for(a=0;e.childNodes.length>a;a+=1)e.id==="editform"||e.className.match(t.regClass)||t.conv_node_from_dic(e.childNodes[a])},t.conv_callback=function(i){t.conv_typo_document();var a,o,s,r,g,l,c,p,d,_,u,h,f,v,m=t.settings.global_dic_page[t.lang],w=[],y=e.config.get("skin");if(!m)return alert(t.getLocalMsg("error_missing_dict_name")+t.lang),n.removeSpinner("var-spinner"),!1;if(!i)return n.removeSpinner("var-spinner"),!1;if(a=i.query,a&&(o=a.pages,s=a.pageids),!a||!o||!s)return n.removeSpinner("var-spinner"),!1;for(typeof m=="object"&&(m=m.pages||m.page),m=m.split("|"),d=0;s.length>d;d+=1)o[s[d]].pageid?w.push([o[s[d]].revisions[0]["*"],m.indexOf(o[s[d]].title)]):(alert(t.getLocalMsg("error_missing_dict")+o[s[d]].title),location.href=e.util.getUrl(null,{variant:e.config.get("wgContentLanguage")}));if(!w.length)return n.removeSpinner("var-spinner"),void 0;for(w.sort(function(e,n){return e[1]-n[1]}),d=0;w.length>d;d+=1){r=w[d][0],g=r.split("\n");for(l in g)h=/^\*\s*(\S[^:]*?)\s*:\s*([\S].*?)\s*(?:\/\/.*?)?$/.exec(g[l]),h&&(t.dictionary[h[1]]=h[2])}if(_=t.settings.local_dic_id,_&&(_=_[t.lang]),u=_?document.getElementById(_):null){r=u.innerHTML,g=r.split("\n");for(d in g)h=/(?:^|<ul>)<li>\s*(\S[^:]*?)(?:\s|&#160;|&nbsp;)*:\s*([\S].*?)\s*(?:\/\/.*?)?<\/li>(?:$|<\/ul>)/i.exec(g[d]),h&&(t.dictionary[h[1]]=h[2])}e.cookie.set(t.cookie,t.lang,{expires:604800,path:"/",secure:t.secure}),v=!1,c=t.conv_text_from_dic(document.title,v),c&&(document.title=c),t.conv_node_from_dic(t.$target[0]),f=t.settings.variants_list;for(p in f)f[p]!==null&&n("#ca-conv-"+p).toggleClass("selected",p===t.lang&&y==="vector");t.settings.show_menu_title&&n("#p-variants-js-label").find("span").text(f[t.lang]).attr("class","flag-"+t.lang),n("#ca-conv-show-hide-changes").toggle(t.lang!==e.config.get("wgContentLanguage")),t.mustReload=!0,n.removeSpinner("var-spinner")},t.startConversion=function(i){var a,o,s,r,g,l,c;if(t.lang=i,t.regClass=RegExp("(?:\\s|^)(?:v-"+t.lang+"|diff-(?:context|deletedline|addedline)|"+t.settings.no_conversion_class+")(?:\\s|$)"),t.regTypoChanges=[],r=t.settings.typo_changes,r&&(r=r[t.lang]),r)if(r.constructor===Array)for(a=0;r.length>a;a+=1)g=r[a],g.length===2&&g[1]!==null&&(typeof g[0]=="string"&&(g[0]=RegExp(e.util.escapeRegExp(g[0]),"g")),t.regTypoChanges.push(g));else if(r.constructor===Object)for(a in r)if(r.hasOwnProperty&&r[a]!==null)try{o=RegExp(e.util.escapeRegExp(a),"g"),t.regTypoChanges.push([o,r[a]])}catch(p){alert(t.getLocalMsg("error_typo_processing")+a+": "+r[a]+".\n\n"+p.message)}return i===e.config.get("wgContentLanguage")?!1:(n("#p-variants-js").injectSpinner({id:"var-spinner"}),s=t.settings.global_dic_page[t.lang],typeof s=="object"?(l=s.api,typeof l=="object"&&l.length===2&&(l=t.secure?l[1]:l[0]),s=s.pages||s.page,c=l===e.config.get("wgServer")+e.util.wikiScript("api")?"json":"jsonp"):(l=e.util.wikiScript("api"),c="json"),n.ajax({url:l,dataType:c,data:{format:"json",action:"query",titles:s,prop:"revisions",rvprop:"content",indexpageids:"1"},success:t.conv_callback}),void 0)},t.isAllowed=function(){var n=t.settings.ns_list,i="*"===n||n[e.config.get("wgNamespaceNumber")],a=t.settings.local_dic_id,o=a[e.config.get("wgContentLanguage")]?document.getElementById(a[e.config.get("wgContentLanguage")]):!1;switch(t.settings.selection_mode){case"AND":if(!i||!o)return!1;break;case"OR":if(!i&&!o)return!1}return!0},t.load=function(){t.settings={msg:{error_missing_dict:"The following dictionary was not found:\n",error_missing_dict_name:"It is necessary to define the page name of the dictionary for ",error_word_processing:"Error has occurred while processing the following word:\n",error_typo_processing:"Error has occurred while processing the following typographic change:\n",help_page_link:"Open help page",show_changes_link:"Show changes",hide_changes_link:"Hide changes",menu_title:"Variants"},word_chars:"a-zA-Z'-",typo_changes:{},variants_list:{},ns_list:{0:!0},local_dic_id:"modernization",no_conversion_class:"no-conversion",global_dic_page:"Project:Dictionary",help_page:"Project:Language Converter",show_changes:!1,show_menu_title:!0,selection_mode:"AND",max_seq:3},n.extend(!0,t.settings,t.config),t.isAllowed()&&(t.secure=location.protocol==="https:",t.$target=n("#content"),t.$target.length||(t.$target=n("#mw-content-text")),t.lang=t.get_preferred_variant(),e.cookie.set(t.cookie,t.lang,{expires:604800,path:"/",secure:t.secure}),t.render_navigation(),t.startConversion(t.lang))},n.when(e.loader.using(["mediawiki.util","mediawiki.cookie","jquery.spinner"]),n.ready).then(function(){t.load(),self.gadget_typographie&&!self.gadget_mod2&&t.conv_typo_document()})}(mediaWiki,jQuery,window.LanguageConverter);
// </nowiki>