nestjs-i18n-client
v2.5.9
Published
NestJS i18n module with dynamic translation loading from external API
Maintainers
Readme
nestjs-i18n-client
A NestJS module built on top of nestjs-i18n that adds dynamic translation loading from external APIs. This package extends the powerful nestjs-i18n library with automatic translation caching, scheduled updates, and robust error handling with retry mechanisms.
Features
- 🏗️ Built on nestjs-i18n: Extends the popular
nestjs-i18nlibrary with additional capabilities - 🔄 Dynamic Translation Loading: Fetch translations from external APIs
- ⏰ Scheduled Updates: Automatic refresh every 3 hours using an internal cron job
- 🔐 API Key Authentication: Secure API communication
- 🔁 Retry & Backoff: Robust error handling with exponential backoff
- 🏥 Health Checks: API health monitoring
- 📦 TypeScript Support: Full TypeScript definitions included
- ✨ Type-Safe Autocomplete: Automatic IntelliSense for translation keys
- 🧪 Testing Support: Easy testing configuration
Installation
npm install nestjs-i18n-clientPeer Dependencies
This package requires the following peer dependencies to be installed in your project:
npm install @nestjs/common @nestjs/core nestjs-i18n rxjs reflect-metadata class-validator class-transformerCompatible Versions:
@nestjs/common: ^10.0.0 || ^11.0.0 || ^12.0.0@nestjs/core: ^10.0.0 || ^11.0.0 || ^12.0.0nestjs-i18n: >=10.6.0 <11.0.0rxjs: ^7.0.0reflect-metadata: ^0.1.13 || ^0.2.0class-validator: ^0.13.0 || ^0.14.0class-transformer: ^0.4.0 || ^0.5.0
Important:
- Make sure to install
reflect-metadataas it's required for NestJS decorators to work properly - The package uses CommonJS format for maximum compatibility with NestJS projects
- NestJS runtime dependencies are declared as peerDependencies to avoid version conflicts
- Scheduled refresh is managed internally and does not require
ScheduleModule.forRoot() - Peer dependency ranges are capped to tested major/minor lines instead of open-ended
>=ranges - The repo keeps an exact dev dependency for
nestjs-i18nand a compatibility test for supported 10.x lines
Quick Start
1. Basic Configuration
import { Module } from '@nestjs/common';
import { I18nClientModule } from 'nestjs-i18n-client';
@Module({
imports: [
I18nClientModule.forRoot({
apiUrl: 'https://your-api.com',
apiKey: 'your-api-key',
defaultLanguage: 'en',
category: 'web', // Optional, defaults to 'web'
}),
],
})
export class AppModule {}Default Resolvers: The module automatically configures:
HeaderResolverwithx-custom-langheaderAcceptLanguageResolverfor browser language detection
2. Async Configuration
import { Module } from '@nestjs/common';
import { I18nClientModule } from 'nestjs-i18n-client';
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
ConfigModule.forRoot(),
I18nClientModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
apiUrl: configService.get('I18N_API_URL'),
apiKey: configService.get('I18N_API_KEY'),
defaultLanguage: configService.get('I18N_DEFAULT_LANGUAGE', 'en'),
category: configService.get('I18N_CATEGORY', 'web'), // Optional, defaults to 'web'
}),
inject: [ConfigService],
}),
],
})
export class AppModule {}3. Using the Service
import { I18nService } from 'nestjs-i18n';
import { Injectable } from '@nestjs/common';
@Injectable()
export class MyService {
constructor(private readonly i18n: I18nService) {}
async getMessage() {
// ✨ Type-safe autocomplete works automatically!
// Start typing: this.i18n.t('success.
const message = this.i18n.t('success.health_status_retrieved_successfully');
return message;
}
}Note: TypeScript autocomplete is automatically generated on module initialization. No additional setup required!
API Endpoints
The module expects your API to provide the following endpoints:
Health Check
GET /healthResponse:
{
"status": "ok",
"timestamp": "2024-01-01T00:00:00Z",
"version": "1.0.0"
}Get All Languages
GET /translations/language?category=webResponse:
{
"success": true,
"timestamp": "2025-10-22T06:31:14.664Z",
"message": "Languages fetched successfully",
"data": {
"languages": ["tr", "en", "de"],
"totalLanguages": 3
}
}Get All Translations
Note: The module loads translations per language using the endpoint below. To get all translations at once, you can use:
GET /translations?category=webQuery Parameters:
category(required): Category of translations (e.g., 'web', 'mobile')
Response:
{
"success": true,
"timestamp": "2025-10-22T06:31:46.058Z",
"data": {
"EN": {
"validation.is_required": "This field is required"
},
"TR": {
"validation.is_required": "Bu alan zorunludur"
}
}
}Get Language Translations
GET /translations?category=web&language=enQuery Parameters:
category(required): Category of translations (e.g., 'web', 'mobile')language(required): Language code (e.g., 'en', 'tr', 'de')
Response:
{
"success": true,
"timestamp": "2025-10-22T06:32:52.116Z",
"data": {
"validation.is_required": "This field is required"
}
}TypeScript Autocomplete
The module automatically generates TypeScript types for your translation keys, providing full IntelliSense autocomplete support in your IDE.
How It Works
- Automatic Generation: When
I18nClientModuleinitializes, it fetches translations from your API and generates TypeScript type definitions - Zero Configuration: Types are generated automatically - no manual setup required
- Watch Mode Optimized: Type generation is optimized for
nest start --watchto prevent restart loops - Module Augmentation: Automatically extends
nestjs-i18n'sI18nService.t()method with type-safe autocomplete
Usage
Simply use I18nService as you normally would - autocomplete works automatically:
import { I18nService } from 'nestjs-i18n';
import { Injectable } from '@nestjs/common';
@Injectable()
export class MyService {
constructor(private readonly i18n: I18nService) {}
getMessage() {
// ✨ IntelliSense will show autocomplete when you type:
// this.i18n.t('success.
const message = this.i18n.t('success.health_status_retrieved_successfully');
// ✅ Fully type-safe with autocomplete!
return message;
}
}Manual Type Generation
If you need to manually generate types (e.g., in CI/CD), you can use the CLI script:
npm run generate:types -- --api-url https://your-api.com --api-key your-key --category web --language enOr use the programmatic API:
import { generateTypesFromAPI } from 'nestjs-i18n-client';
await generateTypesFromAPI({
apiUrl: 'https://your-api.com',
apiKey: 'your-key',
category: 'web',
defaultLanguage: 'en',
});Troubleshooting
Autocomplete not working?
- Make sure your NestJS app has started at least once (types are generated on module initialization)
- Restart your TypeScript server in your IDE (VS Code:
Cmd/Ctrl + Shift + P→ "TypeScript: Restart TS Server") - Check that
node_modules/nestjs-i18n-client/types.d.tsexists and contains your translation keys
Types not generated?
- Check your API URL and API key are correct
- Verify the API endpoint
/translations?category=web&language=enreturns data - Check application logs for type generation errors (they're logged as warnings)
Configuration Options
interface I18nClientModuleOptions {
apiUrl: string; // Base URL of the translation API
apiKey: string; // API key for authentication
defaultLanguage?: string; // Default language code (default: 'en')
category?: string; // Category for translations (default: 'web')
retryConfig?: RetryConfig; // Retry config for manual/scheduled refresh
bootstrapRetryConfig?: RetryConfig; // Retry config for initial app bootstrap (default: no retries)
requestTimeout?: number; // HTTP timeout in ms (default: 5000)
failOnLoadError?: boolean; // Throw when initial loading fails (default: false)
}
interface RetryConfig {
maxRetries?: number; // Max retry attempts (default: 3)
baseDelay?: number; // Base delay in ms (default: 1000)
maxDelay?: number; // Max delay in ms (default: 10000)
backoffMultiplier?: number; // Backoff multiplier (default: 2)
}Initial application bootstrap is fail-open by default: if the translation API
does not respond, the module returns empty fallback translations immediately
instead of blocking the host application through retry chains. Manual and
scheduled refreshes still use retryConfig.
Scheduled Updates
The module automatically refreshes translations every 3 hours using an internal cron job (0 */3 * * *, UTC). It does not register anything through @nestjs/schedule, so applications can use their own ScheduleModule.forRoot() independently. You can also trigger manual refreshes:
// Manual refresh
await this.i18nClient.manualRefresh();
// Check if refresh is in progress
const isRefreshing = this.i18nClient.isRefreshInProgress();Error Handling
The module includes comprehensive error handling:
- Retry Logic: Automatic retries with exponential backoff
- Health Checks: Monitors API availability
- Logging: Detailed logging for debugging
Development
Building the Package
npm run buildPublishing
npm run prepublishOnly
npm publishLicense
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
