npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@kharradp/translation

v1.0.20

Published

Centralized translation & master data enrichment with Redis + Elasticsearch cache

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 _id references
  • lang: string → Language code ("en", "hi", "fr", etc.)
  • masterKeys: string[] → Paths to fields that should be translated
  • indexMap?: 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

  1. Collects all _ids for given master keys.
  2. Tries Redis cache (MGET) first.
  3. Falls back to Elasticsearch mget for missing entries.
  4. Updates Redis cache (with TTL).
  5. Replaces _id references with localized values in records.

📄 License

Zentek ©2025