MediaWiki:Inciskin/JS/Traductor tuvalí.js

De Inciclopedia
Ir a la navegación Ir a la búsqueda

Nota: Después de publicar, quizás necesite actualizar la caché de su navegador para ver los cambios.

  • Firefox/Safari: Mantenga presionada la tecla Shift mientras pulsa el botón Actualizar, o presiona Ctrl+F5 o Ctrl+R (⌘+R en Mac)
  • Google Chrome: presione Ctrl+Shift+R (⌘+Shift+R en Mac)
  • Internet Explorer/Edge: mantenga presionada Ctrl mientras pulsa Actualizar, o presione Ctrl+F5
  • Opera: Presiona Ctrl+F5.
$('#query').submit(function (evt) {
    evt.preventDefault();
});
function translate(input) {
    var split = input.split(" ");
    var result = [];
    var nword;
    var word;
    for (var i = 0; i < split.length; i++) {
        word = split[i];
        if (word.length < 2) {
            result.push(word);
        } else {
            symbol = "";
            while (word.length > 0 && word[word.length-1].toLowerCase() == word[word.length-1].toUpperCase()) {
                symbol = symbol + word[word.length - 1];
                word = word.substring(0, word.length - 1);
            }
            if (word.length == 0) {
            	result.push(symbol);
            	continue;
            }
            nword = word.substring(word.length - 1, word.length);
            if (!['a', 'e', 'i', 'o', 'u'].includes(word[word.length-1]) || word[0] == word[word.length - 1]) {
                nword = nword + '\'';
            }
            nword = nword + word.substring(0, word.length - 1) + symbol;
            result.push(nword);
        }
    }
    return result.join(" ");
}

function reverse(input) {
	var split = input.split(" ");
    var result = [];
    for (var i = 0; i < split.length; i++) {
    	word = split[i];
		if (word.length < 2) {
			result.push(word);
		} else {
			symbol = "";
            while (word.length > 0 && word[word.length-1].toLowerCase() == word[word.length-1].toUpperCase()) {
                symbol = symbol + word[word.length - 1];
                word = word.substring(0, word.length - 1);
            }
            if (word.length == 0) {
            	result.push(symbol);
            	continue;
            }
			word = word.replace("'", "");
			result.push(word.substring(1, word.length) + word[0] + symbol);
		}
    }
    return result.join(" ");
}

let input_el = document.querySelector('#query > input[name="search"]');
let textarea_el = document.createElement('textarea');
input_el.getAttributeNames().forEach( attrName => {
    textarea_el.setAttribute( attrName, 
      (document.createAttribute( attrName ).value =            
       input_el.getAttribute( attrName ) )
    );
});
textarea_el.setAttribute('rows', '10');
textarea_el.setAttribute('cols', '40');
input_el.replaceWith(textarea_el);

$("input[value='Traducir a tuvalí']").off().on("click", () => {
	msg = $("#query > textarea")[0].value;
	$("#resultado").html(translate(msg));
});

$("input[value='Traducir a español']").off().on("click", () => {
	msg = $("#query > textarea")[0].value;
	$("#resultado").html(reverse(msg));
});