Typed Translations with Astro's Built-in i18n
Astro 4.0+ has built-in routing support for i18n. You can combine it with i18n-tiny to get the best of both worlds: Astro's native routing and i18n-tiny's type-safe translations.
Configuration
Set up your astro.config.mjs using Astro's native i18n configuration.
typescript
// astro.config.mjs
import { defineConfig } from 'astro/config'
export default defineConfig({
i18n: {
defaultLocale: 'en',
locales: ['en', 'ja'],
routing: {
prefixDefaultLocale: false
}
}
})Integration
Since Astro handles the routing, you only need i18n-tiny for message resolution.
astro
---
// src/pages/[locale]/index.astro
import { locales, getTranslations } from '../../i18n'
// Static output: build one page per locale (leave this out with output: 'server')
export function getStaticPaths() {
return locales.map((locale) => ({ params: { locale } }))
}
// Set by Astro's built-in i18n
const t = getTranslations(Astro.currentLocale)
---
<h1>{t('common.title')}</h1>Benefits of this approach
- Use Astro's native
Astro.currentLocale. - Use Astro's automatic redirect features.
- Keep the ultra-simple and type-safe translation API of i18n-tiny.