Transloco के साथ Angular i18n: सेटअप और अनुवाद गाइड
इंस्टॉलेशन से प्रोडक्शन तक: Transloco कॉन्फ़िगर करें, ICU सिंटैक्स से बहुवचन संभालें, स्मार्ट locale fallback जोड़ें और AI से अनुवाद ऑटोमेट करें।
Transloco इंस्टॉल करें
Transloco, Angular के लिए सबसे लोकप्रिय थर्ड-पार्टी i18n लाइब्रेरी है। यह रनटाइम पर अनुवाद लोड करने, ICU message format के समर्थन, lazy-loaded scopes और structural directives व pipes—दोनों के साथ एक सुव्यवस्थित template API की सुविधा देता है।
npm install @jsverse/translocoTransloco कॉन्फ़िगर करें
अपने application config में Transloco रजिस्टर करें। आपको उपलब्ध भाषाएँ देनी होंगी, डिफ़ॉल्ट भाषा सेट करनी होगी और translation loader कॉन्फ़िगर करना होगा। Transloco, standalone components (Angular 14+) और NgModule patterns—दोनों का समर्थन करता है।
Standalone Components (सुझाया गया विकल्प)
// 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 {}अनुवाद फ़ाइलें बनाएँ
src/assets/i18n/ में हर भाषा के लिए एक JSON फ़ाइल बनाएँ। स्ट्रिंग्स को सुविधा के अनुसार व्यवस्थित करने के लिए nested keys का उपयोग करें। Transloco बहुवचन और variables के लिए ICU message format का उपयोग करता है।
// 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}}"
}
}Templates और Services में अनुवादों का उपयोग करें
Transloco, templates में अनुवाद करने के तीन तरीके देता है: structural directive (*transloco), pipe (| transloco) और TypeScript code के लिए service (TranslocoService)। अधिकांश उपयोगों के लिए structural directive सुझाया जाता है, क्योंकि यह एक ही subscription बनाता है और पूरे template block को translate function उपलब्ध कराता है।
Template में अनुवाद
<!-- 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>Service में अनुवाद (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 Message Format से बहुवचन संभालें
Transloco बहुवचन और select expressions के लिए ICU message format का उपयोग करता है। ICU जटिल बहुवचन नियमों को अपने-आप संभालता है—अरबी (6 रूप), रूसी (3 रूप), जापानी (1 रूप)—और यह सब एक ही message string से होता है। अपनी अनुवाद फ़ाइलों में बहुवचन नियम निर्धारित करें और 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 के साथ स्मार्ट Locale Fallback
डिफ़ॉल्ट रूप से, कोई translation key न मिलने पर Transloco आपके डिफ़ॉल्ट locale का उपयोग करता है। इस कारण pt-BR यूज़र को बिल्कुल उपयुक्त pt-PT अनुवादों के बजाय अंग्रेज़ी दिखाई देती है। angular-locale-chain, अनुवादों को Transloco को सौंपने से पहले configurable fallback chain से deep-merge करके इसे ठीक करता है। हर key भर जाती है—न कोई खाली जगह, न कोई अनुपलब्ध अनुवाद।
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 का उपयोग करके अपनी locale फ़ाइलों का अनुवाद करें। अपने IDE में AI assistant से अपनी source JSON फ़ाइल का अनुवाद करने को कहें या पूरी तरह ऑटोमेटेड स्थानीयकरण के लिए अपनी CI/CD pipeline में i18n Agent CLI का उपयोग करें।
# 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अनुवाद की गुणवत्ता जाँचने की प्रक्रिया ऑटोमेट करें
आम समस्याएँ
अनुवादों के बजाय Raw Keys दिखाई देना
Scoped Keys का Resolve न होना
प्रोडक्शन में Console Warnings
Route Navigation के दौरान अनुवादों का क्षणभर चमकना
Regional Users को Parent Locale के बजाय अंग्रेज़ी दिखाई देना
i18n Agent अभी आज़माएँ
अपनी अनुवाद फ़ाइल यहाँ छोड़ें
JSON, YAML, PO, XML, CSV, Markdown, Properties
या ब्राउज़ करने के लिए क्लिक करें
लक्षित भाषाएँ
angular-locale-chain के साथ Locale Fallback
जब pt-BR जैसे regional locale में कोई translation key अनुपलब्ध होती है, तो Angular का TranslocoLoader पहले parent locale pt की जाँच करने के बजाय सीधे डिफ़ॉल्ट locale पर चला जाता है।
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',
});समर्थित frameworks और 75 built-in chains की पूरी सूची के लिए हमारी Locale Fallback Guide देखें। Learn more →