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.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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);