amannn / next-intl

🌐 Internationalization (i18n) for Next.js
https://next-intl-docs.vercel.app
MIT License
2.58k stars 236 forks source link

feat: Add `defineRouting` for easier i18n routing setup #1294

Closed amannn closed 2 months ago

amannn commented 2 months ago

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);

export const config = {
  // Match only internationalized pathnames
  matcher: ['/', '/(de|en)/:path*']
};
// 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

TODO:

vercel[bot] commented 2 months ago

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 1:49pm
next-intl-example-app-router βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Aug 28, 2024 1:49pm
next-intl-example-app-router-without-i18n-routing βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Aug 28, 2024 1:49pm