https://github.com/pablopunk/nextjs-redirect-locale
Redirect to corresponding locale route
https://github.com/pablopunk/nextjs-redirect-locale
Last synced: about 1 year ago
JSON representation
Redirect to corresponding locale route
- Host: GitHub
- URL: https://github.com/pablopunk/nextjs-redirect-locale
- Owner: pablopunk
- Created: 2020-04-24T14:49:36.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-11-08T13:52:06.000Z (over 5 years ago)
- Last Synced: 2025-03-29T17:35:55.532Z (about 1 year ago)
- Language: TypeScript
- Homepage: https://pablopunk.com
- Size: 20.5 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# nextjs-redirect-locale
Redirect to corresponding locale. Works on client and server.
> Note: This project still works but there's now an official way of doing i18n in NextJS. [Check out the official docs](https://nextjs.org/docs/advanced-features/i18n-routing).
## Install
```sh
npm install nextjs-redirect-locale
```
## Usage
Let's say you have a next.js project ([like this one](https://github.com/pablopunk/pablopunk.com)) that creates one route per locale automatically, i.e. a page like `page/home.js` will be available in different languages with the following routes:
- `/en/home`
- `/es/home`
- `/gl/home`
- `/fr/home`
- `/fr_CA/home`
If you want to redirect to the proper locale once the user visits `/`, then you can create a `pages/index.js` with the following code:
```jsx
import redirectLocale from 'nextjs-redirect-locale'
export default redirectLocale(['en', 'es', 'gl', 'fr', 'fr_CA'])
```
Result: `/` redirects to `/es/` (if user locale is `es`)
Keep in mind the first locale is gonna be the default (i.e. the one that gets selected if the user locale is not on the list).
You can checkout this example live in [pablopunk.com](https://pablopunk.com/).
Also, here's a demo of the code you can play with: [codesandbox.io/s/nextjs-redirect-locale-ci](https://codesandbox.io/s/nextjs-redirect-locale-ci-utjwg).
## Static export
This package is compatible with static exports via `next export` since version 2.0.0
## Related
- Custom redirects for NextJS: [nextjs-redirect](https://github.com/pablopunk/nextjs-redirect)
## License
MIT
## Author
|  |
| ---------------------------------------------------------------------------- |
| [Pablo Varela](https://pablo.pìnk) |