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
hrefstringRequired-Destination path
localestring | false-Explicitly set locale. false or '' disables localization (raw path).
normalizebooleanfalseIf true, removes existing locale prefix from href before processing.
OthersHTMLAttributes<'a'>-Attributes for standard HTML anchor tag

Example:

---
import Link from '@i18n-tiny/astro/router/Link.astro'
---

<!-- 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>

<!-- Path normalization -->
<Link href="/ja/about" locale="en" normalize>English</Link>

getLocalizedPath(path, locale, defaultLocale, prefixDefault?)

Generates a localized path with a locale prefix. Can be imported from @i18n-tiny/astro/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.

Example:

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

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

removeLocalePrefix(pathname, locales)

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

Parameters:

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

Returns:

  • string: Pathname without prefix.

Example:

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

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

hasLocalePrefix(pathname, locales)

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

Parameters:

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

Returns:

  • boolean: true if prefix exists.

Example:

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

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

getLinkHref(href, currentPathname, currentLocale, options?)

Utility to generate final URLs for links. Can be imported from @i18n-tiny/astro/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/astro/router'

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