Html 为什么 Chrome 会错误地确定页面使用不同的语言并提供翻译?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2467454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:31:01  来源:igfitidea点击:

Why does Chrome incorrectly determine page is in a different language and offer to translate?

htmllocalizationinternationalizationgoogle-chrome

提问by Samuel Neff

The new Google Chrome auto-translation feature is tripping up on one page within one of our applications. Whenever we navigate to this particular page, Chrome tells us the page is in Danish and offers to translate. The page is in English, just like every other page in our app. This particular page is an internal testing page that has a few dozen form fields with English labels. I have no idea why Chrome thinks this page is Danish.

新的 Google Chrome 自动翻译功能在我们的一个应用程序中的一页上出错。每当我们导航到这个特定页面时,Chrome 都会告诉我们该页面是丹麦语并提供翻译。该页面是英文的,就像我们应用程序中的所有其他页面一样。这个特定的页面是一个内部测试页面,有几十个带有英文标签的表单域。我不知道为什么 Chrome 认为这个页面是丹麦语。

Does anyone have insights into how this language detection feature works and how I can determine what is causing Chrome to think the page is in Danish?

有没有人了解此语言检测功能的工作原理以及我如何确定导致 Chrome 认为该页面是丹麦语的原因?

回答by Kyle

Update: according to Google

更新:根据谷歌

We don't use any code-level language information such as lang attributes.

我们不使用任何代码级语言信息,例如 lang 属性。

They recommend you make it obvious what your site's language is. Use the following which seems to help although Content-Languageis deprecated and Google says they ignore lang

他们建议您明确说明您网站的语言是什么。使用以下似乎有帮助的方法虽然Content-Language已被弃用,但 Google 表示他们忽略了lang

<html lang="en" xml:lang="en" xmlns= "http://www.w3.org/1999/xhtml">
<meta charset="UTF-8">
<meta name="google" content="">
<meta http-equiv="Content-Language" content="en">

If that doesn't work, you can always place a bunch of text (your "About" page for instance) in a hidden div. That might help with SEO as well.

如果这不起作用,您始终可以在隐藏的 div 中放置一堆文本(例如您的“关于”页面)。这也可能有助于 SEO。

EDIT (and more info)

编辑(和更多信息)

The OP is asking about Chrome, so Google's recommendationis posted above. There are generally three ways to accomplish this for other browsers:

OP 正在询问 Chrome,因此上面发布了Google 的建议。对于其他浏览器,通常有三种方法可以实现这一点:

  1. W3C recommendation: Use the langand/or xml:langattributes in the html tag:

    <html lang="en" xml:lang="en" xmlns= "http://www.w3.org/1999/xhtml">
    
  2. UPDATE: previously a Google recommendationnow deprecated specalthough it may still help with Chrome. : meta http-equiv(as described above):

    <meta http-equiv="Content-Language" content="en">
    
  3. Use HTTP headers (not recommended based on cross-browser recognition tests):

    HTTP/1.1 200 OK
    Date: Wed, 05 Nov 2003 10:46:04 GMT
    Content-Type: text/html; charset=iso-8859-1
    Content-Language: en
    
  1. W3C 建议:在 html 标签中使用lang和/或xml:lang属性:

    <html lang="en" xml:lang="en" xmlns= "http://www.w3.org/1999/xhtml">
    
  2. 更新:以前的 Google 推荐现在已弃用规范,尽管它可能仍然对 Chrome 有帮助。:(meta http-equiv如上所述):

    <meta http-equiv="Content-Language" content="en">
    
  3. 使用 HTTP 标头(不推荐基于跨浏览器识别测试):

    HTTP/1.1 200 OK
    Date: Wed, 05 Nov 2003 10:46:04 GMT
    Content-Type: text/html; charset=iso-8859-1
    Content-Language: en
    

Exit Chrome completely and restart it to ensure the change is detected. Chrome doesn't always pick up the new meta tag on tab refresh.

完全退出 Chrome 并重新启动它以确保检测到更改。Chrome 并不总是在选项卡刷新时选择新的元标记。

回答by Chris Broski

I added lang="en"to the doctype declaration, added meta tags for charset utf-8 and Content-Langauge in the HTML header, specified charset as utf-8 and Content-Language as enin the HTTP response headers and it did nothing to stop Chrome from declaring my page was in Portuguese. The only thing that fixed the problem was adding this to the HTML header:

我添加lang="en"到 doctype 声明中,在 HTML 标头中添加了字符集 utf-8 和 Content-Langauge 的元标记,将字符集指定为 utf-8,将 Content-Language 指定为enHTTP 响应标头,但它没有阻止 Chrome 声明我的页面是葡萄牙语。解决问题的唯一方法是将其添加到 HTML 标头中:

<meta name="google" content="">

But now I've prevented users from translating my page that is clearly in English to their own language. Poor job, Chrome. You can be better than this.

但现在我已经阻止用户将我的页面翻译成他们自己的语言。糟糕的工作,铬。你可以比这更好。

回答by NinjaCat

Without knowing what the text was, perhaps the ngram detection is being tricked by the content of your page.

在不知道文本是什么的情况下,ngram 检测可能被您的页面内容所欺骗。

http://googleresearch.blogspot.com/2006/08/all-our-n-gram-are-belong-to-you.html

http://googleresearch.blogspot.com/2006/08/all-our-n-gram-are-belong-to-you.html

https://en.wikipedia.org/wiki/N-gram

https://en.wikipedia.org/wiki/N-gram

回答by James Revillini

Chromium thinks this page in Filipino: http://www.reyalvarado.com/portfolio/cuba/Notes: There is pretty much no text on the page except for the owner's name and the menu items. Menu items are dynamically replaced with images by FLIR.

Chromium 认为这个页面是菲律宾语:http: //www.reyalvarado.com/portfolio/cuba/注意:页面上几乎没有文字,除了所有者的名字和菜单项。菜单项由 FLIR 动态替换为图像。

The HTML declares the page as US English:

HTML 将页面声明为美国英语:

<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US"> 

回答by mattavatar

Specify the default language for the document, then use the translate attributeand Google's class per element/container, as in:

指定文档的默认语言,然后使用translate 属性每个元素/容器的Google类,如下所示:

<html lang="en">
    ...
    <span><a href="#" translate="no">English</a></span>

Explanation:

解释:

The accepted answer presents a blanket solution, but does not address how to specify the language per element, which can fix the bug and ensure your page remains translatable.

接受的答案提供了一个全面的解决方案,但没有解决如何为每个元素指定语言,这可以修复错误并确保您的页面保持可翻译

Why is this better? This will cooperate with Google's internationalization versus shut it off. Referring back to the OP:

为什么这样更好?这将与谷歌的国际化合作而不是将其关闭。回顾OP:

Why does Chrome incorrectly determine page is in a different language and offer to translate?

为什么 Chrome 会错误地确定页面使用不同的语言并提供翻译?

Answer: Google is trying to help you with internationalization, but we need to understand why this is failing. Building off of NinjaCat'sanswer, we assume that Google reads and predictsthe language of your website using an N-gram algorithm-- so, we can't say exactly why Google wants to translate your page; we can only assume that:

:Google 正在努力帮助您进行国际化,但我们需要了解为什么会失败。基于NinjaCat 的回答,我们假设 Google使用N-gram 算法读取和预测您网站的语言——因此,我们无法确切说明 Google 想要翻译您的页面的原因;我们只能假设:

  1. There are words on your page that belong to a different language.
  2. Marking the containing element as translate="no"and lang="en"(or removing these words) will help Google to correctly predict the language of your page.
  1. 您的页面上有属于不同语言的字词。
  2. 将包含元素标记为translate="no"lang="en"(或删除这些词)将有助于 Google 正确预测您网页的语言。

Unfortunately, most people reaching this post won't know what words are causing the trouble. Use Chrome's built-in "Translate to English" feature (in the Right-Click context menu) to see what gets translated, you may see unexpected translations like the following:

不幸的是,大多数到达这篇文章的人不知道是什么词导致了问题。使用Chrome的内置“翻译成英文的”功能(右键单击上下文菜单),看看有什么被翻译,你可能会看到意外的翻译像下面这样

enter image description here

在此处输入图片说明

So, update your html with the appropriate translation tags until the Google Translation of your page changes nothing -- then we should expect the popup to go away for future visitors.

因此,使用适当的翻译标签更新您的 html,直到您页面的 Google 翻译没有任何变化——然后我们应该期待弹出窗口对未来的访问者消失。

Won't it be a lot of work to add all these extra tags?Yes, very likely. If you are using Wordpress or another Content Management System then look in their documentationfor quick ways to update your code!

添加所有这些额外的标签会不会有很多工作?是的,很有可能。如果您使用的是 Wordpress 或其他内容管理系统,请查看他们的文档以了解更新代码的快速方法!

回答by Alan

Try including the property xml:lang=""to the <html>, if the other solutions don't work:

尝试包括财产 xml:lang=""<html>,如果其他的解决方案不起作用:

<html class="no-js" lang="pt-BR" dir="ltr" xml:lang="pt-BR">