Setting <html lang="..."> by default
See original GitHub issueI’m wondering if there is any way to implicitly set the lang
attribute to the html tag. This is a good practice, which improves SEO and a11y.
Currently, my workaround is roughly this:
// ./pages/_document.js
import Document, { Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document {
render() {
return (
<html lang={this.props.__NEXT_DATA__.props.initialLanguage}>
<Head />
<body>
<Main />
<NextScript />
</body>
</html>
)
}
}
If there is no way to inject lang
automatically, shall it be mentioned in the example or plugin docs?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:9
- Comments:44 (30 by maintainers)
Top Results From Across the Web
Declaring language in HTML - W3C
Quick answer. Always use a language attribute on the html tag to declare the default language of the text in the page. This...
Read more >What is the default value <html lang="???"> - Stack Overflow
The lang attribute value of the node or an ancestor. The pragma-set default language. HTTP headers (or details from some other protocol if...
Read more >lang - HTML: HyperText Markup Language - MDN Web Docs
Note: The default value of lang is unknown , therefore it is recommended to always specify this attribute with the appropriate value.
Read more >How to fix: Consider adding a “lang” attribute to the “html” start ...
Always use a language attribute on the <html> tag to declare the default language of the text in the page, using the lang...
Read more >Setting a default language for the HTML tag - Episerver Support
The HTML lang attribute can be used to declare the language of a Web page or a portion of a Web page. This...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ok so a more robust code is as follow:
and in plain JS:
Use that in your custom
_document
getInitialProps
call:and then finally:
Thanks all for your fast answers!
I’ve found out that
i18next-express-middleware
that is applied bynext-i18next-middleware
is attachinglanguage
&languageDirection
onres.locals
.So the code may look like: