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

feat: Add defineRouting for easier i18n routing setup #1299

Merged
merged 1 commit into from
Aug 28, 2024
Merged

feat: Add defineRouting for easier i18n routing setup #1299

merged 1 commit into from
Aug 28, 2024

Conversation

amannn
Copy link
Owner

@amannn amannn commented Aug 28, 2024

Summary

Adds a new defineRouting API that returns a routing object that can be used to initialize the middleware as well as the navigation APIs. This provides type-safety for your i18n config with a single import and helps to ensure that your i18n config is in sync across your app.

// routing.ts

import {defineRouting} from 'next-intl/routing';
 
export const routing = defineRouting({
  locales: ['en-US', 'en-GB'],
  defaultLocale: 'en-US',
  localePrefix: {
    mode: 'always',
    prefixes: {
      'en-US': '/us',
      'en-GB': '/uk'
    }
  },
  pathnames: {
    '/': '/',
    '/organization': {
      'en-US': '/organization',
      'en-GB': '/organisation'
    }
  }
});
// middleware.ts

import createMiddleware from 'next-intl/middleware';
import {routing} from './routing';
 
export default createMiddleware(routing);
// navigation.ts

import {createSharedPathnamesNavigation} from 'next-intl/navigation';
import {routing} from './routing';
 
export const {Link, redirect, usePathname, useRouter} =
  createSharedPathnamesNavigation(routing);

While this change is purely additional, it's encouraged to adopt defineRouting in existing code bases and the docs have been updated to reflect this.

Migration example: amannn/next-intl-bug-repro-app-router@0a00684

In case you're providing either alternateLinks or localeDetection to createMiddleware, these options can now be passed as a second argument:

createMiddleware(routing, {
  alternateLinks: false,
  localeDetection: false
});

Changes

  • feat: Add defineRouting API
  • feat: Allow to pass middleware options (ie. now everything that is not routing) as second param to createMiddleware
  • docs: Suggest using src/routing.ts instead of src/navigation.ts
  • docs: Suggest setting up navigation APIs in getting started docs
  • docs: Suggest setting up middleware before i18n.ts in getting started docs

**Summary**

Adds a new
[`defineRouting`](https://next-intl-docs-git-feat-v4-routing-next-intl.vercel.app/docs/routing#define-routing)
API that returns a `routing` object that can be used to initialize the
middleware as well as the navigation APIs. This provides type-safety for
your i18n config with a single import and helps to ensure that your i18n
config is in sync across your app.

```tsx
// routing.ts

import {defineRouting} from 'next-intl/routing';
 
export const routing = defineRouting({
  locales: ['en-US', 'en-GB'],
  defaultLocale: 'en-US',
  localePrefix: {
    mode: 'always',
    prefixes: {
      'en-US': '/us',
      'en-GB': '/uk'
    }
  },
  pathnames: {
    '/': '/',
    '/organization': {
      'en-US': '/organization',
      'en-GB': '/organisation'
    }
  }
});
```

```tsx
// middleware.ts

import createMiddleware from 'next-intl/middleware';
import {routing} from './routing';
 
export default createMiddleware(routing);
 
export const config = {
  // Match only internationalized pathnames
  matcher: ['/', '/(de|en)/:path*']
};
```

```tsx
// navigation.ts

import {createSharedPathnamesNavigation} from 'next-intl/navigation';
import {routing} from './routing';
 
export const {Link, redirect, usePathname, useRouter} =
  createSharedPathnamesNavigation(routing);
```

(in the docs we now suggest to create navigation APIs directly in
`routing.ts`)

**Changes**
 - feat: Add `defineRouting` API
- feat: Allow to pass middleware options (ie. now everything that is not
`routing`) as second param to `createMiddleware`
 - docs: Suggest using `src/routing.ts` instead of `src/navigation.ts`
 - docs: Suggest setting up navigation APIs in getting started docs
- docs: Suggest setting up middleware before `i18n.ts` in getting
started docs

**TODO:**
- [ ] More testing (via a prerelease)
 - [ ] Update bug repro app router (after release)

---------

Co-authored-by: amannn <amannn@users.noreply.github.com>
Copy link

vercel bot commented Aug 28, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-intl-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 28, 2024 2:43pm
next-intl-example-app-router ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 28, 2024 2:43pm
next-intl-example-app-router-without-i18n-routing ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 28, 2024 2:43pm

@amannn amannn marked this pull request as ready for review August 28, 2024 14:41
@amannn amannn merged commit 5ff6120 into main Aug 28, 2024
12 checks passed
@amannn amannn deleted the canary branch August 28, 2024 15:13
@amannn amannn changed the title feat: Add defineRouting for easier i18n routing setup (#1294) feat: Add defineRouting for easier i18n routing setup Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant