跳转至主要内容
Angular i18n

Angular i18n 解决方案

为 Angular 应用程序提供独立组件、Signals 和 SSR 支持。

Sync
npx lefine strings pull
Runtime
@ngx-translate/core
lefine · @ngx-translate/core
01终端
npm install @ngx-translate/core
02src/app/lefine.loader.ts
import { Injectable, inject } from '@angular/core'import { HttpClient } from '@angular/common/http'import { TranslateLoader } from '@ngx-translate/core'import { Observable } from 'rxjs'// TranslateLoader's contract: one Observable of messages per language.// The files are the ones `lefine strings pull` wrote.@Injectable({ providedIn: 'root' })export class LefineLoader implements TranslateLoader {  private http = inject(HttpClient)  getTranslation(lang: string): Observable<Record<string, unknown>> {    return this.http.get<Record<string, unknown>>(`/assets/locales/${lang}.json`)  }}
03src/app/app.component.html
<!-- app.component.html --><h1>{{ 'home.title' | translate }}</h1><p>{{ 'home.greeting' | translate: { name: 'World' } }}</p><!-- ICU plurals authored in the Lefine dashboard --><p>{{ 'cart.items' | translate: { count: 3 } }}</p><app-locale-switcher />
'home.greeting' | translate: { name: 'World' }
Hello, World
In a template

In a template

translate pipe 和 service 都从 loader 读取,因此组件完全不需要知道 CDN 的存在。

01/Setup

在 Angular 中实现运行时翻译,只需三步

Angular 内置的 i18n 会为每种语言编译一个包。若希望翻译在不重新构建的情况下更新,ngx-translate 可以在运行时从 Lefine 读取。

  1. 01

    安装 loader 依赖

    ngx-translate 提供 pipe、service 和 loader 契约;core 负责获取并缓存文案。

    terminalbash
    npm install @ngx-translate/corenpx lefine loginnpx lefine strings pull -o src/assets/locales# pull writes src/assets/locales/en.json, de.json, ...# Run it again after each publish (or in your build) to refresh them.
  2. 02

    编写一个基于 CDN 的 TranslateLoader

    TranslateLoader 要求每种语言返回一个 Observable,因此用 rxjs 的 from() 包一层 getMessages 就是整个适配器。

    src/app/lefine.loader.ts
    import { Injectable, inject } from '@angular/core'import { HttpClient } from '@angular/common/http'import { TranslateLoader } from '@ngx-translate/core'import { Observable } from 'rxjs'// TranslateLoader's contract: one Observable of messages per language.// The files are the ones `lefine strings pull` wrote.@Injectable({ providedIn: 'root' })export class LefineLoader implements TranslateLoader {  private http = inject(HttpClient)  getTranslation(lang: string): Observable<Record<string, unknown>> {    return this.http.get<Record<string, unknown>>(`/assets/locales/${lang}.json`)  }}
  3. 03

    注册并在运行时切换

    在 bootstrapApplication 中注册一个 provider,之后 use() 即可切换语言,无需刷新页面,也无需按语言构建。

    src/main.ts
    import { bootstrapApplication } from '@angular/platform-browser'import { importProvidersFrom } from '@angular/core'import { provideHttpClient } from '@angular/common/http'import { TranslateModule, TranslateLoader } from '@ngx-translate/core'import { AppComponent } from './app/app.component'import { LefineLoader } from './app/lefine.loader'bootstrapApplication(AppComponent, {  providers: [    provideHttpClient(),    importProvidersFrom(      TranslateModule.forRoot({        defaultLanguage: 'en',        loader: { provide: TranslateLoader, useClass: LefineLoader },      })    ),  ],})
02/Switching locale

快速开始

使用管道和服务将 i18n 添加到您的 Angular 应用程序中。

src/app/locale-switcher.component.ts
import { Component, inject } from '@angular/core'import { TranslateModule, TranslateService } from '@ngx-translate/core'@Component({  selector: 'app-locale-switcher',  standalone: true,  imports: [TranslateModule],  template: `    <select (change)="switchTo($any($event.target).value)">      <option *ngFor="let l of languages" [value]="l.code">{{ l.name }}</option>    </select>  `,})export class LocaleSwitcherComponent {  private translate = inject(TranslateService)  // The locales you pulled, one file each.  languages = [    { code: 'en', name: 'English' },    { code: 'de', name: 'Deutsch' },  ]  switchTo(lang: string) {    // use() calls the loader, which GETs /assets/locales/<lang>.json.    this.translate.use(lang)  }}
03/Capabilities

功能

  • 独立组件支持
  • Signals 支持
  • 翻译管道
  • i18n 指令
  • 可注入服务
  • 延迟加载模块
  • Angular SSR(@angular/ssr)
  • AOT 编译支持
  • 无需更改 Angular CLI:只需一个 loader 和一个 provider
04/Works with

兼容热门 Angular i18n 库

Lefine 补充你的 Angular i18n 库 — 可视化管理翻译、与翻译人员协作,并通过 CDN 部署。

  • @ngx-translate/core

    使用最广泛的 Angular 翻译库。通过管道、指令和服务注入实现运行时翻译。

    自定义 TranslateLoader 从 Lefine CDN 读取你的消息。在仪表板中编辑翻译并发布——无需重新构建。

  • Angular i18n (built-in)

    Angular 官方 i18n 系统,支持 AOT 编译、ICU 表达式和构建时翻译提取。

    将您的 Angular XLIFF 文件导入 Lefine 来管理翻译工作流;Angular CLI 仍负责构建特定语言环境的包。

  • Transloco

    现代、轻量级的 Angular i18n 库,支持延迟加载、丰富的插件和出色的 TypeScript 支持。

    通过 Lefine 的 GitHub 集成(beta)同步 Transloco JSON 文件,或在运行时从 CDN 加载。

How it works

Angular 中的文案从哪里来

不需要按语言分包:loader 在运行时解析语言,由 edge 返回结果。

Read path — every locale load
Your Angular app

The translate pipe reads what TranslateService already loaded.

0 network calls per render

lefine

The TranslateLoader reads the file lefine strings pull wrote for each language.

60 s cache · 0 deps

CDN edge

Cloudflare worker answers from the nearest edge cache.

max-age=60 · never a 5xx

R2 object store

The published translation files the sync worker wrote.

source of truth

Fallback chain — tried in order when a hop fails
  • 01In-memory TTL cache
  • 02CDN fetch, with timeout and one retry
  • 03Persistent storage, if configured
  • 04staticData bundled with the app
  • 05Throw — after everything above missed
Write path — dashboard to app
AI or translator

Proposal reviewed in the dashboard, glossary enforced.

MCP · dashboard · CLI

Publish

Sync worker writes the locale files to R2.

lefine strings publish

CDN purge

Fire-and-forget purge of the affected keys and the manifest.

non-critical by design

Live in the app

The next getMessages() past the TTL returns the new copy.

~5 min worst case

0dependencies in core
5 minSDK cache in production; edge 60s
200CDN status, even on failure
5fallback layers before an error
Get started

免费开始。

开始使用 Angular i18n 构建

提供免费层级。无需信用卡。