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

@tiagogcastro/ai-i18n-translate

v1.1.2

Published

A TypeScript library to automatically translate JSON localization files while preserving the base language key structure and order using AI.

Readme

AI-i18n-Translate

Automate the translation of your internationalization (i18n) JSON files using AI. This library integrates easily with OpenAI (or other AI providers) to translate multiple locales while preserving keys, placeholders, and terminology.


Features

  • Automatic translation of i18n JSON files.
  • Supports multiple languages and regional variants (e.g., en-GB, es-MX).
  • Preserves placeholders ({{count}}, {username}, etc.) and JSON structure.
  • Creates target locale files automatically if they do not exist.
  • Optional translation summary report generation.
  • Works with OpenAI by default; other AI providers can be used by implementing a compatible translateChunk function.
  • Flexible folder and file structure for locales.
  • Supports chunked translation; a chunkSize of 10 is a good default for most projects.
  • Contextualizing your application and terminology improves translation quality and prevents errors.

Folder Structure

You can organize your locale files however you prefer. Examples:

locales/{locale}/home/page.json       -> locales/en/home/page.json
locales/{locale}/home-page.json       -> locales/en/home-page.json
locales/{locale}.json                 -> locales/en.json

Folder and file names do not matter. The library automatically finds all .json files.
Important: You must define a baseLocale. Files that do not exist under the baseLocale will not be translated because the source content cannot be determined.


Installation

npm install @tiagogcastro/ai-i18n-translate
# or
yarn add @tiagogcastro/ai-i18n-translate

Usage

Basic Example with OpenAI

import { runTranslation } from '@tiagogcastro/ai-i18n-translate';
import path from 'path';

(async () => {
  await runTranslation({
    baseLocale: "en", // Source language
    targetLocales: ["fr", "es-MX"], // Languages to translate to
    OPENAI_API_KEY: process.env.OPENAI_API_KEY as string,
    model: "gpt-4o-mini", // AI model for translation
    basePath: path.resolve(__dirname, 'locales'), // Path to locale files
    chunkSize: 10, // Keys translated per request; 10 is a good default
    reportOptions: {
      enabled: true,
      outputPath: path.resolve(__dirname, 'translation-summary.md'),
    },
    context: `
Terminology:
- Keep product names and brands unchanged

Placeholders:
- Keep all placeholders like {{count}}, {username}, {amount} unchanged

Language-specific guidance:
- "fr" => French: formal, suitable for UI
- "es-MX" => Spanish (Mexico): local expressions

Instructions:
- Return a JSON object preserving all keys from the source.
- Translate each value according to the rules above.
- Do not add explanations or modify keys.
`
  });
})();

Context Explanation

  • baseLocale: The source language of your JSON files.

  • targetLocales: Array of languages to translate to (e.g., en, es-MX, fr). Target locale files are created automatically if they don't exist.

  • OPENAI_API_KEY: Your OpenAI API key.

  • model: The AI model to use for translation.

  • basePath: Path to your locale files; folder/file structure is flexible.

  • chunkSize: Number of keys translated per request. 10 is recommended as a safe default.

  • reportOptions: Optional summary report generation.

    • enabled: Enable or disable the report.
    • outputPath: Path to save the report.
  • context: Optional string for terminology, placeholders, or language-specific rules. Contextualizing your application improves translation quality.


Using a Different AI Provider

runTranslation is built for OpenAI by default. To use another AI provider (Claude, Gemini, etc.), you do not just change the model. You need to implement a compatible translateChunk function that:

  1. Accepts the same inputs: texts, from, to, context (and optionally model or API key).
  2. Returns a Promise resolving to a JSON object with the same keys as the input.

Example generic structure:

import { TranslateChunkFunction, TranslateChunkRequest, runTranslation } from '@tiagogcastro/ai-i18n-translate';

const translateChunkWithOtherAI: TranslateChunkFunction = async ({ texts, from, to, context, systemPrompt }: TranslateChunkRequest) => {
  // Call your AI provider here (Claude, Gemini, AWS Bedrock, etc.)
  // Apply the context and return a JSON object with the same keys
  return { ...texts }; // replace with actual translations
};

await runTranslation({
  baseLocale: 'en',
  targetLocales: ['fr'],
  basePath: './locales',
  chunkSize: 10,
  context: 'Preserve placeholders and terminology',
  translateChunk: translateChunkWithOtherAI
});

Inputs (texts, from, to, context) and outputs (JSON with same keys) must match for the library to work correctly with chunking, reporting, and automatic file creation.


Technologies

  • Node.js
  • TypeScript
  • OpenAI SDK

Author

Developed by Tiago Gonçalves de Castro 🚀