@kharradp/translation
v1.0.20
Published
Centralized translation & master data enrichment with Redis + Elasticsearch cache
Maintainers
Readme
📦 Master Data Translation (Redis + Elasticsearch)
A lightweight utility to translate your records with translated master data.
It uses Redis as a cache layer with Elasticsearch fallback for missing data.
✨ Features
- 🔍 Fetch master data by IDs from Elasticsearch
- ⚡ Redis caching for fast repeated lookups
- 🌍 Language-aware (with fallback to English)
- 🔗 Generic Translation function for any record structure
🚀 Installation
npm install Make sure you have Redis and Elasticsearch configured.
Environment Variables
Before using the package, make sure to set the following environment variables in your .env file:
REDIS_URL=redis://localhost:6379
ES_NODE=http://localhost:9200🛠️ Usage
The main function you will use is translateWithMaster.
import { translateWithMaster } from "centralized-translation";
// Example records
const users = [
{ id: 1, state: { _id: 10 }, district: { _id: 100 } },
{ id: 2, state: { _id: 11 }, district: { _id: 101 } },
];
// Enrich with translations
const translated = await translateWithMaster(users, "hi", ["state", "district"]);
/**
* Result example:
* [
* { id: 1, state: "उत्तर प्रदेश", district: "लखनऊ" },
* { id: 2, state: "Madhya Pradesh", district: "Indore" }
* ]
*/📌 API Reference
translateWithMaster<T>(records, lang, masterKeys, indexMap?)
Translate records with localized master data.
Parameters:
records: T[]→ Array of records containing_idreferenceslang: string→ Language code ("en","hi","fr", etc.)masterKeys: string[]→ Paths to fields that should be translatedindexMap?: Record<string, string>→ Optional mapping of field → Elasticsearch index
Returns:
Promise<T[]>→ Array of translated records
🏗️ Example with Index Map
const data = [
{ id: 1, location: { state: { _id: 5 } } },
];
// Map nested field to ES index
const indexMap = {
"location.state": "state",
};
const translated = await translateWithMaster(data, "hi", ["location.state"], indexMap);⚡ How It Works
- Collects all
_ids for given master keys. - Tries Redis cache (
MGET) first. - Falls back to Elasticsearch
mgetfor missing entries. - Updates Redis cache (with TTL).
- Replaces
_idreferences with localized values in records.
📄 License
Zentek ©2025
