Created
September 1, 2022 07:25
-
-
Save iffa/15cc575184a2b9725848f9026a258e7a to your computer and use it in GitHub Desktop.
Utility component for keeping the "lang" attribute of the document up to date with current user locale (Next.js/React)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useLocale } from "next-intl"; | |
import { useEffect } from "react"; | |
/** | |
* Utility component for keeping the lang-attribute of the document up to date. | |
* For Next.js, include this in e.g. custom App component. | |
*/ | |
export const HtmlLangProvider = () => { | |
// Using next-intl here but realistically can be anything | |
const locale = useLocale(); | |
useEffect(() => { | |
if (typeof document !== "undefined") { | |
document.documentElement.lang = locale; | |
} | |
}, [locale]); | |
return null; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment