-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new argument to
useActualLocale
to simplify integration a…
…nd a new `resolveLocale` API
- Loading branch information
1 parent
d3d1063
commit 93b1c8e
Showing
7 changed files
with
149 additions
and
134 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,9 @@ | ||
import type { AppProps } from 'next/app' | ||
import './_app.css' | ||
|
||
import { setCookieLocale, useActualLocale } from 'next-multilingual' | ||
import { useRouter } from 'next/router' | ||
import { useActualLocale } from 'next-multilingual' | ||
|
||
export default function MyApp({ Component, pageProps }: AppProps): JSX.Element { | ||
// Forces Next.js to use the actual (proper) locale. | ||
useActualLocale() | ||
// The next two lines are optional, to enable smart locale detection on the homepage. | ||
const router = useRouter() | ||
// Persist locale on page load (only used on `/` when using smart locale detection). | ||
setCookieLocale(router.locale) | ||
useActualLocale() // Forces Next.js to use the actual (proper) locale. | ||
return <Component {...pageProps} /> | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import Layout from '@/layout' | ||
import type { GetServerSideProps, NextPage } from 'next' | ||
import { ResolvedLocaleServerSideProps, resolveLocale, useResolvedLocale } from 'next-multilingual' | ||
import { getTitle, useMessages } from 'next-multilingual/messages' | ||
|
||
const Home: NextPage<ResolvedLocaleServerSideProps> = ({ resolvedLocale }) => { | ||
// Force Next.js to use a locale that was resolved dynamically on the homepage. | ||
useResolvedLocale(resolvedLocale) | ||
|
||
// Load the messages in the correct locale. | ||
const messages = useMessages() | ||
|
||
return ( | ||
<Layout title={getTitle(messages)}> | ||
<h1>{messages.format('headline')}</h1> | ||
</Layout> | ||
) | ||
} | ||
|
||
export default Home | ||
|
||
export const getServerSideProps: GetServerSideProps<ResolvedLocaleServerSideProps> = async ( | ||
nextPageContext | ||
) => { | ||
return { | ||
props: { | ||
resolvedLocale: resolveLocale(nextPageContext), | ||
}, | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.