User:JSutherland (WMF)/autoTranslationMarkup.js

This page contains changes which are not marked for translation.

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.
//v1.0
//For documentation about this script, see https://meta.wikimedia.org/wiki/User:JSutherland_(WMF)/autoTranslationMarkup
// Please note that this script outputs markup that does not completely follow the guidelines for translation markup - it tags each line, which is messy. Please feel free to improve it.

mw.loader.using('mediawiki.util', function () {
	$(document).ready(function () {
		//create a button the user can click to indicate they want to run this script 
		//(ganked from https://www.mediawiki.org/wiki/ResourceLoader/Modules#addPortletLink)
		var transLink = mw.util.addPortletLink('p-cactions', '#', 'EasyTranslate', 'pt-EasyTranslate');

		//what happens when they click it? this! 
		//(as long as you're in edit mode. If you run it while just viewing a page, nothing happens)
		$(transLink).click(function(){
			var allText = $("#wpTextbox1").text(),
			texts = allText.split("\n"),
			n = 0,
			wikitext;

			for (i = 0; i < texts.length; i++) {
					var temp = texts[i];
				if(!temp.match(/(^[0-9,\.]+$)|(^\|\s?[0-9]+$)|(^\([0-9,\.]+\)$)|(^[\{\}].*?)|(^\[\[File:.*?)|(\[\[Category\:.*?)|(^<\/?\w+?>$)/)) {
					// This will ignore nodes that are just numbers and punctuation (e.g. table entries), lines beginning with curly brackets (i.e. templates), Files, and Categories.
					// and if node is not one of these...
					if($.trim(texts[i]).length>0){ // ...check it is not empty
						if (temp.match(/^(\s?\|\s+?\w+\s+?=\s?)(.*)/)) { //For templates
							texts[i] = texts[i].replace(/^(\s?\|\s+?\w+\s+?=\s?)(.*)/, "$1<translate>$2</translate>");
						} else if (temp.match(/^[\*\#;\|\!<]/)) { // catch things which need tagged individually
							texts[i] = texts[i].replace(/^(\*+:?|#+:?|;)(.*)/, "$1 <translate>$2</translate>");
							texts[i] = texts[i].replace(/^(\||\!)([^-\{\}+])(.*)/, "$1 <translate>$2$3</translate>");
							texts[i] = texts[i].replace(/(<div.*?>|<small.*?>|<big.*?>)(.+?)(<.?div>|<.?small>|<.?big>)/g, "$1<translate>$2</translate>$3");
						} else {
							// Not sure if this is a good idea or not \/
							// texts[i] = texts[i].replace(/([^<translate\>])(<\/?div.*?>|<\/?span.*?>|<\/?small.*?>|<\/?big.*?>)(.*?)(<\/?div.*?>|<\/?span.*?>|<\/?small.*?>|<\/?big.*?>)([^<translate\>])/g, "$1</tran" + "slate>$2<translate>$3</translate>$4<tran" + "slate>$5");
							texts[i] = "<translate>" + texts[i] + "</translate>"; // ...then wrap the thing in translate tags
						}
						wikitext = wikitext + "\n" + texts[i]; // add the result to the wikitext
					} else {
						wikitext = wikitext + "\n"; // if the line was empty, just add a new line
					}
				} else {
					wikitext = wikitext + "\n" + texts[i]; // if no tags were added but the line wasn't empty, add it back unchanged
				}
			}
			// Adding "tvar"s - probably ought to have more foolproofing
			wikitext = wikitext.replace(/\[\[([^\|]+)\]\]/g, "[[<tvar|TVARNAME>$1</>|$1]]")
				.replace(/(\[\[[^<].*?)(\|)/g, "$1</>$2")
				.replace(/\[\[([^<])/g, "[[<tvar|TVARNAME>$1")
				.replace(/(\[)([a-zA-Z])/g, "$1<tvar|TVARNAME>$2")
				.replace(/(http.*?)\s/g, "$1</> ");
			// This will number the tvar names so they're unique and don't need to be manually added
			wikitext = wikitext.replace(/TVARNAME/g, function(){return ("".concat("link", ++n)) });

			//now to pop the edited wikitext back into the edit window
			wikitext = wikitext.replace(/^undefined\n/, "<noinclude><languages /></noinclude>\n"); //hack for lazy JS above
			wikitext = wikitext.replace(/\* <translate>\*/g, "**"); //laziness involving bullets
			wikitext = wikitext.replace(/\<\/translate\>\<\/translate\>/g, "</translate>"); //laziness involving bullets
			$( '#wpTextbox1' ).val( wikitext );
			// alert("Remember to check for <tvar|NAME>s! There might be lots. Cmd+F them all!");
		}); //end click TransLink
	}); //end doc.ready
}); //end mw.loader