Skip to main content

Router

Router utilities for localized navigation.

A localized Link component that automatically detects the locale from the current URL.

Props:

PropTypeRequiredDefaultDescription
hrefstring | UrlObjectRequired-Destination path
localestring | false-Explicitly set locale. false or '' disables localization (raw path).
normalizebooleanfalseIf true, removes existing locale prefix from href before processing.
OthersNextLinkProps-Other props for Next.js Link component

Example:

import { Link } from '@i18n-tiny/next/router'

// Auto-localized (maintains current URL pattern)
<Link href="/about">About</Link>

// Explicit locale override
<Link href="/" locale="ja">日本語</Link>

// Raw path (no localization)
<Link href="/" locale="">English</Link>
<Link href="/" locale={false}>English</Link>

// Conditional locale
<Link href="/" locale={condition && 'ja'}>Conditional</Link>

// Path normalization (useful for language switchers)
const pathname = usePathname() // '/ja/about'
<Link href={pathname} locale="en" normalize>English</Link> // -> '/en/about'

getLocalizedPath(path, locale, defaultLocale, prefixDefault?)

Generates a localized path with a locale prefix. Can be imported from @i18n-tiny/next/router.

Parameters:

ParameterTypeRequiredDefaultDescription
pathstringRequired-The path to localize
localestringRequired-Target locale
defaultLocalestringRequired-Default locale
prefixDefaultbooleanfalseWhether to prefix the default locale

Returns:

  • string: The localized path (e.g., /ja/about).

Example:

import { getLocalizedPath } from '@i18n-tiny/next/router'

getLocalizedPath('/about', 'ja', 'en') // '/ja/about'
getLocalizedPath('/about', 'en', 'en') // '/about'
getLocalizedPath('/about', 'en', 'en', true) // '/en/about'

removeLocalePrefix(pathname, locales)

Removes the locale prefix from a pathname. Can be imported from @i18n-tiny/next/router.

Parameters:

ParameterTypeRequiredDescription
pathnamestringRequiredThe pathname to process
localesreadonly string[]RequiredArray of supported locales

Returns:

  • string: Pathname without prefix (e.g., /ja/about/about).

Example:

import { removeLocalePrefix } from '@i18n-tiny/next/router'

removeLocalePrefix('/ja/about', ['en', 'ja']) // '/about'

hasLocalePrefix(pathname, locales)

Checks if a pathname contains a locale prefix. Can be imported from @i18n-tiny/next/router.

Parameters:

ParameterTypeRequiredDescription
pathnamestringRequiredThe pathname to check
localesreadonly string[]RequiredArray of supported locales

Returns:

  • boolean: true if prefix exists, otherwise false.

Example:

import { hasLocalePrefix } from '@i18n-tiny/next/router'

hasLocalePrefix('/ja/about', ['en', 'ja']) // true

getLinkHref(href, currentPathname, currentLocale, options?)

Utility to generate final URLs for links. Used internally by the Link component. Can be imported from @i18n-tiny/next/router.

Parameters:

ParameterTypeRequiredDescription
hrefstringRequiredDestination path
currentPathnamestringRequiredCurrent pathname
currentLocalestring | undefinedRequiredCurrent locale
optionsGetLinkHrefOptionsOptions for explicit locale or normalization

Returns:

  • string: The final generated URL.

Example:

import { getLinkHref } from '@i18n-tiny/next/router'

const href = getLinkHref('/about', '/ja', 'ja', { locale: 'en' }) // '/en/about'