Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use lang in HTML

Tags:

html

lang

I want to ask you how to properly use lang in HTML code. Im trying to have website in 2 langs en, pl.

<html lang="en">
<html lang="pl">

Is this way correct?

like image 659
Martin Joe Avatar asked Nov 27 '16 11:11

Martin Joe


1 Answers

If the document’s primary language is in, say, Polish with large parts that are in English, then it should have <html lang="pl"> with <div lang="en"> or something around the English parts:

<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]
…

Regardless the html element should if possible always have a lang value, 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.

But if a document is such a mix of languages that it can’t really be seen as having a single primary language, 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>
    …

For more detailed information on this, see the following W3C guides:

  • Declaring the overall language of a page
  • Choosing language tags
like image 169
4 revs Avatar answered Sep 25 '22 09:09

4 revs