使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商
版本历史
- "更新 Solid useIntlayer API 用法以直接访问属性"v8.9.02026/5/4
- "初始化历史记录"v8.0.42026/1/26
此页面的内容已使用 AI 翻译。
查看英文原文的最新版本If you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.
GitHub link to the documentationCopy doc Markdown to clipboard
使用 Intlayer 翻译您的 Analog (Angular) 应用 | 国际化 (i18n)
目录
为什么选择 Inlayer 而不是替代品?
与“ngx-translate”或“angular-l10n”等主要解决方案相比,Intlayer 是一个具有集成优化的解决方案,例如:
完整的模拟覆盖
Intlayer 经过优化,可与 Analog 完美配合,提供多语言路由、SSR 支持以及扩展国际化 (i18n) 所需的所有功能。
捆绑尺寸
不要将大量 JSON 文件加载到页面中,而只需加载必要的内容。 Intlayer 有助于将捆绑包和页面大小减少多达 50%。
可维护性
确定应用程序内容的范围有利于大型应用程序的维护。您可以复制或删除单个功能文件夹,而无需承担检查整个内容代码库的精神负担。此外,Intlayer 具有完全类型化 (fully typed),以确保您的内容的准确性。
人工智能代理
共置内容减少大型语言模型 (LLM) 所需的上下文。 Intlayer 还附带了一套工具,例如用于测试缺失翻译的 CLI、LSP、MCP 和 agent技能,使 AI 代理的开发者体验 (DX) 更加流畅。
自动化
使用您选择的法学硕士,通过自动化在 CI/CD 管道中进行翻译,而费用由您的 AI 提供商承担。 Intlayer 还提供了一个编译器来自动提取内容,以及一个网络平台来帮助在后台翻译。
表现
将大量 JSON 文件连接到组件可能会导致性能和反应性问题。 Intlayer 可在构建时 (build time)优化您的内容加载。
无需开发即可扩展
Intlayer 不仅仅是一个 i18n 解决方案,还提供了一个自托管的可视化编辑器和一个完整的 CMS 来帮助您管理多语言内容实时,与译员、文案人员和其他团队成员无缝协作。内容可以本地和/或远程存储。
在 Analog 应用中设置 Intlayer 的分步指南
查看 GitHub 上的 应用模板。
第 1 步:安装依赖项
使用 npm 安装必要的软件包:
复制代码到剪贴板
npm install intlayer angular-intlayer vite-intlayernpx intlayer initintlayer
angular-intlayer 将 Intlayer 与 Angular 应用集成的软件包。它为 Angular 国际化提供上下文提供者和 Hook。
vite-intlayer 将 Intlayer 与 Vite 集成的软件包。它提供一个插件来处理内容声明文件,并为优化性能设置别名。
第 2 步:项目配置
创建一个配置文件来配置应用的语言:
复制代码到剪贴板
import { Locales, type IntlayerConfig } from "intlayer";
const config: IntlayerConfig = {
internationalization: {
locales: [
Locales.ENGLISH,
Locales.FRENCH,
Locales.SPANISH,
// 您的其他语言
],
defaultLocale: Locales.ENGLISH,
},
};
export default config;通过此配置文件,您可以设置本地化 URL、中间件重定向、Cookie 名称、内容声明的位置和扩展名、禁用控制台中的 Intlayer 日志等。有关可用参数的完整列表,请参考配置文档。
第 3 步:在 Vite 配置中集成 Intlayer
要将 Intlayer 与 Analog 集成,您需要使用 vite-intlayer 插件。
修改您的 vite.config.ts 文件:
复制代码到剪贴板
import { defineConfig } from "vite";import { intlayer } from "vite-intlayer";import analog from "@analogjs/platform";// https://vitejs.dev/config/export default defineConfig(() => ({ plugins: [ analog(), intlayer(), // 添加 Intlayer 插件 ],}));intlayer() 插件为 Vite 配置了 Intlayer。它处理内容声明文件并为优化性能设置别名。
第 4 步:声明您的内容
创建并管理您的内容声明以存储翻译:
您的内容声明可以定义在应用中的任何位置,只要它们包含在contentDir目录(默认为./src)中,并且符合内容声明文件扩展名(默认为.content.{json,ts,tsx,js,jsx,mjs,cjs,md,mdx,yaml,yml})。
有关更多详细信息,请参考内容声明文档。
第 5 步:在代码中使用 Intlayer
要在整个 Analog 应用中使用 Intlayer 的国际化功能,您需要在应用配置中提供 Intlayer。
复制代码到剪贴板
import { ApplicationConfig } from "@angular/core";import { provideIntlayer } from "angular-intlayer";export const appConfig: ApplicationConfig = { providers: [ provideIntlayer(), // 在此处添加 Intlayer 提供者 ],};然后,您可以在任何组件中使用 useIntlayer 函数。
复制代码到剪贴板
import { Component } from "@angular/core";import { useIntlayer } from "angular-intlayer";@Component({ selector: "app-home", standalone: true, template: ` <div class="content"> <h1>{{ content().title }}</h1> <p>{{ content().congratulations }}</p> </div> `,})export default class HomeComponent { content = useIntlayer("app");}Intlayer 内容以 Signal 形式返回,因此您通过调用 Signal 来访问值:content().title。
(可选)第 6 步:更改内容语言
要更改内容的语言,您可以使用 useLocale 函数提供的 setLocale 函数。这允许您设置应用的语言环境并相应地更新内容。
创建一个用于切换语言的组件:
复制代码到剪贴板
import { Component } from "@angular/core";import { CommonModule } from "@angular/common";import { useLocale } from "angular-intlayer";@Component({ selector: "app-locale-switcher", standalone: true, imports: [CommonModule], template: ` <div class="locale-switcher"> <select [value]="locale()" (change)="setLocale($any($event.target).value)" > @for (loc of availableLocales; track loc) { <option [value]="loc">{{ loc }}</option> } </select> </div> `,})export class LocaleSwitcherComponent { localeCtx = useLocale(); locale = this.localeCtx.locale; availableLocales = this.localeCtx.availableLocales; setLocale = this.localeCtx.setLocale;}然后,在您的页面中使用此组件:
复制代码到剪贴板
import { Component } from "@angular/core";import { useIntlayer } from "angular-intlayer";import { LocaleSwitcherComponent } from "../locale-switcher.component";@Component({ selector: "app-home", standalone: true, imports: [LocaleSwitcherComponent], template: ` <app-locale-switcher></app-locale-switcher> <div class="content"> <h1>{{ content().title }}</h1> <p>{{ content().congratulations }}</p> </div> `,})export default class HomeComponent { content = useIntlayer("app");}配置 TypeScript
Intlayer 使用模块扩充 (module augmentation) 来利用 TypeScript 的优势并使您的代码库更强大。


确保您的 TypeScript 配置包含自动生成的类型。
复制代码到剪贴板
{ // ... 您现有的 TypeScript 配置 "include": [ // ... 您现有的 TypeScript 配置 ".intlayer/**/*.ts", // 包含自动生成的类型 ],}Git 配置
建议忽略 Intlayer 生成的文件。这可以避免将它们提交到您的 Git 仓库。
为此,您可以将以下指令添加到 .gitignore 文件中:
复制代码到剪贴板
# 忽略 Intlayer 生成的文件.intlayerVS Code 扩展
为了提升您的 Intlayer 开发体验,您可以安装官方的 Intlayer VS Code 扩展。
此扩展提供:
- 翻译键的自动补全。
- 缺失翻译的实时错误检测。
- 翻译内容的内联预览。
- 轻松创建和更新翻译的快速操作。
有关如何使用该扩展的更多详细信息,请参考 Intlayer VS Code 扩展文档。