Astro Integration
Relevant source files
The following files were used as context for generating this wiki page:
Astro Integration provides internationalization (i18n) capabilities for Astro projects using the core logic of Nuxt I18n Micro. It enables translation management, route-specific support, and locale-prefixed routing strategies within the Astro ecosystem. The integration offers full TypeScript support and dedicated components for common i18n tasks like translation rendering and locale switching.
Sources: packages/astro/README.md:3-5, packages/astro/package.json:3-8
Architecture and Core Logic
The integration relies on AstroI18n, a class that manages translation storage, formatting, and locale resolution. It extends core functionalities from @i18n-micro/core and @i18n-micro/node to provide a framework-agnostic runtime adapted for Astro's static and server-side rendering modes.
Translation Flow
The following diagram illustrates how translations are resolved and served to the Astro application.
The system separates global translations (common texts like menus) from page-specific files to optimize payload sizes and build performance.
Sources: packages/astro/README.md:46-48, scripts/api-surface/i18n-micro__astro.api.txt:3-10, AGENTS.md:20-25
Key Components and Helpers
The @i18n-micro/astro package exports several built-in Astro components and composables to simplify i18n implementation.
Composables
The primary entry point for using i18n in Astro pages is the useI18n hook. This function accepts the global Astro object and returns translation helpers and locale state.
| Helper | Purpose |
|---|---|
t | Translates a key into a CleanTranslation (string, number, or object). |
ts | Returns a strict string translation. |
tc | Handles pluralization based on a count. |
tn | Formats numbers according to locale-specific rules. |
td | Formats dates and times. |
locale | Provides the current active locale code. |
locales | Returns the list of configured locale objects. |
Sources: packages/astro/README.md:35-39, scripts/api-surface/i18n-micro__astro.api.txt:140-140
Built-in Components
The integration provides several components available via subpath exports:
- i18n-t: Renders a translation key.
- i18n-link: A localized version of the standard anchor tag.
- i18n-switcher: A component for changing the current language.
- i18n-group: Groups translation keys for specific contexts.
Sources: packages/astro/package.json:42-53
Routing and Locales
Routing is handled through locale-prefixed URLs (e.g., /[locale]/*). The integration provides a dedicated routing strategy and middleware to detect and redirect users based on their preferences or path.
Locale Detection Sequence
Configuration Options
The i18nIntegration function accepts an I18nIntegrationOptions object.
| Option | Type | Description |
|---|---|---|
locale | string | The default active locale. |
locales | Locale[] | Array of supported locales with ISO and display names. |
strategy | Strategies | Routing strategy (e.g., prefix, no_prefix). |
translationDir | string | Directory where translation JSON files are stored. |
messages | Record | Inline translations for quick start. |
autoDetect | boolean | Enables automatic locale detection from browser headers. |
Sources: packages/astro/README.md:15-30, scripts/api-surface/i18n-micro__astro.api.txt:115-132
Rewriting for Locale Routes
When implementing locale-prefixed routes, developers use rewrite stubs in src/pages/[locale]/. The integration provides prepareLocaleRewrite and getLocaleRewritePath to determine the correct internal target for Astro.rewrite.
import { prepareLocaleRewrite } from '@i18n-micro/astro'
const rewriteTarget = prepareLocaleRewrite(Astro)
if (rewriteTarget instanceof Response) return rewriteTarget
return Astro.rewrite(rewriteTarget)
Sources: packages/astro/README.md:54-61, packages/astro/CHANGELOG.md:12-13
Framework Islands Support
The integration supports client-side "islands" for Vue, React, Preact, and Svelte. Each framework has a dedicated client provider (e.g., I18nProvider for React) and corresponding hooks (e.g., useAstroI18nVue) to synchronize the translation state between the server and the browser.
Sources: packages/astro/package.json:54-73, scripts/api-surface/i18n-micro__astro.api.txt:143-152
Summary
The Astro Integration for @i18n-micro enables high-performance internationalization by leveraging shared core utilities and Astro's unique routing capabilities. It prioritizes small bundle sizes and fast build times by offering strategy-based routing, efficient translation loading, and seamless state synchronization for framework-specific islands.