Jump to content

MediaWiki:Gadget-TranslateTagger.js

From wikiNonStop

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* This function adds a custom "TranslateTagger" button to the editing toolbar.
When clicked, it takes the current wikitext from the editor, sends it to the
TranslateTagger API for adding translation tags, and replaces the editor content 
with the updated wikitext. */

(function () {
    function addTranslateTaggerButton() {
      const previewButton = document.querySelector('.tool[rel="realtimepreview"]');
  
      if (!previewButton || document.querySelector('#translateTaggerButton')) return;
  
      const newButton = previewButton.cloneNode(true);
      const label = newButton.querySelector('.oo-ui-labelElement-label');
      label.textContent = "TranslateTagger";
  
      const icon = newButton.querySelector('.oo-ui-iconElement-icon');
      if (icon) icon.className = "oo-ui-iconElement-icon oo-ui-icon-code";
  
      newButton.id = "translateTaggerButton";
      newButton.addEventListener("click", function () {
        const textarea = document.querySelector('#wpTextbox1');
        if (!textarea) {
          console.error("Wikitext textarea not found.");
          return;
        }
  
        const wikitext = textarea.value;
        if (!wikitext.trim()) {
          alert("The editor is empty.");
          return;
        }
  
        fetch("https://translatetagger.toolforge.org/api/convert", {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ wikitext: wikitext })
        })
        .then(response => response.json())
        .then(data => {
            const translated = data.translated_wikitext || data.converted;
            if (!translated) {
              alert("Could not get translated wikitext.");
              return;
            }
          
            textarea.value = translated;
            alert("Wikitext updated with translation tags. Showing changes...");
          
            // Trigger the "Show changes" button
            const diffButton = document.querySelector('#wpDiff');
            if (diffButton) {
              diffButton.click();
            } else {
              console.warn("Diff button (#wpDiff) not found.");
            }
          })
          
        .catch(err => {
          console.error("TranslateTagger error:", err);
          alert("Failed to contact TranslateTagger API.");
        });
      });
  
      previewButton.parentNode.insertBefore(newButton, previewButton);
    }
  
    // Wait until toolbar is loaded
    $(document).ready(function () {
      mw.hook('wikipage.editform').add(() => {
        setTimeout(addTranslateTaggerButton, 500); // Slight delay to ensure toolbar is ready
      });
    });
  })();