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

@safekit/i18n

v0.5.0

Published

A lightweight type-safe i18n library with interpolation support and namespace scoping

Downloads

285

Readme

@safekit/i18n

⚠️ This package is in active development. Expect breaking changes between versions.

A lightweight, type-safe internationalization (i18n) library for TypeScript applications with support for interpolation and namespace scoping.

npm version TypeScript License: MIT

Features

  • 🔒 Type-safe: Full TypeScript support with compile-time key validation
  • 🔧 Interpolation: Support for parameterized translations with type checking
  • 📦 Namespace scoping: Organize translations by namespace for better maintainability
  • 🪶 Lightweight: Zero dependencies and minimal runtime overhead
  • 🎯 IntelliSense: Full autocomplete support for translation keys and parameters
  • 📁 TypeScript-first: Works with TypeScript (as const) for maximum type safety
  • 🌍 Multi-language: Dynamic locale loading with preserved type safety

Installation

npm install @safekit/i18n
yarn add @safekit/i18n
bun add @safekit/i18n

createTranslator

The main function for creating a translator with type-safe translation keys and interpolation.

import { createTranslator } from '@safekit/i18n';

const translations = {
  "welcome": "Welcome!",
  "greeting": "Hello {{name}}!",
  "tasks.count": "You have {{count}} tasks"
} as const;

const t = createTranslator(translations);

// ✅ Type-safe usage
t("welcome"); // "Welcome!"
t("greeting", { name: "Alice" }); // "Hello Alice!"
t("tasks.count", { count: 5 }); // "You have 5 tasks"

// ❌ Compile-time errors (with TypeScript translations)
t("invalid.key"); // Error: invalid key
t("greeting"); // Error: missing required parameter 'name'
t("greeting", { wrongParam: "test" }); // Error: invalid parameter

getFixedT

Create a scoped translator for a specific namespace to avoid repeating prefixes.

import { getFixedT } from '@safekit/i18n';

const translations = {
  "user.greeting": "Hello {{name}}!",
  "user.welcome": "Welcome {{firstName}} {{lastName}}!",
  "form.validation.required": "{{field}} is required",
  "form.validation.email": "Please enter a valid email"
} as const;

// Create scoped translators
const tUser = getFixedT(translations, "user");
const tForm = getFixedT(translations, "form.validation");

// Use without namespace prefix
tUser("greeting", { name: "Sarah" }); // "Hello Sarah!"
tUser("welcome", { firstName: "Emma", lastName: "Smith" }); // "Welcome Emma Smith!"
tForm("required", { field: "Password" }); // "Password is required"
tForm("email"); // "Please enter a valid email"

Multi-language Support

For dynamic translation loading with multiple locales:

// Define the schema that all translations must satisfy
export type TranslationSchema = {
  "app.title": string;
  "user.greeting": string;
  "user.welcome": string;
  "tasks.count": string;
};

// Define each translation
export const enUS = {
  "app.title": "Task Manager",
  "user.greeting": "Hello {{name}}!",
  "user.welcome": "Welcome {{firstName}} {{lastName}}!",
  "tasks.count": "You have {{count}} tasks"
} as const satisfies TranslationSchema;

export const esES = {
  "app.title": "Gestor de Tareas",
  "user.greeting": "¡Hola {{name}}!",
  "user.welcome": "¡Bienvenido {{firstName}} {{lastName}}!",
  "tasks.count": "Tienes {{count}} tareas"
} as const satisfies TranslationSchema;

// Supported locales
export type SupportedLocale = 'en-US' | 'es-ES' | 'fr-FR' | 'zh-CN';

// Async loader functions (simulating dynamic imports)
const translationLoaders = {
  'en-US': async () => enUS,
  'es-ES': async () => esES,
  'fr-FR': async () => frFR,
  'zh-CN': async () => zhCN,
} as const;

/**
 * Dynamically loads translations for the specified locale with preserved literal types
 */
export const getTranslations = async (locale: SupportedLocale) => {
  const loader = translationLoaders[locale];
  return await loader();
};

// Export the type for static usage
export type Translation = typeof enUS;

// Usage
async function setupI18n(locale: SupportedLocale) {
  const translations = await getTranslations(locale);
  const t = createTranslator(translations);

  t("user.greeting", { name: "Maria" });
  // en-US: "Hello Maria!"
  // es-ES: "¡Hola Maria!"
}

API Reference

createTranslator(translations, options?)

Creates a translator function with type-safe key validation and interpolation support.

Options:

  • silent?: boolean - Disable warning logs (default: auto-detect based on NODE_ENV)

getFixedT(translations, namespace, options?)

Creates a scoped translator for a specific namespace, allowing shorter key names.

Options:

  • Same as createTranslator

Fallback Behavior

  • Missing keys: Returns the key itself as a string (never undefined/null)
  • With $defaultValue: Returns the provided default value
  • Warnings: Logged in development, silent in production (unless overridden)
// @ts-expect-error - runtime behavior
t("missing.key"); // Returns "missing.key" (with warning in dev)
t("missing.key", { $defaultValue: "Fallback" }); // Returns "Fallback"

// Disable warnings
const t = createTranslator(translations, { silent: true });

Workflows

For comprehensive guides on translation workflows, see Translation Workflows.

Examples

See the examples directory for comprehensive usage examples:

  • examples/ts/ - TypeScript translations (recommended when starting out)
  • examples/json-to-ts-codegen/ - JSON → TypeScript code generation workflow (for large teams)
  • examples/ts-to-json-codegen/ - TypeScript → JSON code generation workflow
  • examples/json-direct/ - Direct JSON imports (reference only - not recommended)

Code Generation

For teams using different translation workflows:

Contributing

See CONTRIBUTING.md for contribution guidelines.

License

MIT © safekit