Skip to main content

Proxy

Next.js middleware for automatic locale detection and routing.

create(config)

Creates a Next.js Proxy (Middleware) handler for i18n routing.

Parameters:

ParameterTypeRequiredDefaultDescription
localesreadonly string[]Required-Array of supported locales
defaultLocalestringRequired-Default locale for redirects
fallbackLocalestringdefaultLocaleFallback if detection fails
prefixDefaultbooleanfalseWhether to prefix the default locale in URLs
detectLanguagebooleantrueWhether to detect language from Accept-Language header
routing'rewrite'-SSR rewrite mode (mutually exclusive with prefixDefault/detectLanguage)

Returns:

  • (request: NextRequest) => NextResponse | void: Next.js middleware function.

Routing Behavior Matrix:

prefixDefaultdetectLanguageBehavior at /
falsefalseServes fallbackLocale, no detection
falsetrueDetects, redirects non-default, rewrites default
truefalseRedirects to /[defaultLocale]
truetrueDetects and redirects to detected locale

Example:

// Default: Language detection, non-default redirects, default rewrites
export const proxy = create({
locales: ['en', 'ja'],
defaultLocale: 'en'
})

// Always prefix all locales (including default)
export const proxy = create({
locales: ['en', 'ja'],
defaultLocale: 'en',
prefixDefault: true
})

// SSR rewrite mode (locale in x-locale header)
export const proxy = create({
locales: ['en', 'ja'],
defaultLocale: 'en',
routing: 'rewrite'
})

detectLocale(acceptLanguage, supportedLocales)

Detects the best matching locale from the Accept-Language header. Can be imported directly from @i18n-tiny/next/proxy.

Parameters:

ParameterTypeRequiredDescription
acceptLanguagestring | nullRequiredValue of Accept-Language header
supportedLocalesreadonly string[]RequiredArray of supported locales

Returns:

  • string | null: The matched locale, or null if no match is found.

Example:

import { detectLocale } from '@i18n-tiny/next/proxy'

const locale = detectLocale(request.headers.get('accept-language'), ['en', 'ja'])