Jump to content

User:Ebrahim/ArticleTranslator.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki> DO NOT COPY IT FOR TRANSLATION, READ THE DOCUMENTATION [[:en:User talk:Ebrahim/ArticleTranslator.js]]
/*global jQuery, mediaWiki*/
(function ($, mw) {
	'use strict';

	var action = mw.config.get('wgAction');
	if (action === 'history') {
		return;
	}

	function linkFromWikiLang(page, lang) {
		if (lang === 'imdb')
			return 'https://www.imdb.com/title/' + page + '/';
		if (lang === 'wikidata')
			return 'https://wikidata.org' + mw.util.getUrl(page);
		return 'https://' + lang + '.wikipedia.org' + mw.util.getUrl(page);
	}

	if ($ && mw && mw.Uri) {
		var golang = new mw.Uri().query.golang || new mw.Uri().query.gotolang;
		if (golang) {
			$.post('https://tofawiki-linkstranslator.wmcloud.org/', {
				p: mw.config.get('wgPageName'),
				from: mw.config.get('wgPageContentLanguage'),
				to: golang
			}).then(function (response) {
				var result = Object.values(response)[0];
				if (result) location.href = linkFromWikiLang(result, golang);
				else mw.notify('The page wasn\'t available on the requested language');
			});
		}
	}

	var conf = {
		homeWiki: 'fa',
		fromLang: mw.config.get('wgPageContentLanguage'),
		translatorBarFormat: '$1translate$2 links from $3 to $4 $5',
		templateTranslatorText: 'Template translation',
		removeLinksAliasesText: 'Remove Links Aliases',
		enableTemplateTranslation: true,
		removeLinksAliases: true,
		name: 'Name',
		interwikiCount: 'Language links count',
		linkedTo: 'Linked to',
		listOfUnavailablePagesOn: 'List of not present pages on',
		definedTemplates: ['Portal', 'About', 'ADB', 'Alsoknown', 'Alternateuses', 'Cat main',
			'Cat main article', 'Category disambiguation', 'Category main', 'Catmain', 'Consider disambiguation', 'Contrast',
			'Dabprefixes', 'Detail', 'Details', 'Disambiguation needed', 'Distinguish', 'Distinguish2', 'For', 'For other uses',
			'For2', 'In title', 'Introductory article', 'Look from', 'Main', 'Main cat', 'Main category', 'Maincat', 'More', 'Moredetails',
			'Navbox hatnote *Templates', 'Other', 'Other hurricanes', 'Other meanings', 'Other people', 'Other people2', 'Other people3',
			'Other people5', 'Other places', 'Other places3', 'Other ships', 'Other use', 'Other uses', 'Other uses of', 'Other uses1',
			'Other uses2', 'Other uses-section', 'Othermeanings', 'Otheruse', 'OtherUses', 'Otheruses1', 'Otheruses3', 'OtherusesSubtopic',
			'Othervalues', 'Outline', 'Previously', 'Redirect', 'Redirect10', 'Redirect2', 'Redirect3', 'Redirect4', 'Redirect6', 'Redirect-distinguish2',
			'Redirect-synonym', 'See introduction', 'See Wiktionary', 'Seesubarticle', 'Selfref', 'Srlink', 'Surname links', 'Technical reasons',
			'Template ambiguous', 'Template shortcut', 'This user talk', 'Three other uses', 'Two other uses', 'WikiProject Disambiguation'],
		blacklistedTemplatePattern: /Template:(cite|citation|infobox) [a-z]+/i,
	};

	$.extend(conf, window.articleTranslatorConf);

	// getting the last translator preference from the cookie
	if ($.cookie && $.cookie('homeWiki') !== null) { conf.homeWiki = $.cookie('homeWiki'); }
	if ($.cookie && $.cookie('fromLang') !== null) { conf.fromLang = $.cookie('fromLang'); }

	function translate(links, showMissings) {
		// unique titles
		links = Object.keys(links.reduce(function (object, item) {
			object[item] = true;
			return object;
		}, {}));

		var request = {
			from: conf.fromLang,
			to: conf.homeWiki,
			missings: !!showMissings,
			p: links.join('|')
		};

		// https://github.com/ebraminio/linkstranslator
		return $.post('https://tofawiki-linkstranslator.wmcloud.org/', request).then(null, function () {
			return $.post('https://linkstranslator.toolforge.org/', request);
		});
	}

	function hasNotPersianCharacter(x) {
		return !/[كﮑﮐﮏﮎﻜﻛﻚﻙىﻯيہەھﻰ-ﻴً-ِْٰء-ٕپچژگکكڪﻙﻚیﻱﻲٔ۱۲۳۴۵۶۷۸۹۰]/.test(x);
	}

	function editboxTranslator() {
		var translationTextArea;

		if (conf.fromLang === mw.config.get('wgPageContentLanguage')) {
			$('#wpTextbox2').remove();
			translationTextArea = $('#wpTextbox1').clone().prop('id', 'wpTextbox2').css({
				'background-color': '#CCCEFF'
			}).val($('#wpTextbox1').val());
			$('#wpTextbox1').before(translationTextArea);
		} else {
			translationTextArea = $('#wpTextbox1');
		}

		var raw = translationTextArea.val();
		if (raw.match(/\{\{(Navbox|Sidebar|Campaignbox)/)) {
			raw = raw.replace(/(\|\s*name\s*=\s*)([^\n\|\}]*)/, '$1' + mw.config.get('wgTitle'));
		}
		$.Deferred().resolve().then(function () {
			var links = (raw.match(/\[\[.*?[|\]]/g) || []).map(function (x) { return x.split(/\[\[:?/)[1].split(/[|\]]/)[0]; });

			if (links.length === 0) {
				return;
			}

			if (conf.homeWiki === 'fa') {
				links = links.filter(hasNotPersianCharacter);
			}

			return translate(links).then(function (result) {
				Object.keys(result).forEach(function (from) {
					raw = raw.replace(
						new RegExp('(\\[\\[:?)' + mw.RegExp.escape(from) + '((?:\\|[^\\]]*)?)(\\]\\])', 'g'),
						'$1' + result[from] + (conf.removeLinksAliases ? '' : '$2') + '$3'
					);
				});
			});

		}).then(function () {
			if (!conf.enableTemplateTranslation) { return; }

			var templateLinksRegexp = new RegExp('(\\{\\{\\s*(?:Template:)?(?:' + conf.definedTemplates.join('|') + ')\\|)([^\n|]+?)((?:\\|[^\n\}]*?)?\\}\\})', 'ig');
			var links = (raw.match(templateLinksRegexp) || []).map(function (x) { return x.split('|')[1].split('}')[0]; });

			if (links.length === 0) {
				return;
			}

			return translate(links).then(function (result) {
				raw = raw.replace(templateLinksRegexp, function ($0, $1, $2, $3) {
					return $1 + (result[$2] || $2) + $3;
				});
			});

		}).then(function () {
			if (!conf.enableTemplateTranslation) { return; }

			var templatesRegexp = /((?:[^{]|^)\{\{\s*(?:Template:|))([^\n|{]+?)([|}\n])/ig;
			var links = (raw.match(templatesRegexp) || [])
				.map(function (x) { return 'Template:' + x.split(/\{\{/)[1].split(/[|}\n]/)[0]; })
				.filter(function (x) { return !conf.blacklistedTemplatePattern.test(x); });

			if (links.length === 0) {
				return;
			}

			if (conf.homeWiki === 'fa') {
				links = links.filter(hasNotPersianCharacter);
			}

			return translate(links).then(function (result) {
				raw = raw.replace(templatesRegexp, function ($0, $1, $2, $3) {
					return result['Template:' + $2] ? ($1 + result['Template:' + $2].replace(/[^:]*:/, '') + $3) : $0;
				});
			});

		}).then(function () {
			// Persian specific cleanings, disabled: https://fa.wikipedia.org/?diff=18668571
			//if (conf.homeWiki === 'fa')
			//	raw = raw.replace(/\]\]s/g, ']]').replace(/, /g, '، ');

			translationTextArea.val(raw);
		});
	}

	function viewTranslator(translatorBar, showMissings) {
		$('.linkstranslator-added-content, .linkstranslator-missings-wrapper').remove();
	
		var pageLinks = {};
		document.querySelectorAll('#bodyContent a').forEach(function (x) {
			var href = x.getAttribute('href');
			if (!href || href.indexOf('/wiki/') === -1) return;
			var title = decodeURIComponent(href.replace(/.*?\/wiki\//, ''))
				.replace(/_/g, ' ').replace(/#.*$/g, '');
			if (title.length) {
				if (!pageLinks[title]) pageLinks[title] = [];
				pageLinks[title].push(x);
			}
		});
		
		$('.linkstranslator-added-content, .linkstranslator-missings-wrapper', translatorBar).remove();
		$('.translator-button, .translator-plus', translatorBar).css('color', 'lightgray');
		translate(Object.keys(pageLinks), showMissings).then(function (result) {
			$('.linkstranslator-added-content, .linkstranslator-missings-wrapper').remove();

			$('.translator-button, .translator-plus', translatorBar).css('color', '#808b96');

			var processedResult = Object.keys(result).reduce(function (o, key) {
				if (key.indexOf('#') !== 0)
					o[mw.util.getUrl(key).split('/wiki/')[1]] = result[key];
				return o;
			}, {});

			Object.entries(result).forEach(function (entry) {
				(pageLinks[entry[0]] || []).forEach(function (x) {
					$(x).after($('<span>', {
						class: 'linkstranslator-added-content'
					}).append('(', !navigator.clipboard ? '' : $('<a>', {
						href: '#',
						style: 'font-size: 140%; font-weight: normal; line-height: 0;',
						title: 'Copy to clipboard',
						text: '⎘',
						click: function (e) {
							e.preventDefault();
							navigator.clipboard.writeText(entry[1]).then(function () {
								e.target.animate && e.target.animate([
									{ fontSize: '140%' }, { fontSize: '100%' }
								], { duration: 220, iterations: 1 });
							}, console.error);
						}
					}), ' ', $('<bdi>').append($('<a>', {
						lang: conf.homeWiki,
						href: linkFromWikiLang(entry[1], conf.homeWiki),
						text: entry[1]
					})), ')'));
				});
			});

			if (showMissings) showTables(translatorBar, result, pageLinks);

			if (result['#debug']) console.log('Server debug:', result['#debug']);
		});
	}
	function showTables(translatorBar, result, pageLinks) {
		var missings = result['#missings'];
		// Red nodes inserted after the missing pages
		Object.keys(missings).map(function (page) {
			if (!missings[page].langlinks) return;
			(pageLinks[page] || []).forEach(function (link) {
				$(link).after($('<span>', {
					class: 'linkstranslator-added-content'
				}).append('(', $('<bdi>', {
					style: 'color: red',
					text: missings[page].langlinks,
					class: 'translatorNeededLink'
				}), ')'));
			});
		});

		// Missings table
		var links = Object.keys(missings).map(function (page) {
			return [page, missings[page].langlinks, missings[page].links];
		}).filter(function (x) { return x[1]; });

		links = links.sort(function (x, y) { return y[1] - x[1]; });
		var start = document.dir === 'ltr' ? 'left' : 'right';
		$('header').after($('<div>', {
			style: 'line-height: 1.25; font-size: 50%;',
			class: 'linkstranslator-missings-wrapper'
		}).append(
			conf.listOfUnavailablePagesOn + ' ' + conf.homeWiki + '.wiki: ',
			$('<div>', {
				style: 'height: 10em; overflow-y: scroll; overflow-x: hidden; width: 100em;'
			}).append(
				$('<div>', { style: 'float: ' + start }).append(
					$('<table>', {
						class: 'wikitable sortable'
					}).append($('<tr>').append(
						$('<th>', { text: conf.name }),
						$('<th>', { text: conf.interwikiCount }),
						$('<th>', { text: conf.linkedTo })
					)).append(links.map(function (x) {
						return $('<tr>').append(
							$('<td>').append($('<a>', {
								href: linkFromWikiLang(x[0], conf.fromLang),
								text: x[0]
							})),
							$('<td>', { text: x[1].toLocaleString(mw.config.get('wgUserLanguage')) }),
							$('<td>', { text: x[2].toLocaleString(mw.config.get('wgUserLanguage')) })
						);
					}))
				),
				$('<div>', { style: 'width: 2em; float: ' + start }).append('<br>'),
				$('<div>', { style: 'float: ' + start }).append(
					$('<table>', {
						class: 'wikitable sortable'
					}).append($('<tr>').append(
						$('<th>', { text: conf.fromLang }),
						$('<th>', { text: conf.homeWiki })
					)).append(Object.keys(result).map(function (x) {
						if (x.indexOf('#') === 0) return '';
						return $('<tr>').append(
							$('<td>', { dir: 'auto' }).append($('<a>', {
								href: linkFromWikiLang(x, conf.fromLang),
								text: x
							})),
							$('<td>', { dir: 'auto' }).append($('<a>', {
								href: linkFromWikiLang(result[x], conf.homeWiki),
								text: result[x]
							}))
						);
					}))
				)
			)
		));
	}

	function saveLanguageConfigs(translatorBar) {
		if ($('.translator-from', translatorBar).val().trim() === '') {
			$('.translator-from', translatorBar).val(conf.fromLang || 'fa');
		}
		conf.fromLang = $('.translator-from', translatorBar).val();
		$.cookie('fromLang', conf.fromLang);
		if ($('.translator-to', translatorBar).val().trim() === '') {
			$('.translator-to', translatorBar).val(conf.homeWiki || 'en');
		}
		conf.homeWiki = $('.translator-to', translatorBar).val();
		$.cookie('homeWiki', conf.homeWiki);
	}

	$(function () {
		$('.translatorBar').remove();

		var h1 = $('h1.firstHeading:first');

		if (h1.width() === 0 && mw.config.get('wgContentLanguage') === 'en') {
			h1 = $('<span>').prependTo('#mw-content-text');
		}
		
		var translatorBar = $('<span>', { class: 'translatorBar noprint' });

		h1.append(translatorBar.css({
			margin: '0 2em',
			'font-size': '40%'
		}).append($('<span>').css({
			display: 'inline-block',
			'user-select': 'none'
		}).append(
			conf.translatorBarFormat
				.replace('$1', '<sub class="translator-equ-wrapper"/> <a class="translator-button" href="#">')
				.replace('$2', '</a>' + (action === 'view' ? '<sup><a class="translator-plus" href="#">+</a></sup>' : '') +
					'<span contenteditable style="display: inline-block"></span>')
				.replace('$3', '<input class="translator-from">')
				.replace('$4', '<input class="translator-to">')
				.replace('$5', '<a class="translator-switch" href="#">⇄</a>')
		)));
		if (action === 'view') {
			$('.translator-equ-wrapper', translatorBar).append([
				$('<a>', { href: '#', text: '=' }).click(function (e) {
					e.preventDefault();
					var title = mw.config.get('wgTitle');
					$(e.target).after(
						$('<a>', {
							target: '_blank',
							href: new mw.Uri('https://translate.google.com/').extend({
								sl: conf.fromLang,
								tl: conf.homeWiki,
								q: title
							}),
							text: 'Google Translator'
						}),
						' / ',
						$('<a>', {
							target: '_blank',
							href: 'https://www.bing.com/translator',
							text: 'Bing Translator'
						}),
						' / ',
						$('<a>', {
							target: '_blank',
							href: new mw.Uri('https://www.google.com/search').extend({
								q: title,
								lr: 'lang_' + conf.homeWiki
							}),
							text: 'Language Restricted Search'
						}),
						' / ',
						$('<a>', {
							target: '_blank',
							text: 'Wiktionary',
							href: 'https://en.wiktionary.org' + mw.util.getUrl(mw.config.get('wgTitle'))
						}),
						' / ',
						$('<a>', {
							target: '_blank',
							text: 'MinT',
							href: 'https://translate.wmcloud.org'
						})
					).hide();
				}),
				' ',
				mw.config.get('wgContentLanguage') !== 'en' ? '' : $('<a>', {
					text: '▶️',
					href: '#'
				}).click(function (e) {
					e.preventDefault();
					var utterance = new SpeechSynthesisUtterance(mw.config.get('wgTitle'));
					utterance.rate = 0.6;
					speechSynthesis.speak(utterance);
				}),
				' ',
				mw.config.get('wgContentLanguage') !== 'en' ? '' : $('<a>', {
					target: '_blank',
					text: 'f',
					href: 'https://forvo.com/search/' + encodeURI(mw.config.get('wgTitle'))
				})
			]);
		}
		$('.translator-from', translatorBar).prop({
			value: conf.fromLang,
			spellcheck: 'false'
		});
		$('.translator-to', translatorBar).prop({
			value: conf.homeWiki,
			spellcheck: 'false'
		});
		$('.translator-from, .translator-to', translatorBar).css({
			display: 'inline-block',
			width: '18px',
			border: 'none',
			'background-color': 'transparent',
			'text-align': 'center',
			'font-family': 'inherit',
			'font-size': 'inherit'
		});

		if (action === 'edit' || action === 'submit') {
			translatorBar.append(
				' ',
				$('<label>', {
					text: ' ' + conf.templateTranslatorText
				}).prepend($('<input>', {
					type: 'checkbox',
					class: 'enableTemplateTranslation'
				})),
				' ',
				$('<label>', {
					text: ' ' + conf.removeLinksAliasesText
				}).prepend($('<input>', {
					type: 'checkbox',
					class: 'removeLinksAliases'
				}))
			);
		}

		$('.translator-button, .translator-plus', translatorBar).click(function (e) {
			e.preventDefault();
			saveLanguageConfigs(translatorBar);
			if (action === 'edit' || action === 'submit') {
				editboxTranslator();
			} else {
				viewTranslator(translatorBar, e.target.classList.contains('translator-plus'));
			}
		});

		$('.translator-switch', translatorBar).click(function (event) {
			event.preventDefault();

			var t = conf.homeWiki;

			conf.homeWiki = conf.fromLang;
			$.cookie('homeWiki', conf.fromLang);
			$('.translator-to', translatorBar).val(conf.fromLang);

			conf.fromLang = t;
			$.cookie('fromLang', t);
			$('.translator-from', translatorBar).val(t);
		});

		// disable enter on them
		$('.translator-from, .translator-to', translatorBar).keypress(function (e) {
			if (e.which !== 13) return;
			e.target.blur();
			e.preventDefault();
		}).click(function () {
			document.execCommand('selectAll', false, null);
		}).focusout(function () { saveLanguageConfigs(translatorBar); });


		$('.enableTemplateTranslation', translatorBar).attr('checked', conf.enableTemplateTranslation).click(function (e) {
			conf.enableTemplateTranslation = e.target.checked;
		});

		$('.removeLinksAliases', translatorBar).attr('checked', conf.removeLinksAliases).click(function (e) {
			conf.removeLinksAliases = e.target.checked;
		});
	});
}(jQuery, mediaWiki));