Angular i18n ด้วย Transloco : คู่มือการตั้งค่าและแปล
ตั้งแต่ติดตั้งจนถึงระบบจริง กำหนดค่า Transloco จัดการพหูพจน์ด้วยไวยากรณ์ ICU เพิ่มการใช้ภาษาสำรองอัจฉริยะ แล้วทำให้การแปลเป็นอัตโนมัติด้วย AI
ติดตั้ง Transloco
Transloco เป็นไลบรารี i18n ของบุคคลที่สามยอดนิยมที่สุดสำหรับ Angular มีการโหลดคำแปลขณะรัน รองรับรูปแบบข้อความ ICU ขอบเขตที่โหลดแบบหน่วงเวลา และ API เทมเพลตที่สะอาดพร้อมทั้งคำสั่งเชิงโครงสร้างและไปป์
npm install @jsverse/translocoกำหนดค่า Transloco
ลงทะเบียน Transloco ในการกำหนดค่าแอปพลิเคชัน คุณต้องระบุภาษาที่มี ตั้งภาษาเริ่มต้น และกำหนดค่าตัวโหลดคำแปล Transloco รองรับทั้งคอมโพเนนต์แบบสแตนด์อโลน (Angular 14+) และรูปแบบ NgModule
คอมโพเนนต์แบบสแตนด์อโลน (แนะนำ)
// app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideHttpClient } from '@angular/common/http';
import {
provideTransloco,
TranslocoHttpLoader,
} from '@jsverse/transloco';
export const appConfig: ApplicationConfig = {
providers: [
provideHttpClient(),
provideTransloco({
config: {
availableLangs: ['en', 'de', 'ja', 'es', 'fr'],
defaultLang: 'en',
fallbackLang: 'en',
reRenderOnLangChange: true,
prodMode: true,
},
loader: TranslocoHttpLoader,
}),
],
};รูปแบบ NgModule
// app.module.ts
import { NgModule } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {
TranslocoModule,
TRANSLOCO_LOADER,
TranslocoHttpLoader,
provideTransloco,
} from '@jsverse/transloco';
@NgModule({
imports: [TranslocoModule],
providers: [
provideTransloco({
config: {
availableLangs: ['en', 'de', 'ja', 'es', 'fr'],
defaultLang: 'en',
fallbackLang: 'en',
reRenderOnLangChange: true,
prodMode: true,
},
loader: TranslocoHttpLoader,
}),
],
})
export class AppModule {}สร้างไฟล์แปล
สร้างไฟล์ JSON หนึ่งไฟล์ต่อภาษาใน src/assets/i18n/ ใช้คีย์ซ้อนเพื่อจัดระเบียบข้อความตามฟีเจอร์ Transloco ใช้รูปแบบข้อความ ICU สำหรับพหูพจน์และตัวแปร
// assets/i18n/en.json
{
"nav": {
"home": "Home",
"about": "About",
"settings": "Settings"
},
"greeting": "Hello, {{ name }}!",
"cart": {
"itemCount": "{count, plural, one {# item} other {# items}}"
}
}
// assets/i18n/de.json
{
"nav": {
"home": "Startseite",
"about": "Uber uns",
"settings": "Einstellungen"
},
"greeting": "Hallo, {{ name }}!",
"cart": {
"itemCount": "{count, plural, one {# Artikel} other {# Artikel}}"
}
}ใช้คำแปลในเทมเพลตและบริการ
Transloco มีสามวิธีในการแปลในเทมเพลต ได้แก่ คำสั่งเชิงโครงสร้าง (*transloco) ไปป์ (| transloco) และบริการ (TranslocoService) สำหรับโค้ด TypeScript แนะนำให้ใช้คำสั่งเชิงโครงสร้างในกรณีส่วนใหญ่ เพราะสร้างการสมัครเพียงครั้งเดียวและให้ฟังก์ชันแปลกับบล็อกเทมเพลตทั้งหมด
การแปลในเทมเพลต
<!-- Using the transloco directive (recommended) -->
<ng-container *transloco="let t">
<h1>{{ t('greeting', { name: userName }) }}</h1>
<nav>
<a routerLink="/">{{ t('nav.home') }}</a>
<a routerLink="/about">{{ t('nav.about') }}</a>
</nav>
</ng-container>
<!-- Using the transloco pipe -->
<h1>{{ 'greeting' | transloco:{ name: userName } }}</h1>
<!-- Using the structural directive with read -->
<ng-container *transloco="let t; read: 'nav'">
<a routerLink="/">{{ t('home') }}</a>
<a routerLink="/about">{{ t('about') }}</a>
</ng-container>การแปลในบริการ (TypeScript)
import { Component, inject } from '@angular/core';
import { TranslocoService } from '@jsverse/transloco';
@Component({
selector: 'app-notification',
template: '<span>{{ message }}</span>',
})
export class NotificationComponent {
private translocoService = inject(TranslocoService);
message = '';
showSuccess() {
// Translate in TypeScript
this.message = this.translocoService.translate('notifications.saved');
}
switchLanguage(lang: string) {
this.translocoService.setActiveLang(lang);
}
}จัดการพหูพจน์ด้วยรูปแบบข้อความ ICU
Transloco ใช้รูปแบบข้อความ ICU สำหรับพหูพจน์และนิพจน์ select โดย ICU จัดการกฎพหูพจน์ซับซ้อนอัตโนมัติ ทั้งภาษาอาหรับ 6 รูป รัสเซีย 3 รูป และญี่ปุ่น 1 รูปจากข้อความเดียว กำหนดกฎพหูพจน์ในไฟล์แปล แล้ว Transloco จะเลือกรูปแบบที่ถูกต้องขณะรัน
// assets/i18n/en.json
{
"cart": {
"itemCount": "{count, plural, one {# item} other {# items}}",
"emptyMessage": "Your cart is empty"
},
"notifications": {
"unread": "{count, plural, =0 {No new notifications} one {# new notification} other {# new notifications}}"
}
}
// assets/i18n/ar.json — Arabic has 6 plural forms
{
"cart": {
"itemCount": "{count, plural, zero {لا عناصر} one {عنصر واحد} two {عنصران} few {# عناصر} many {# عنصرًا} other {# عنصر}}"
}
}
// Template usage:
// <span>{{ t('cart.itemCount', { count: cartItems.length }) }}</span>การใช้ภาษาสำรองอัจฉริยะด้วย angular-locale-chain
ตามค่าเริ่มต้น Transloco จะถอยไปใช้ภาษาเริ่มต้นเมื่อไม่มีคีย์คำแปล ผู้ใช้ pt-BR จึงเห็นภาษาอังกฤษแทนคำแปล pt-PT ที่ใช้ได้ดี angular-locale-chain แก้ปัญหานี้ด้วยการผสานคำแปลเชิงลึกตามลำดับภาษาสำรองที่กำหนดค่าได้ก่อนส่งให้ Transloco ทุกคีย์จึงครบ ไม่มีช่องว่างหรือคำแปลที่หายไป
npm install angular-locale-chain// app.config.ts — with angular-locale-chain
import { ApplicationConfig } from '@angular/core';
import { provideHttpClient } from '@angular/common/http';
import {
provideTransloco,
TranslocoHttpLoader,
TRANSLOCO_LOADER,
TRANSLOCO_FALLBACK_STRATEGY,
} from '@jsverse/transloco';
import {
LocaleChainLoader,
LocaleChainFallbackStrategy,
} from 'angular-locale-chain';
export const appConfig: ApplicationConfig = {
providers: [
provideHttpClient(),
provideTransloco({
config: {
availableLangs: ['en', 'fr', 'fr-CA', 'pt', 'pt-BR', 'de', 'de-AT'],
defaultLang: 'en',
fallbackLang: 'en',
reRenderOnLangChange: true,
prodMode: true,
},
}),
{
provide: TRANSLOCO_LOADER,
useFactory: () => {
const inner = new TranslocoHttpLoader();
return new LocaleChainLoader(inner, {
defaultLocale: 'en',
});
},
},
{
provide: TRANSLOCO_FALLBACK_STRATEGY,
useFactory: () => new LocaleChainFallbackStrategy(),
},
],
};โครงสร้างไฟล์ที่แนะนำ
my-angular-app/
├── src/
│ ├── assets/
│ │ └── i18n/
│ │ ├── en.json # Source language
│ │ ├── de.json # German
│ │ ├── ja.json # Japanese
│ │ ├── es.json # Spanish
│ │ └── fr.json # French
│ ├── app/
│ │ ├── app.config.ts # Transloco provider config
│ │ ├── app.component.ts
│ │ └── components/
│ │ └── lang-switcher/
│ │ └── lang-switcher.component.ts
│ └── main.ts
├── angular.json
└── package.jsonทำให้การแปลเป็นอัตโนมัติ
เมื่อตั้งค่า Transloco เสร็จแล้ว ให้แปลไฟล์ภาษาด้วย AI โดยบอกผู้ช่วย AI ใน IDE ให้แปลไฟล์ JSON ต้นฉบับ หรือใช้ CLI ของ i18n Agent ในไปป์ไลน์ CI/CD เพื่อทำโลคัลไลเซชันอัตโนมัติเต็มรูปแบบ
# In your IDE, ask your AI assistant:
> Translate src/assets/i18n/en.json to German, Japanese, and Spanish
✓ de.json created (1.2s)
✓ ja.json created (1.5s)
✓ es.json created (1.1s)
# Or use the CLI in CI/CD:
npx i18n-agent translate src/assets/i18n/en.json --lang de,ja,esทำให้คุณภาพการแปลเป็นอัตโนมัติ
ข้อผิดพลาดที่พบบ่อย
คำแปลแสดงคีย์ดิบ
ค้นหาคีย์ตามขอบเขตไม่สำเร็จ
คำเตือนในคอนโซลของระบบจริง
คำแปลกะพริบเมื่อเปลี่ยนเส้นทาง
ผู้ใช้รายภูมิภาคเห็นภาษาอังกฤษแทนภาษาหลัก
ลองใช้ i18n Agent ตอนนี้
ลากและวางไฟล์แปลของคุณที่นี่
JSON, YAML, PO, XML, CSV, Markdown, Properties
หรือคลิกเพื่อเลือกไฟล์
ภาษาเป้าหมาย
การใช้ภาษาสำรองด้วย angular-locale-chain
เมื่อไม่มีคีย์คำแปลในภาษาตามภูมิภาคอย่าง pt-BR TranslocoLoader ของ Angular จะข้ามไปใช้ภาษาเริ่มต้นทันทีแทนที่จะตรวจภาษาหลัก pt ก่อน
npm install angular-locale-chainimport { LocaleChainLoader } from 'angular-locale-chain';
new LocaleChainLoader(innerLoader, {
fallbacks: {
'pt-BR': ['pt', 'en'],
'zh-Hant-HK': ['zh-Hant', 'zh', 'en'],
},
defaultLocale: 'en',
});ดูคู่มือการใช้ภาษาสำรองของเราสำหรับรายการเฟรมเวิร์กที่รองรับทั้งหมดและลำดับสำเร็จรูป 75 รายการ Learn more →