Skip to content

RFC: Localization of pathnames and streamlining of navigation APIs #402

Closed
@amannn

Description

@amannn

Localization of pathnames

Update: This is available as of [email protected], see the announcement thread.

We currently provide minimal documentation for localizing pathnames (see also the named routes example).

It would be helpful to have built-in support for this.

We could achieve this by accepting a route map:

// The `pathnames` object holds pairs of internal
// and external paths. Based on the locale, the
// corresponding external path will be picked.
const pathnames = {
  // If all locales use the same pathname, a single
  // external path can be used for all locales.
  '/': '/',
  '/blog': '/blog',
 
  // If locales use different paths, you can
  // specify each external path per locale.
  '/about': {
    en: '/about',
    de: '/ueber-uns'
  },
 
  // Dynamic params are supported via square brackets
  '/news/[articleSlug]-[articleId]': {
    en: '/news/[articleSlug]-[articleId]',
    de: '/neuigkeiten/[articleSlug]-[articleId]'
  }
};

Proposed docs:

Prior art: https://v8.i18n.nuxtjs.org/guide/custom-paths

Streamlining of navigation APIs

Similar to the proposed factory function createLocalizedPathnamesNavigation, we could offer a createSharedPathnamesNavigation factory function that can be used to create the currently existing navigation APIs for non-localized pathnames.

This would:

  1. Streamline all imports to a single namespace
  2. Provide strict typing for the locales
  3. Make it easier to upgrade from shared pathnames to localized pathnames (you'll have to adjust pathnames with dynamic params though, but we can provide TypeScript support here)
import {createSharedPathnamesNavigation} from 'next-intl/navigation';
 
export const locales = ['en', 'de'] as const;
 
export const {Link, redirect, usePathname, useRouter} =
  createSharedPathnamesNavigation({locales});

Potentially we could furthermore return a typed version of useLocale from these functions.

Rollout plan: Introduce createSharedPathnamesNavigation in a minor version, deprecate the previous imports and remove them in a major release.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions