Docs
Preferred Language
Preferred Language
Adapt to user language preferences dynamically
About
The usePreferredLanguage
hook adapts automatically to the user’s preferred language. It reactively returns a string representing the user’s preferred language as set in the browser settings.
Return Values
Name | Type | Description |
---|---|---|
language | string | The hook returns a string that represents the preferred language of the user, as set in the browser settings. |
Installation
Run the following command:
npx scriptkavi-hooks@latest add preferred-language
Usage
import { usePreferredLanguage } from "@/hooks/preferred-language"
import * as React from "react"
export default function App() {
const language = usePreferredLanguage()
return (
<section>
<h1>usePreferredLanguage</h1>
<p>Change language here - chrome://settings/languages</p>
<h2>
The correct date format for <pre>{language}</pre> is{" "}
<time>{new Date(Date.now()).toLocaleDateString(language)}</time>
</h2>
</section>
)
}