Google Translate Auto Language Enhanced

Automatically set the target language to English based on detected source language changes and auto-trigger translation if page language differs from browser language.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Google Translate Auto Language Enhanced
// @namespace   https://greasyfork.org/en/users/1030895-universedev
// @version     2.5
// @author      UniverseDev
// @license     GPL-3.0-or-later
// @description Automatically set the target language to English based on detected source language changes and auto-trigger translation if page language differs from browser language.
// @match       *://translate.google.*/*
// @noframes
// ==/UserScript==

"use strict";

const targetLang = "en";
const browserLang = navigator.language.split('-')[0];

function setTargetLanguage() {
    const selector = `[data-language-code="${targetLang}"]`;
    const targetTab = document.querySelector(selector);
    if (targetTab && targetTab.getAttribute("aria-selected") !== "true") {
        targetTab.click();
    }
}

function observeSourceLanguage() {
    const sourceLangTab = document.querySelector("[role=tablist] [aria-selected='true']");
    if (sourceLangTab) {
        const observer = new MutationObserver(() => {
            setTargetLanguage();
        });
        observer.observe(sourceLangTab, { characterData: true, subtree: true });
    }
}

function detectPageLanguage() {
    const pageLang = document.documentElement.lang || document.querySelector("html").getAttribute("lang");
    if (pageLang && pageLang !== browserLang) {
        console.log(`Detected page language (${pageLang}) differs from browser language (${browserLang}). Triggering translation.`);
        setTargetLanguage();
    }
}

function init() {
    setTargetLanguage();
    observeSourceLanguage();
    detectPageLanguage();
}

window.addEventListener("load", init);