User:Redaking/ArticleTranslator.js: Difference between revisions
Appearance
Content deleted Content added
No edit summary Tag: Reverted |
No edit summary Tags: Manual revert Reverted |
||
Line 1: | Line 1: | ||
// <nowiki> DO NOT COPY IT FOR TRANSLATION, READ THE DOCUMENTATION [[:en:User talk: |
// <nowiki> DO NOT COPY IT FOR TRANSLATION, READ THE DOCUMENTATION [[:en:User talk:Ebraminio/ArticleTranslator.js]] |
||
/*global jQuery, mediaWiki*/ |
/*global jQuery, mediaWiki*/ |
||
(function ($, mw) { |
(function ($, mw) { |
||
'use strict'; |
'use strict'; |
||
// prevent double execution of the tool |
|||
if (window.isArticletranslatorInitialized && location.hash === '#tofawikiframe') { return; } |
|||
window.isArticletranslatorInitialized = true; |
|||
var action = mw.config.get('wgAction'); |
var action = mw.config.get('wgAction'); |
||
if (action === 'history') { |
if (action === 'history') { |
||
return; |
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 = { |
var conf = { |
||
homeWiki: ' |
homeWiki: 'hy', |
||
fromLang: mw.config.get('wgPageContentLanguage'), |
fromLang: mw.config.get('wgPageContentLanguage'), |
||
translatorBarFormat: '$1translate$2 links from $3 to $4 $5', |
translatorBarFormat: '$1translate$2 links from $3 to $4 $5', |
||
Line 44: | Line 25: | ||
linkedTo: 'Linked to', |
linkedTo: 'Linked to', |
||
listOfUnavailablePagesOn: 'List of not present pages on', |
listOfUnavailablePagesOn: 'List of not present pages on', |
||
definedTemplates: ['Portal', 'About', 'ADB', 'Alsoknown', 'Alternateuses', 'Cat main', |
definedTemplates: ['Flag icon', 'flagicon', 'Flag', 'Portal', 'About', 'ADB', 'Alsoknown', 'Alternateuses', 'Cat main', |
||
'Cat main article', 'Category disambiguation', 'Category main', 'Catmain', 'Consider disambiguation', 'Contrast', |
'Cat main article', 'Category disambiguation', 'Category main', 'Catmain', 'Consider disambiguation', 'Contrast', |
||
'Dabprefixes', 'Detail', 'Details', 'Disambiguation needed', 'Distinguish', 'Distinguish2', 'For', 'For other uses', |
'Dabprefixes', 'Detail', 'Details', 'Disambiguation needed', 'Distinguish', 'Distinguish2', 'For', 'For other uses', |
||
Line 62: | Line 43: | ||
if ($.cookie && $.cookie('homeWiki') !== null) { conf.homeWiki = $.cookie('homeWiki'); } |
if ($.cookie && $.cookie('homeWiki') !== null) { conf.homeWiki = $.cookie('homeWiki'); } |
||
if ($.cookie && $.cookie('fromLang') !== null) { conf.fromLang = $.cookie('fromLang'); } |
if ($.cookie && $.cookie('fromLang') !== null) { conf.fromLang = $.cookie('fromLang'); } |
||
function translate(links, showMissings) { |
function translate(links, showMissings) { |
||
// unique titles |
// unique titles |
||
Line 69: | Line 50: | ||
return object; |
return object; |
||
}, {})); |
}, {})); |
||
var request = { |
var request = { |
||
from: conf.fromLang, |
from: conf.fromLang, |
||
Line 76: | Line 57: | ||
p: links.join('|') |
p: links.join('|') |
||
}; |
}; |
||
// https://github.com/ebraminio/linkstranslator |
// https://github.com/ebraminio/linkstranslator |
||
return $.post('https:// |
return $.post('https://tools.wmflabs.org/linkstranslator/', request).then(null, function () { |
||
return $.post('https:// |
return $.post('https://fa-tts.wmflabs.org/linkstranslator/', request); |
||
}); |
}); |
||
} |
} |
||
Line 92: | Line 73: | ||
if (conf.fromLang === mw.config.get('wgPageContentLanguage')) { |
if (conf.fromLang === mw.config.get('wgPageContentLanguage')) { |
||
$('#wpTextbox2').remove(); |
$('#wpTextbox2').remove(); |
||
translationTextArea = $('#wpTextbox1').clone(). |
translationTextArea = $('#wpTextbox1').clone().attr({ |
||
'id': 'wpTextbox2' |
|||
}).css({ |
|||
'background-color': '#CCCEFF' |
'background-color': '#CCCEFF' |
||
}).val($('#wpTextbox1').val()); |
}).val($('#wpTextbox1').val()); |
||
Line 99: | Line 82: | ||
translationTextArea = $('#wpTextbox1'); |
translationTextArea = $('#wpTextbox1'); |
||
} |
} |
||
var raw = translationTextArea.val(); |
var raw = translationTextArea.val(); |
||
if (raw.match(/\{\{(Navbox|Sidebar|Campaignbox)/)) { |
if (raw.match(/\{\{(Navbox|Sidebar|Campaignbox)/)) { |
||
Line 111: | Line 94: | ||
} |
} |
||
if (conf.homeWiki === ' |
if (conf.homeWiki === 'hy') { |
||
links = links.filter(hasNotPersianCharacter); |
links = links.filter(hasNotPersianCharacter); |
||
} |
} |
||
Line 125: | Line 108: | ||
}).then(function () { |
}).then(function () { |
||
if (!conf.enableTemplateTranslation) { return; } |
|||
var templateLinksRegexp = new RegExp('(\\{\\{\\s*(?:Template:)?(?:' + conf.definedTemplates.join('|') + ')\\|)([^\n|]+?)((?:\\|[^\n\}]*?)?\\}\\})', 'ig'); |
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]; }); |
var links = (raw.match(templateLinksRegexp) || []).map(function (x) { return x.split('|')[1].split('}')[0]; }); |
||
Line 142: | Line 123: | ||
}).then(function () { |
}).then(function () { |
||
if (!conf.enableTemplateTranslation) { return; } |
if (!conf.enableTemplateTranslation) { return; } |
||
var templatesRegexp = /((?:[^{]|^)\{\{\s*(?:Template:|))([^\n|{]+?)([|}\n])/ig; |
var templatesRegexp = /((?:[^{]|^)\{\{\s*(?:Template:|))([^\n|{]+?)([|}\n])/ig; |
||
var links = (raw.match(templatesRegexp) || []) |
var links = (raw.match(templatesRegexp) || []) |
||
Line 152: | Line 133: | ||
} |
} |
||
if (conf.homeWiki === ' |
if (conf.homeWiki === 'hy') { |
||
links = links.filter(hasNotPersianCharacter); |
links = links.filter(hasNotPersianCharacter); |
||
} |
} |
||
Line 163: | Line 144: | ||
}).then(function () { |
}).then(function () { |
||
// Persian specific cleanings, disabled: https://fa.wikipedia.org/ |
// Persian specific cleanings, disabled: https://fa.wikipedia.org/؟diff=18668571 |
||
//if (conf.homeWiki === ' |
//if (conf.homeWiki === 'fa') |
||
// raw = raw.replace(/\]\]s/g, ']]').replace(/, /g, '، '); |
// raw = raw.replace(/\]\]s/g, ']]').replace(/, /g, '، '); |
||
translationTextArea.val(raw); |
translationTextArea.val(raw); |
||
}); |
}); |
||
} |
} |
||
function |
function linkFromWikiLang(page, lang) { |
||
if (lang === 'imdb') |
|||
$('.linkstranslator-added-content, .linkstranslator-missings-wrapper').remove(); |
|||
return 'http://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); |
|||
} |
|||
var wgUserLanguage = mw.config.get('wgUserLanguage'); |
|||
var pageLinks = {}; |
|||
document.querySelectorAll('#bodyContent a').forEach(function (x) { |
|||
function showTables(result) { |
|||
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']; |
var missings = result['#missings']; |
||
// Red nodes inserted after the missing pages |
// Red nodes inserted after the missing pages |
||
Object.keys(missings).map(function (page) { |
Object.keys(missings).map(function (page) { |
||
if (!missings[page].langlinks) return; |
if (!missings[page].langlinks) return; |
||
$('#bodyContent a[href$="' + mw.util.getUrl(page) + '"]').after( |
|||
(pageLinks[page] || []).forEach(function (link) { |
|||
$('<span>', { |
|||
class: 'linkstranslator-added-content' |
'class': 'linkstranslator-added-content' |
||
}).append('(', $('<bdi>', { |
}).append('(', $('<bdi>', { |
||
style: 'color: red', |
style: 'color: red', |
||
'class': 'translatorNeededLink' |
|||
text: missings[page].langlinks, |
|||
}).text(missings[page].langlinks), ')') |
|||
class: 'translatorNeededLink' |
|||
); |
|||
}); |
|||
}); |
}); |
||
Line 252: | Line 184: | ||
links = links.sort(function (x, y) { return y[1] - x[1]; }); |
links = links.sort(function (x, y) { return y[1] - x[1]; }); |
||
var start = document.dir === 'ltr' ? 'left' : 'right'; |
var start = document.dir === 'ltr' ? 'left' : 'right'; |
||
$(' |
$('#translatorBar').after($('<div>', { |
||
style: 'line-height: 1.25; font-size: 50%;', |
style: 'line-height: 1.25; font-size: 50%;', |
||
id: 'linkstranslator-missings-wrapper' |
|||
}).append( |
}).append( |
||
conf.listOfUnavailablePagesOn + ' ' + conf.homeWiki + '.wiki: ', |
conf.listOfUnavailablePagesOn + ' ' + conf.homeWiki + '.wiki: ', |
||
Line 261: | Line 193: | ||
}).append( |
}).append( |
||
$('<div>', { style: 'float: ' + start }).append( |
$('<div>', { style: 'float: ' + start }).append( |
||
$('<table>', { |
$('<table>', { 'class': 'wikitable sortable' }).append( |
||
$('<tr>').append( |
|||
class: 'wikitable sortable' |
|||
$('<th>').text(conf.name), |
|||
$('<th>' |
$('<th>').text(conf.interwikiCount), |
||
$('<th>' |
$('<th>').text(conf.linkedTo) |
||
) |
|||
$('<th>', { text: conf.linkedTo }) |
|||
).append(links.map(function (x) { |
|||
return $('<tr>').append( |
return $('<tr>').append( |
||
$('<td>').append($('<a>', { |
$('<td>').append($('<a>', { |
||
Line 273: | Line 205: | ||
text: x[0] |
text: x[0] |
||
})), |
})), |
||
$('<td>' |
$('<td>').text(x[1].toLocaleString(wgUserLanguage)), |
||
$('<td>' |
$('<td>').text(x[2].toLocaleString(wgUserLanguage)) |
||
); |
); |
||
})) |
})) |
||
Line 280: | Line 212: | ||
$('<div>', { style: 'width: 2em; float: ' + start }).append('<br>'), |
$('<div>', { style: 'width: 2em; float: ' + start }).append('<br>'), |
||
$('<div>', { style: 'float: ' + start }).append( |
$('<div>', { style: 'float: ' + start }).append( |
||
$('<table>', { |
$('<table>', { 'class': 'wikitable sortable' }).append( |
||
$('<tr>').append( |
|||
class: 'wikitable sortable' |
|||
$('<th>').text(conf.fromLang), |
|||
$('<th>' |
$('<th>').text(conf.homeWiki) |
||
) |
|||
$('<th>', { text: conf.homeWiki }) |
|||
).append(Object.keys(result).map(function (x) { |
|||
if (x.indexOf('#') === 0) return ''; |
if (x.indexOf('#') === 0) return ''; |
||
return $('<tr>').append( |
return $('<tr>').append( |
||
Line 303: | Line 235: | ||
} |
} |
||
function |
function viewTranslator(showMissings) { |
||
$('.linkstranslator-added-content, #linkstranslator-missings-wrapper').remove(); |
|||
if ($('.translator-from', translatorBar).val().trim() === '') { |
|||
$('#translator-button, #translator-plus').css('color', 'lightgray'); |
|||
var titles = {}; |
|||
$('#bodyContent a').get().forEach(function (x) { |
|||
var href = x.getAttribute('href'); |
|||
if (!href || href.indexOf('/wiki/') === -1) return; |
|||
var title = decodeURI(href.replace(/.*?\/wiki\//, '')) |
|||
.replace(/_/g, ' ').replace(/#.*$/g, ''); |
|||
if (title) { |
|||
titles[title] = true; |
|||
} |
|||
}); |
|||
titles = Object.keys(titles); |
|||
translate(titles, showMissings).then(function (result) { |
|||
$('.linkstranslator-added-content, #linkstranslator-missings-wrapper').remove(); |
|||
$('#translator-button, #translator-plus').css('color', '#808b96'); |
|||
Object.keys(result).forEach(function (from) { |
|||
if (from.indexOf('#') === 0) return; |
|||
var to = result[from]; |
|||
$('#bodyContent a[href$="' + mw.util.getUrl(from) + '"]').after( |
|||
$('<span>', { |
|||
'class': 'linkstranslator-added-content' |
|||
}).append('(', $('<a>', { |
|||
href: '#', |
|||
'class': 'linkstranslator-link-copytext', |
|||
style: 'font-size: 140%; font-weight: normal; line-height: 0;', |
|||
title: 'Copy to clipboard', |
|||
text: '⎘' |
|||
}), ' ', $('<bdi>').append( |
|||
$('<a>', { |
|||
lang: conf.homeWiki, |
|||
href: linkFromWikiLang(to, conf.homeWiki), |
|||
text: to |
|||
}) |
|||
), ')') |
|||
); |
|||
}); |
|||
$('#bodyContent').after($('<textarea id="linkstranslator-link-textarea"></textarea>').css({ |
|||
// http://stackoverflow.com/a/30810322 |
|||
position: 'fixed', top: 0, right: '-2em', width: '2em', height: '2em', |
|||
padding: 0, border: 'none', outline: 'none', boxShadow: 'none', |
|||
background: 'transparent' |
|||
})); |
|||
$('.linkstranslator-link-copytext').click(function (event) { |
|||
event.preventDefault(); |
|||
var el = event.target; |
|||
var text = el.nextElementSibling.firstChild.innerText; |
|||
if (location.hash === '#tofawikiframe') |
|||
text = '[[' + text + ']]'; |
|||
$('#linkstranslator-link-textarea').val(text)[0].select(); |
|||
var success = false; |
|||
try { success = document.execCommand('copy'); } catch (e) { } |
|||
if (!success) return; |
|||
$(el).animate({ fontSize: '200%' }, 20) |
|||
.animate({ fontSize: '140%' }, 200); |
|||
}); |
|||
if (showMissings) showTables(result); |
|||
if (result['#debug']) console.log('Server debug:', result['#debug']); |
|||
}); |
|||
} |
|||
function saveLanguageConfigs() { |
|||
if ($('#translator-from').text().trim() === '') { |
|||
$('#translator-from').text(conf.fromLang || 'hy'); |
|||
} |
} |
||
conf.fromLang = $(' |
conf.fromLang = $('#translator-from').text(); |
||
$.cookie('fromLang', conf.fromLang); |
$.cookie('fromLang', conf.fromLang); |
||
if ($(' |
if ($('#translator-to').text().trim() === '') { |
||
$(' |
$('#translator-to').text(conf.homeWiki || 'en'); |
||
} |
} |
||
conf.homeWiki = $(' |
conf.homeWiki = $('#translator-to').text(); |
||
$.cookie('homeWiki', conf.homeWiki); |
$.cookie('homeWiki', conf.homeWiki); |
||
} |
} |
||
$(function () { |
$(function () { |
||
$(' |
$('#translatorBar').remove(); |
||
var h1 = $('h1.firstHeading:first'); |
var h1 = $('h1.firstHeading:first'); |
||
if (h1.width() === 0 && mw.config.get('wgContentLanguage') === 'en') { |
if (h1.width() === 0 && mw.config.get('wgContentLanguage') === 'en') { |
||
h1 = $('<span>').prependTo('#mw-content-text'); |
h1 = $('<span>').prependTo($('#mw-content-text')); |
||
} |
} |
||
var translatorBar = $('<span>', { class: 'translatorBar noprint' }); |
|||
h1.append( |
h1.append($('<span>', { |
||
style: 'font-size: 40%; margin: 0 2em; display: inline-block', |
|||
margin: '0 2em', |
|||
id: 'translatorBar', |
|||
'class': 'noprint' |
|||
}).append($('<span>').css({ |
|||
display: 'inline-block', |
|||
'user-select': 'none' |
|||
}).append( |
}).append( |
||
$('<span contenteditable>'), |
|||
conf.translatorBarFormat |
conf.translatorBarFormat |
||
.replace('$1', '<sub |
.replace('$1', '<sub>' + (action === 'view' ? '<a id="translator-equ" href="#">=</a> <a id="translator-equ-play" href="#">▶️</a> <a id="translator-equ-frovo" target="_blank" href="https://forvo.com/search/' + encodeURI(mw.config.get('wgTitle')) + '">f</a> ' : '') + '<span id="translator-equ-links"></span></sub><a id="translator-button" href="#">') |
||
.replace('$2', '</a>' + (action === 'view' ? '<sup><a |
.replace('$2', '</a>' + (action === 'view' ? '<sup><a id="translator-plus" href="#">+</a></sup>' : '')) |
||
.replace('$3', '<span id="translator-from" spellcheck="false" style="display: inline-block" contenteditable>' + conf.fromLang + '</span>') |
|||
.replace('$ |
.replace('$4', '<span id="translator-to" spellcheck="false" style="display: inline-block" contenteditable>' + conf.homeWiki + '</span>') |
||
.replace('$ |
.replace('$5', '<a id="translator-switch" href="#">⇄</a>') |
||
)); |
|||
.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') { |
if (action === 'edit' || action === 'submit') { |
||
translatorBar.append( |
$('#translatorBar').append( |
||
' ', |
' ', |
||
$('<input>', { type: 'checkbox', name: 'enableTemplateTranslation', id: 'enableTemplateTranslation' }), |
|||
$('<label>', { |
|||
$('<label>', { 'for': 'enableTemplateTranslation', text: conf.templateTranslatorText }), |
|||
}).prepend($('<input>', { |
|||
type: 'checkbox', |
|||
class: 'enableTemplateTranslation' |
|||
})), |
|||
' ', |
' ', |
||
$('<input>', { type: 'checkbox', name: 'removeLinksAliases', id: 'removeLinksAliases' }), |
|||
$('<label>', { |
|||
$('<label>', { 'for': 'removeLinksAliases', text: conf.removeLinksAliasesText }) |
|||
}).prepend($('<input>', { |
|||
type: 'checkbox', |
|||
class: 'removeLinksAliases' |
|||
})) |
|||
); |
); |
||
} |
} |
||
$(' |
$('#translator-button, #translator-plus').click(function (event) { |
||
event.preventDefault(); |
|||
saveLanguageConfigs( |
saveLanguageConfigs(); |
||
if (action === 'edit' || action === 'submit') { |
if (action === 'edit' || action === 'submit') { |
||
editboxTranslator(); |
editboxTranslator(); |
||
} else { |
} else { |
||
viewTranslator( |
viewTranslator(this.id === 'translator-plus'); |
||
} |
} |
||
}); |
}); |
||
// run translator if is in tofawiki context |
|||
$('.translator-switch', translatorBar).click(function (event) { |
|||
if (window.self !== window.top && location.hash === '#tofawikiframe') { |
|||
viewTranslator(false); |
|||
$('#mw-page-base, #mw-head-base, #mw-navigation, #firstHeading, #siteSub, #footer, .read-more-container').remove(); |
|||
$('#content').css('margin-left', 0).css('padding-top', 0); |
|||
importStylesheet('MediaWiki:Print.css'); |
|||
} |
|||
$('#translator-equ').click(function (event) { |
|||
event.preventDefault(); |
|||
var title = mw.config.get('wgTitle'); |
|||
$('#translator-equ-links').empty().append( |
|||
$('<a>', { target: '_blank', href: 'https://translate.google.com/?sl=' + conf.fromLang + '&tl=' + conf.homeWiki + '&q=' + title, text: 'Translator' }), |
|||
' / ', |
|||
$('<a>', { target: '_blank', href: 'https://www.google.com/search?q="' + encodeURI(title) + '"&lr=lang_' + conf.homeWiki, text: 'Specific Language Search' }), |
|||
' / ', |
|||
$('<a>', { target: '_blank', href: 'https://www.bing.com/translator', text: 'Bing Translator' }) |
|||
); |
|||
}); |
|||
$('#translator-equ-play').click(function (e) { |
|||
e.preventDefault(); |
|||
new Audio('https://dls-espeakbox.wmflabs.org/speech?speed=100&text=' + encodeURI(mw.config.get('wgTitle'))).play(); |
|||
}); |
|||
// hide play button on non-English wiki |
|||
mw.config.get('wgContentLanguage') !== 'en' && $('#translator-equ-play, #translator-equ-frovo').hide(); |
|||
$('#translator-switch').click(function (event) { |
|||
event.preventDefault(); |
event.preventDefault(); |
||
Line 458: | Line 401: | ||
conf.homeWiki = conf.fromLang; |
conf.homeWiki = conf.fromLang; |
||
$.cookie('homeWiki', conf.fromLang); |
$.cookie('homeWiki', conf.fromLang); |
||
$(' |
$('#translator-to').text(conf.fromLang); |
||
conf.fromLang = t; |
conf.fromLang = t; |
||
$.cookie('fromLang', t); |
$.cookie('fromLang', t); |
||
$(' |
$('#translator-from').text(t); |
||
}); |
}); |
||
// disable enter on them |
// disable enter on them |
||
$(' |
$('#translator-from, #translator-to').keypress(function (e) { |
||
if (e.which !== 13) return; |
if (e.which !== 13) return; |
||
e.target.blur(); |
|||
$(this).blur(); |
|||
e.preventDefault(); |
e.preventDefault(); |
||
}).click(function () { |
}).click(function () { |
||
document.execCommand('selectAll', false, null); |
document.execCommand('selectAll', false, null); |
||
}).focusout( |
}).focusout(saveLanguageConfigs); |
||
$(' |
$('#enableTemplateTranslation').attr('checked', conf.enableTemplateTranslation).click(function () { |
||
conf.enableTemplateTranslation = |
conf.enableTemplateTranslation = this.checked; |
||
}); |
}); |
||
$(' |
$('#removeLinksAliases').attr('checked', conf.removeLinksAliases).click(function () { |
||
conf.removeLinksAliases = |
conf.removeLinksAliases = this.checked; |
||
}); |
}); |
||
}); |
}); |
Revision as of 05:08, 8 September 2024
// <nowiki> DO NOT COPY IT FOR TRANSLATION, READ THE DOCUMENTATION [[:en:User talk:Ebraminio/ArticleTranslator.js]]
/*global jQuery, mediaWiki*/
(function ($, mw) {
'use strict';
// prevent double execution of the tool
if (window.isArticletranslatorInitialized && location.hash === '#tofawikiframe') { return; }
window.isArticletranslatorInitialized = true;
var action = mw.config.get('wgAction');
if (action === 'history') {
return;
}
var conf = {
homeWiki: 'hy',
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: ['Flag icon', 'flagicon', 'Flag', '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://tools.wmflabs.org/linkstranslator/', request).then(null, function () {
return $.post('https://fa-tts.wmflabs.org/linkstranslator/', request);
});
}
function hasNotPersianCharacter(x) {
return !/[كﮑﮐﮏﮎﻜﻛﻚﻙىﻯيہەھﻰ-ﻴً-ِْٰء-ٕپچژگکكڪﻙﻚیﻱﻲٔ۱۲۳۴۵۶۷۸۹۰]/.test(x);
}
function editboxTranslator() {
var translationTextArea;
if (conf.fromLang === mw.config.get('wgPageContentLanguage')) {
$('#wpTextbox2').remove();
translationTextArea = $('#wpTextbox1').clone().attr({
'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 === 'hy') {
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 () {
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 === 'hy') {
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 linkFromWikiLang(page, lang) {
if (lang === 'imdb')
return 'http://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);
}
var wgUserLanguage = mw.config.get('wgUserLanguage');
function showTables(result) {
var missings = result['#missings'];
// Red nodes inserted after the missing pages
Object.keys(missings).map(function (page) {
if (!missings[page].langlinks) return;
$('#bodyContent a[href$="' + mw.util.getUrl(page) + '"]').after(
$('<span>', {
'class': 'linkstranslator-added-content'
}).append('(', $('<bdi>', {
style: 'color: red',
'class': 'translatorNeededLink'
}).text(missings[page].langlinks), ')')
);
});
// 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';
$('#translatorBar').after($('<div>', {
style: 'line-height: 1.25; font-size: 50%;',
id: '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(wgUserLanguage)),
$('<td>').text(x[2].toLocaleString(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 viewTranslator(showMissings) {
$('.linkstranslator-added-content, #linkstranslator-missings-wrapper').remove();
$('#translator-button, #translator-plus').css('color', 'lightgray');
var titles = {};
$('#bodyContent a').get().forEach(function (x) {
var href = x.getAttribute('href');
if (!href || href.indexOf('/wiki/') === -1) return;
var title = decodeURI(href.replace(/.*?\/wiki\//, ''))
.replace(/_/g, ' ').replace(/#.*$/g, '');
if (title) {
titles[title] = true;
}
});
titles = Object.keys(titles);
translate(titles, showMissings).then(function (result) {
$('.linkstranslator-added-content, #linkstranslator-missings-wrapper').remove();
$('#translator-button, #translator-plus').css('color', '#808b96');
Object.keys(result).forEach(function (from) {
if (from.indexOf('#') === 0) return;
var to = result[from];
$('#bodyContent a[href$="' + mw.util.getUrl(from) + '"]').after(
$('<span>', {
'class': 'linkstranslator-added-content'
}).append('(', $('<a>', {
href: '#',
'class': 'linkstranslator-link-copytext',
style: 'font-size: 140%; font-weight: normal; line-height: 0;',
title: 'Copy to clipboard',
text: '⎘'
}), ' ', $('<bdi>').append(
$('<a>', {
lang: conf.homeWiki,
href: linkFromWikiLang(to, conf.homeWiki),
text: to
})
), ')')
);
});
$('#bodyContent').after($('<textarea id="linkstranslator-link-textarea"></textarea>').css({
// http://stackoverflow.com/a/30810322
position: 'fixed', top: 0, right: '-2em', width: '2em', height: '2em',
padding: 0, border: 'none', outline: 'none', boxShadow: 'none',
background: 'transparent'
}));
$('.linkstranslator-link-copytext').click(function (event) {
event.preventDefault();
var el = event.target;
var text = el.nextElementSibling.firstChild.innerText;
if (location.hash === '#tofawikiframe')
text = '[[' + text + ']]';
$('#linkstranslator-link-textarea').val(text)[0].select();
var success = false;
try { success = document.execCommand('copy'); } catch (e) { }
if (!success) return;
$(el).animate({ fontSize: '200%' }, 20)
.animate({ fontSize: '140%' }, 200);
});
if (showMissings) showTables(result);
if (result['#debug']) console.log('Server debug:', result['#debug']);
});
}
function saveLanguageConfigs() {
if ($('#translator-from').text().trim() === '') {
$('#translator-from').text(conf.fromLang || 'hy');
}
conf.fromLang = $('#translator-from').text();
$.cookie('fromLang', conf.fromLang);
if ($('#translator-to').text().trim() === '') {
$('#translator-to').text(conf.homeWiki || 'en');
}
conf.homeWiki = $('#translator-to').text();
$.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'));
}
h1.append($('<span>', {
style: 'font-size: 40%; margin: 0 2em; display: inline-block',
id: 'translatorBar',
'class': 'noprint'
}).append(
$('<span contenteditable>'),
conf.translatorBarFormat
.replace('$1', '<sub>' + (action === 'view' ? '<a id="translator-equ" href="#">=</a> <a id="translator-equ-play" href="#">▶️</a> <a id="translator-equ-frovo" target="_blank" href="https://forvo.com/search/' + encodeURI(mw.config.get('wgTitle')) + '">f</a> ' : '') + '<span id="translator-equ-links"></span></sub><a id="translator-button" href="#">')
.replace('$2', '</a>' + (action === 'view' ? '<sup><a id="translator-plus" href="#">+</a></sup>' : ''))
.replace('$3', '<span id="translator-from" spellcheck="false" style="display: inline-block" contenteditable>' + conf.fromLang + '</span>')
.replace('$4', '<span id="translator-to" spellcheck="false" style="display: inline-block" contenteditable>' + conf.homeWiki + '</span>')
.replace('$5', '<a id="translator-switch" href="#">⇄</a>')
));
if (action === 'edit' || action === 'submit') {
$('#translatorBar').append(
' ',
$('<input>', { type: 'checkbox', name: 'enableTemplateTranslation', id: 'enableTemplateTranslation' }),
$('<label>', { 'for': 'enableTemplateTranslation', text: conf.templateTranslatorText }),
' ',
$('<input>', { type: 'checkbox', name: 'removeLinksAliases', id: 'removeLinksAliases' }),
$('<label>', { 'for': 'removeLinksAliases', text: conf.removeLinksAliasesText })
);
}
$('#translator-button, #translator-plus').click(function (event) {
event.preventDefault();
saveLanguageConfigs();
if (action === 'edit' || action === 'submit') {
editboxTranslator();
} else {
viewTranslator(this.id === 'translator-plus');
}
});
// run translator if is in tofawiki context
if (window.self !== window.top && location.hash === '#tofawikiframe') {
viewTranslator(false);
$('#mw-page-base, #mw-head-base, #mw-navigation, #firstHeading, #siteSub, #footer, .read-more-container').remove();
$('#content').css('margin-left', 0).css('padding-top', 0);
importStylesheet('MediaWiki:Print.css');
}
$('#translator-equ').click(function (event) {
event.preventDefault();
var title = mw.config.get('wgTitle');
$('#translator-equ-links').empty().append(
$('<a>', { target: '_blank', href: 'https://translate.google.com/?sl=' + conf.fromLang + '&tl=' + conf.homeWiki + '&q=' + title, text: 'Translator' }),
' / ',
$('<a>', { target: '_blank', href: 'https://www.google.com/search?q="' + encodeURI(title) + '"&lr=lang_' + conf.homeWiki, text: 'Specific Language Search' }),
' / ',
$('<a>', { target: '_blank', href: 'https://www.bing.com/translator', text: 'Bing Translator' })
);
});
$('#translator-equ-play').click(function (e) {
e.preventDefault();
new Audio('https://dls-espeakbox.wmflabs.org/speech?speed=100&text=' + encodeURI(mw.config.get('wgTitle'))).play();
});
// hide play button on non-English wiki
mw.config.get('wgContentLanguage') !== 'en' && $('#translator-equ-play, #translator-equ-frovo').hide();
$('#translator-switch').click(function (event) {
event.preventDefault();
var t = conf.homeWiki;
conf.homeWiki = conf.fromLang;
$.cookie('homeWiki', conf.fromLang);
$('#translator-to').text(conf.fromLang);
conf.fromLang = t;
$.cookie('fromLang', t);
$('#translator-from').text(t);
});
// disable enter on them
$('#translator-from, #translator-to').keypress(function (e) {
if (e.which !== 13) return;
$(this).blur();
e.preventDefault();
}).click(function () {
document.execCommand('selectAll', false, null);
}).focusout(saveLanguageConfigs);
$('#enableTemplateTranslation').attr('checked', conf.enableTemplateTranslation).click(function () {
conf.enableTemplateTranslation = this.checked;
});
$('#removeLinksAliases').attr('checked', conf.removeLinksAliases).click(function () {
conf.removeLinksAliases = this.checked;
});
});
}(jQuery, mediaWiki));