Documentation
Get and change the locale

Get and change the locale

Export useChangeLocale and useCurrentLocale from createI18n:

// locales/index.ts
export const {
  useChangeLocale,
  useCurrentLocale,
  ...
} = createI18n({
  ...
})

Then use it as a hook:

import { useChangeLocale, useCurrentLocale } from '../locales'
 
export default function Page() {
  const changeLocale = useChangeLocale()
  const locale = useCurrentLocale()
 
  return (
    <>
      <p>Current locale: {locale}</p>
      <button onClick={() => changeLocale('en')}>English</button>
      <button onClick={() => changeLocale('fr')}>French</button>
    </>
  )
}