Skip to 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. Reads the locales from Astro.locals.locales, which only the i18n-tiny middleware sets.
OthersHTMLAttributes<'a'>-Attributes for standard HTML anchor tag

Example:

astro
---
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 (requires the i18n-tiny middleware) -->
<Link href="/ja/about" locale="en" normalize>English</Link>

"Maintains current URL pattern" means the link gets a locale prefix only when the current URL has one. With the SSG integration, the root pages are copies of the /[defaultLocale]/ pages, so their links still point to /[defaultLocale]/... (e.g., /en/about).

Without the middleware, normalize does nothing, because Astro.locals.locales is not set. Remove the prefix yourself instead:

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

{locales.map((loc) => (
  <Link href={removeLocalePrefix(Astro.url.pathname, locales)} locale={loc}>{loc}</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:

typescript
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:

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

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

hasLocalePrefix(pathname, locale) ​

Checks if a pathname starts with the given locale prefix. Can be imported from @i18n-tiny/astro/router.

Parameters:

ParameterTypeRequiredDescription
pathnamestringRequiredThe pathname to check
localestringRequiredThe locale to check for

Returns:

  • boolean: true if the pathname starts with /[locale]/ or equals /[locale].

Example:

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

hasLocalePrefix('/ja/about', 'ja')  // true
hasLocalePrefix('/about', 'ja')     // false