Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(examples): add useSyncLanguage example to change language #173

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ There are a few ready made projects that you can find in the examples folder. Th
- [Custom Language Selection](../examples/custom-language-selection)
- [Custom Translation Files Location](../examples/custom-location)
- [Cached Translations](../examples/cached-translations)
- [Use `useSyncLanguage` to change language](../examples/use-sync-language)
3 changes: 3 additions & 0 deletions examples/use-sync-language/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
14 changes: 14 additions & 0 deletions examples/use-sync-language/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Using useSyncLanguage to switch current language

Shows how to use `useSyncLanguage` into a regular `Next.js` project.
See example in the header component.

## Doc

`useSyncLanguage` can be used when not leveraging next.js' localized routes.
It will make sure to set the `current language` properly in `i18next` based
on the language parameter you pass to it.

Note that this can cause an error when rendering on the server. This happens
because `useTranslation` returns a Suspense. To fix it, either enable react
Suspense Mode, or set `react.useSuspense = false` in your `ni18n-options.ts`
7 changes: 7 additions & 0 deletions examples/use-sync-language/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"paths": {
"@/*": ["./src/*"]
}
}
}
7 changes: 7 additions & 0 deletions examples/use-sync-language/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
i18n: {
defaultLocale: 'en',
locales: ['en', 'es', 'pt'],
},
reactStrictMode: true,
}
4 changes: 4 additions & 0 deletions examples/use-sync-language/ni18n.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const ni18nConfig = {
supportedLngs: ["en", "es", "pt"],
ns: ["home"],
};
Loading