I'm working on a multi-language statically built (next export) Next.js site. I can't use next internationalization so I've had to hack together a solution, mostly based on this nextjs-18n-static-page-starter
My page structure looks like this:
pages
┣ [lang]
┃ ┣ category
┃ ┃ ┣ [slug].js
┃ ┃ ┗ index.js
┃ ┗ index.js
┣ _app.js
┣ _document.js
┗ index.js
I would like to set the html lang attribute in a custom document. Is there any way to dynamically pull this from the url at the document level?
I have this hacky solution going, but it doesn't update when the user changes their language in the app (which does correctly redirect to a new url):
export default class MyDocument extends Document {
render() {
const queryLang = this?.props?.__NEXT_DATA__?.query?.lang;
const lang = ( queryLang === null || queryLang === undefined ) ? DEFAULT_LANG : queryLang;
return (
<Html lang={lang}>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}