跳转到内容

Astro i18n Next

翻译URL路由、内容本地化、多语言Markdown和i18next集成 — 只需一次 createI18n() 调用。

功能特性

翻译URL路由

为所有页面自动添加语言前缀路径。/about/ 变为 /es/sobre/ — 无需动态 slug 或通配路由。

虚拟模块

virtual:i18n 导入 tlocalePathswitchLocalePath 等。一次导入,获取所有 i18n 辅助工具。

多语言 Markdown

在单个 Markdown 文件中使用 YAML frontmatter 和 <!-- locale:es --> 标记编写所有语言版本。基于 Astro 5 的 Content Loader API。

i18next 集成

完整的 i18next 支持,包括点号键名、回退链和插件扩展性(ICU、插值等)。

页面 Slug 翻译

.astro 页面导出 slugs 来定义每种语言的 URL 段。构建时自动发现 — 无需额外配置。

完全静态

专为 Astro 的静态输出设计。所有路由预渲染,零运行时开销。

快速示例

astro.config.mjs
import { createI18n } from '@otrodigital/astro-i18n-next';
export default defineConfig({
integrations: [createI18n({
defaultLocale: 'en',
locales: ['en', 'es'],
localeLabels: { en: 'English', es: 'Español' },
localeHtmlLang: { en: 'en', es: 'es' },
translations: { en, es },
pagesDir: 'src/pages',
})],
});
---
// 任意 .astro 组件
import { t, localePath } from 'virtual:i18n';
const locale = Astro.locals.locale;
---
<h1>{t(locale, 'home.title')}</h1>
<a href={localePath(locale, '/about/')}>{t(locale, 'nav.about')}</a>