0.8.0 • Published 3 months ago

@labdigital/next-intl-custom-paths v0.8.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

@labdigital/next-intl-custom-paths

Opinionated wrapper around next-intl 3.0 for internationalization with custom paths.

This decouples the locale used in your codebase from the path. So for example /en can internally be used as en-US.

It's a bit opinionated and specific for our use cases for now:

  • Only supports localized path names, see cprussin's comment on how to set something like this up for shared pathnames
  • Doesn't support saving the default locale based on cookie

Heavily inspired by https://github.com/amannn/next-intl/issues/653#issuecomment-1823273158, thanks @cprussin.

Usage

In your middleware.ts add the following

import { createNextIntlCustomPathMiddleware } from "@labdigital/next-intl-custom-paths";

const intlMiddleware = createNextIntlCustomPathMiddleware({
		defaultLocale: "en-US",
		locales: ["en-US", "nl-NL", "de-DE"],
		pathToLocaleMapping: {
			"en": "en-US",
			"nl": "nl-NL",
			"de": "de-DE",
		}
	});

You can also edit all next-intl middleware arguments using the nextIntlMiddlewareOptions key, e.g.:

const intlMiddleware = createNextIntlCustomPathMiddleware({
		defaultLocale: "en-US",
		locales: ["en-US", "nl-NL", "de-DE"],
		pathToLocaleMapping: {
			"en": "en-US",
			"nl": "nl-NL",
			"de": "de-DE",
		},
		nextIntlMiddlewareOptions: {
			localeDetection: false,
			pathnames: {
				"/": "/"
			}
		}
	});

This package has it's own navigation helpers to translate path to locale, you should use these if you use our middleware:

import { createLocalizedNavigation } from "@labdigital/next-intl-custom-paths";

export const { usePathname, useRouter, Link } = createLocalizedNavigation(
	locales,
	defaultLocale,
	localePrefix,
	pathToLocaleMapping,
	pathnames,
);
0.8.0

3 months ago

0.7.0

4 months ago

0.5.2

4 months ago

0.6.0

4 months ago

0.5.1

4 months ago

0.3.0

4 months ago

0.5.0

4 months ago

0.4.0

4 months ago

0.2.1

4 months ago

0.2.0

4 months ago

0.1.0

5 months ago