Skip to content

Instantly share code, notes, and snippets.

@iffa
Created September 1, 2022 07:25
Show Gist options
  • Save iffa/15cc575184a2b9725848f9026a258e7a to your computer and use it in GitHub Desktop.
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)
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