Skip to content

feat: Add defineRouting for easier i18n routing setup - #1299

Merged
amannn merged 1 commit into
mainfrom
canary
Aug 28, 2024
Merged

feat: Add defineRouting for easier i18n routing setup#1299
amannn merged 1 commit into
mainfrom
canary

Conversation

@amannn

@amannn amannn commented Aug 28, 2024

Copy link
Copy Markdown
Owner

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>
@vercel

vercel Bot commented Aug 28, 2024

Copy link
Copy Markdown

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

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