i18n is overwriting custom HTML lang attribute
See original GitHub issueWhat version of Next.js are you using?
12.0.1
What version of Node.js are you using?
v14.17.3
What browser are you using?
Chrome
What operating system are you using?
macOS
How are you deploying your application?
Vercel
Describe the Bug
I need a custom lang attribute because the language is only the first part of the locale. This works, but after using next router to switch to a different local, Next.js injects the whole locale instead of using my lang props. I would not expect Next.js to overwrite my custom code.
import Document, { Head, Html, Main, NextScript } from 'next/document'
import { ServerStyleSheet } from 'styled-components'
import { splitLocale } from '_utils/locales'
export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const sheet = new ServerStyleSheet()
const originalRenderPage = ctx.renderPage
const [lang = 'en'] = splitLocale(ctx?.locale)
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
sheet.collectStyles(<App {...props} />),
})
const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
lang,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
}
} finally {
sheet.seal()
}
}
render() {
return (
<Html lang={this.props.lang}>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
Expected Behavior
Only overwrite the html lang attribute when there is no custom lang attribute, or an option to disable the behaviour.
To Reproduce
Switch to a different locale with next router.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Next.js route.replace changing custom html lang attribute set ...
I am using Next js with i18n-routing with following next.config.js ... I am overriding html lang attribute to en-IN using _document.js
Read more >NextJS add lang attribute to HTML tag - Daily Dev Tips
There is an alternative way of adding a custom HTML tag if you want to override multiple HTML properties. Inside your pages directory,...
Read more >i18n - Tutorial - Docusaurus
This tutorial will walk you through the basics of the Docusaurus i18n system.
Read more >Configure internationalization (i18n) rewrites | Firebase Hosting
Use internationalization rewrites ("i18n rewrites") to serve different content depending on a user's country or preferred language. Here are some example ...
Read more >Rails Internationalization (I18n) API - Ruby on Rails Guides
To tell the I18n library where it can find your custom translation files you can ... If you want to translate your Rails...
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 FreeTop 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
Top GitHub Comments
That sounds irrelevant/more of a feature request. As the docs state, we do support a standardized version of locales, but thanks for the extra information.
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.