我应该为混合语言页面使用什么 <html lang=""> 属性值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7076837/
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 10:05:47  来源:igfitidea点击:

What <html lang=""> attribute value should I use for a mixed language page?

htmllang

提问by Francisc

I usually use this: <html lang="en">.

我通常使用这个:<html lang="en">

However, I am working on a website that will use two languages and mix them up sometimes in the same sentence or heading.

但是,我正在开发一个网站,该网站将使用两种语言,有时将它们混合在同一个句子或标题中。

How would the above code look in this case? Can I use <html lang="lang1 lang2">?

在这种情况下,上面的代码看起来如何?我可以使用<html lang="lang1 lang2">吗?

回答by Jamie Dixon

As far as I can tell from reading the HTML5 spec the langattribute:

据我从阅读 HTML5 规范中了解到的lang属性:

value must be a valid BCP 47 language tag, or the empty string

值必须是有效的 BCP 47 语言标签,或空字符串

Source: http://www.w3.org/TR/html5/dom.html#the-lang-and-xml:lang-attributes

来源:http: //www.w3.org/TR/html5/dom.html#the-lang-and-xml: lang-attributes

There's no mention in the spec of an array of language strings and every example I've found uses a single language string.

规范中没有提到语言字符串数组,我发现的每个示例都使用单个语言字符串。

This makes sense since really a given section can only be in one language unless we're creating a new hybrid language.

这是有道理的,因为除非我们正在创建一种新的混合语言,否则给定的部分实际上只能使用一种语言。

Since the lang attribute is valid on all HTML elements you can wrap your language specific code in a new tag in order to indicate its language.

由于 lang 属性对所有 HTML 元素都有效,因此您可以将特定于语言的代码包装在新标签中以指示其语言。

<html lang="en">
[...]
<body>
<h1>I am a heading <span lang="de-DE">Eine überschrift</span></h1>
</body>
</html>

回答by user11448

As I understand it you should be able to use <html lang="mul">to indicate Multiple languages.

据我了解,您应该能够使用<html lang="mul">来表示多种语言

Choose subtags from the IANA Language Subtag Registry.

从 IANA 语言子标签注册表中选择子标签。

Source; https://www.w3.org/TR/2007/NOTE-i18n-html-tech-lang-20070412/#ri20030112.224623362

来源; https://www.w3.org/TR/2007/NOTE-i18n-html-tech-lang-20070412/#ri20030112.224623362

There is a subtag in the list named Subtag: mul

列表中有一个名为Subtag的子标签:mul

Source: http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry

来源:http: //www.iana.org/assignments/language-subtag-registry/language-subtag-registry

However I don't think you will be able to specify exactly which languages you're mixing in the html element. However, as Jamie wrote, you can specify different lang attributes for different elements at the page.

但是,我认为您无法在 html 元素中准确指定要混合的语言。但是,正如 Jamie 所写,您可以为页面上的不同元素指定不同的 lang 属性。

There do exist four special language codes within ISO 639-3 and all of them are also valid within the IANA subtag registry; https://en.wikipedia.org/wiki/ISO_639-3#Special_codes

ISO 639-3 中确实存在四种特殊语言代码,并且所有这些代码在 IANA 子标签注册中也有效; https://en.wikipedia.org/wiki/ISO_639-3#Special_codes

However, I doubt this have good support from search engines as Google.

但是,我怀疑这是否得到了 Google 等搜索引擎的良好支持。

回答by Improv

Adding this answer in April 2020 to provide the latest guidance from the W3C (W3.org) ...

在 2020 年 4 月添加此答案以提供 W3C ( W3.org)的最新指南...

Firstly, no, you cannot use <html lang="lang1 lang2">since it will not validate properly. This is the result when validating via the W3's Nu Html Checkerwith more than one language (English and Swahili) in the language attribute of the htmltag. This error will result with or without comma(s):

首先,不,您不能使用,<html lang="lang1 lang2">因为它无法正确验证。这是通过W3 的 Nu Html Checkerhtml标签的语言属性中使用一种以上语言(英语和斯瓦希里语)进行验证时的结果。无论是否使用逗号都会导致此错误:

Error: Bad value en frfor attribute langon element html: The language subtag en swhis not a valid language subtag.

<html lang="en swh">?</html>

错误:元素上的en fr属性值错误:语言子标签不是有效的语言子标签。langhtmlen swh

<html lang="en swh">?</html>

Below is the latest based on the W3C's Declaring language in HTMLif you want to declare the language of polyglot web pages with more than one language:

如果您想用一种以上的语言声明多语言网页的语言,以下是基于 W3C 的HTML 声明语言的最新版本:

QUICK ANSWER

Always use a language attribute on the htmltag to declare the default language of the text in the page. When the page contains content in another language, add a language attribute to an element surrounding that content.

Use the langattribute for pages served as HTML, and the xml:langattribute > for pages served as XML. For XHTML 1.x and HTML5 polyglot documents, use both together.

Use language tags from the IANA Language Subtag Registry. You can find subtags using > the unofficial Language Subtag Lookuptool.

Use nested elements to take care of content and attribute values on the same element that are in different languages.

快速回答

始终使用html标签上的语言属性来声明页面中文本的默认语言。当页面包含另一种语言的内容时,向围绕该内容的元素添加语言属性。

lang对作为 HTML 的页面使用属性,对xml:lang作为 XML 的页面使用属性 >。对于 XHTML 1.x 和 HTML5 多语言文档,将两者一起使用。

使用来自IANA 语言子标签注册表 的语言标签。您可以使用 > 非官方语言子标签查找工具来查找子标签。

使用嵌套元素处理不同语言的同一元素上的内容和属性值。

What if element content and attribute values are in different languages?

如果元素内容和属性值使用不同的语言怎么办?

In the image below from the W3C's site, the link text shows the language of the target page (Spanish) using the language of the target page ("Espa?ol"), but an associated title attribute contains a hint in the language of the current page ("Spanish" in English):

在 W3C 网站的下图中,链接文本使用目标页面的语言(“Espa?ol”)显示了目标页面的语言(西班牙语),但相关的标题属性包含使用目标页面语言的提示当前页面(英语中的“西班牙语”):

enter image description here

在此处输入图片说明

The markup for the above should look like follows, where the spanelement inherits the default ensetting of the htmlelement:

上述标记应如下所示,其中span元素继承元素的默认en设置html

<span title="Spanish"><a lang="es" href="qa-html-language-declarations.es">Espa?ol</a></span>

What if there's no element to hang your attribute on?

如果没有元素可以挂起您的属性怎么办?

If you want to specify the language of some content but there is no markup around it, use an element such as spanor divaround the content. Here is an example:

如果要指定某些内容的语言但周围没有标记,请使用诸如spandiv内容周围的元素。下面是一个例子:

<p>You'd say that in Chinese as <span lang="zh-Hans">china科学院文献情报中心</span>.</p>

How can you specify metadata for more than one audience language?

如何为多种受众语言指定元数据?

Get the server to send the information in the HTTP Content-Language header. If your intended audience speaks more than one language, the HTTP header allows you to use a comma-separated list of languages.

获取服务器发送 HTTP Content-Language 标头中的信息。如果您的目标受众使用多种语言,则 HTTP 标头允许您使用逗号分隔的语言列表。

Here is an example of an HTTP header that declares the resource to be a mixture of English, Hindi and Punjabi:

下面是一个 HTTP 标头示例,它声明资源是英语、印地语和旁遮普语的混合:

Content-Language: en, hi, pa

Note that this approach is not effective if your page is accessed from a hard drive, disk or other non-server based location. There is currently no widely recognized way of using this kind of metadata inside the page.

请注意,如果您的页面是从硬盘驱动器、磁盘或其他非基于服务器的位置访问的,则此方法无效。目前还没有广泛认可的方式在页面内使用这种元数据。

In the past, many people used a metaelement with the http-equivattribute set to Content-Language. Due to long-standing confusion and inconsistent implementations of this element, the HTML5 specification made this non-conforming in HTML, so you should no longer use it.

过去,很多人使用属性设置为的meta元素。由于此元素的长期混淆和不一致的实现,HTML5 规范使此元素在 HTML 中不符合标准,因此您不应再使用它。http-equivContent-Language

See these links for the details:

有关详细信息,请参阅这些链接:

回答by Atif Anwar

You can use like this.

你可以这样使用。

For HTML 5 use:

对于 HTML 5 使用:

Please see 'Declaring character encoding in HTML' for more information about character encoding.

有关字符编码的更多信息,请参阅“在 HTML 中声明字符编码”。