如何在 HTML 中正确使用 lang

How to properly use lang in HTML

我想问你如何在HTML代码中正确使用lang。 我正在尝试使用 2 langs en, pl.

建立网站
<html lang="en">
<html lang="pl">

这种方式正确吗?

同一文档中不能有两个 <html> 标签。您应该使用一个不带 lang 属性的 <html> 标签,并在仅包含一种语言的标签上使用 lang 属性。这是一个例子:

<html>
<head>
    <!--some elements in the head-->
</head>
<body>
    <p lang="en">This is text</p>
    <p lang="fr">Ceci est du texte</p>
</body>
</html>

如果文档的 主要 语言是波兰语,大部分是英语,那么它应该有 <html lang="pl"><div lang="en"> 或英语部分的一些东西:

<html lang="pl">
…
<body>
[Polish content]
    <div lang="en">
        [English content]
    </div>
[more Polish content]
    <div lang="en">
        [more English content]
    </div>
[more Polish content]
…

尽管 html 元素应尽可能始终具有 lang 值,per the HTML spec:

Authors are encouraged to specify a lang attribute on the root html element, giving the document's language. This aids speech synthesis tools to determine what pronunciations to use, translation tools to determine what rules to use, and so forth.

但是,如果一份文件是多种语言的混合体,以至于不能真正将其视为具有单一的主要语言,then the html element still should have a lang attribute, but with an empty value:

Setting the attribute to the empty string indicates that the primary language is unknown.

<html lang="">
…
<body>
    <div lang="pl">
        [Polish content]
    </div>
    <div lang="en">
        [English content]
    </div>
    <div lang="pl">
        [Polish content]
    </div>
    <div lang="en">
        [English content]
    </div>
    …

有关这方面的更多详细信息,请参阅以下 W3C 指南: