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 🙏

© 2025 – Pkg Stats / Ryan Hefner

tstlai

v1.2.6

Published

Just-In-Time AI localization engine with intelligent caching

Downloads

4,061

Readme

tstlai: Just-In-Time AI Localization

tstlai is middleware for Just-In-Time AI localization with intelligent caching to minimize costs and latency.

Why tstlai?

Traditional i18n requires maintaining separate JSON files for each language - extracting strings, syncing changes across files, and coordinating with translators. As your app grows, this becomes a maintenance burden that slows down development.

tstlai takes a different approach:

  • One source of truth - Maintain a single JSON file or write inline. Either way, you never sync multiple language files.
  • Add languages instantly - One line of config. No translation workflow, no waiting on translators.
  • Context-aware accuracy - Unlike standard auto-translation, AI understands context. "Save" translates correctly whether it's a button, a discount, or a rescue mission.
  • Pay once per string - Intelligent caching means each unique string is translated only once.

How It Works

  1. You write your app in your source language
  2. tstlai intercepts text at render time (or build time)
  3. New strings are translated via AI and cached
  4. Cached translations are served instantly on subsequent requests

The result: your app supports any language without changing your development workflow.

Features

  • HTML-Safe - Only text nodes are translated; structure, classes, and attributes stay intact
  • RTL Support - Automatic dir and lang attributes for Arabic, Hebrew, Persian, etc.
  • Flexible Caching - Redis for production, in-memory for development
  • Multiple Integration Modes - Auto-translate, server-side, or CLI generation at build time
  • Framework Support - Next.js, Express, Fastify, Astro, Remix

Installation

npm install tstlai

Quick Start

import { Tstlai } from 'tstlai';

const translator = new Tstlai({
  targetLang: 'es',
  provider: { type: 'openai', apiKey: process.env.OPENAI_API_KEY },
  cache: { type: 'memory' },
});

// Translate text
const translated = await translator.translateText('Hello, world!');

// Or process full HTML
const result = await translator.process('<h1>Hello</h1>');
console.log(result.html); // <h1>Hola</h1>

Documentation

| Guide | Description | | :----------------------------------------------------------- | :----------------------------------------- | | Quick Start | 5 minutes to a translated app | | Configuration | All options, env vars, caching | | Next.js Integration | Auto-translate, SSR, next-intl migration | | CLI Generate | Static file generation for CI/CD | | Frameworks | Express, Fastify, Astro, Remix | | Caching | Redis setup, multi-language keys |

Integration Methods

| Method | Best For | Setup | | :-------------------- | :------------------------- | :-------------------- | | Auto-Translate | Legacy apps, fastest setup | Drop-in component | | Page Translations | New projects | List strings upfront | | JSON Adapter | next-intl migration | Use existing JSON | | CLI Generate | Static sites, mobile apps | Build-time generation |

Environment Variables

| Variable | Description | Default | | :---------------- | :--------------- | :-------------------------- | | OPENAI_API_KEY | OpenAI API Key | — | | OPENAI_MODEL | Model ID | gpt-3.5-turbo | | OPENAI_BASE_URL | Custom endpoint | https://api.openai.com/v1 | | REDIS_URL | Redis connection | redis://localhost:6379 |

CLI

# Generate Spanish and French from English source
npx tstlai generate -i locales/en.json -o locales/ -l es,fr

# With context for better quality
npx tstlai generate -i en.json -l de,ja -c "E-commerce website"

Preventing Translation

<span data-no-translate>BrandName™</span>

Or use excludedTerms in config for global exclusions.

Security

tstlai includes built-in protections and provides guidance for secure deployments:

Built-in Protections

  • XSS Safe - Translations are applied via textContent, not innerHTML
  • Request Limits - Route handlers enforce default limits (100 texts, 100K chars per request)
  • Production Warnings - Console warnings alert you to add rate limiting

Safest Option: Server-Side Only

Use createPageTranslations or CLI generate to avoid exposing any public endpoint:

// No public API needed—runs entirely on your server
const t = await createPageTranslations(translator, ['Welcome', 'About us']);
return <h1>{t('Welcome')}</h1>;

If Using Client-Side Translation

When exposing translation endpoints (e.g., for AutoTranslate), you must implement:

  • Rate limiting - Prevent API abuse and cost overruns
  • Origin validation - Restrict access to your domains
  • Monitoring - Track usage and set billing alerts with your AI provider
// Example: Customize limits
export const POST = createNextRouteHandler(translator, {
  maxTexts: 50, // Stricter limit
  maxTotalChars: 25000,
});

⚠️ Without rate limiting, your endpoint is exploitable. An attacker can use your AI credits for free translations.

📖 Full Security Guide - Rate limiting examples, origin validation, complete protected route implementation.

License

MIT


tstlai is developed by Zaguán AI, a 100% OpenAI-compatible API that makes it easy to switch between models.