某些语言不翻译

Notranslate for some languages

我有一个手动翻译成五种语言的网站,添加了选择首选语言的按钮。

我的问题是有时 Chrome 会为用户的语言提供 Translate this page 选项,或者根据设置自动翻译它。

并且由于 $_SERVER['HTTP_ACCEPT_LANGUAGE'] 并不总是 100% 可靠(如果根本没有丢失的话),可能会发生这样的情况,即打开我的英文版网站的用户(假设是意大利用户)会发现不太准确的 google 翻译页面,而不是网站的 "official" 意大利语版本。

此外,使用意大利语 HTTP_ACCEPT_LANGUAGE 的用户可能出于任何原因希望以另一种语言查看该网站,看到 Google 非常烦人每次更改页面时都会弹出(即使您可以Disable it for this website)。

所以我 found 这些解决方案:<html lang="en" translate="no">(由于某种原因不起作用)和 <meta name="google" content="notranslate">(有效)。

问题是他们阻止 Google 翻译成 任何 语言,包括我网站中未包含的语言。

那么,有没有办法阻止 Google(或其他翻译人员)建议翻译 of/automatically 翻译 我的页面只用某些语言?

我建议在 server-side 上嗅探接受的语言,然后动态添加 header 以防用户使用您 "natively" 在您的网站上支持的语言之一。

<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的语言检测:

Chrome 使用 so-called "CLD"(紧凑型语言检测器,现在是版本 3),它使用神经网络扫描网页上的文本以确定最有可能使用哪种语言在呈现的网页上使用。整个过程是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