phphtmlgoogle-chromegoogle-translatehttp-accept-language

Notranslate for some languages


I have a website which I manually translated in five languages, adding the button to choose the preferred one.

My problem is that sometimes Chrome offers a Translate this page option for the user's language or, depending on the settings, automatically translates it.

And since $_SERVER['HTTP_ACCEPT_LANGUAGE'] is not always 100% reliable (if not missing at all), it could happen that an user (let's say an Italian user) who opens my website in the English version, finds a non-so-accurate-google-translated page instead of the "official" italian version of the website.

Furthermore, an user with an Italian HTTP_ACCEPT_LANGUAGE could want to see the website in another language for any reason, and it's quite annoying seeing Google's popup every time you change page (even if you can Disable it for this website).

So I found these solutions: <html lang="en" translate="no"> (which does not work for some reason) and <meta name="google" content="notranslate"> (which works).

The problem is that they prevent Google from translating in ANY language, comprehending the ones not included inside my website.

So, is there a way to prevent Google (or other translators) from proposing the translation of/automatically translate my page only in some languages?


Solution

  • I would recommend to sniff the accepted languages on the server-side, then add the header dynamically in case the user uses one of the languages you "natively" support on your site.

    <html>
    <head>
    <?php
        $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
        $acceptLang = ['it', 'en', 'fr']; // your list of supported languages
        if (in_array($lang, $acceptLang)) {
    ?>
    <meta name="google" content="notranslate">
    <?php    
        }
    
    ?>
    </head>
    

    Chrome's language detection:

    Chrome uses a so-called "CLD" (Compact Language Detector, now in version 3) which scans text on a web page using a neural network to determine which language is most likely used on the rendered web page. The entire process is well documented for CLD V2 if you want to understand the entire process. If a web page does not translate in Chrome this is most likely due to Chrome not being able to properly identify the language based on the information found on the page. A possible work-around to support easier identification is to add a hidden text block containing enough text in the language of your page at the start of the page, at least it is a recommendation taken from this SO answer.