An open API service indexing awesome lists of open source software.

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

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

| ![me](https://gravatar.com/avatar/fa50aeff0ddd6e63273a068b04353d9d?size=100) |
| ---------------------------------------------------------------------------- |
| [Pablo Varela](https://pablo.pìnk) |