Type-Safe Internationalization for TypeScript

1.0.5 · active · verified Sun Apr 19

i18n-ts is a lightweight, type-safe internationalization library designed specifically for TypeScript applications. It allows developers to define translation messages as plain TypeScript objects, leveraging TypeScript's robust type inference system to ensure that message keys and their associated arguments are correctly used at compile time. This approach significantly reduces runtime errors related to missing translations or incorrect parameter usage, a common issue in dynamic i18n systems. The current stable version is 1.0.5, suggesting a relatively mature and stable codebase with a likely slow release cadence. Its key differentiator lies in its deep integration with TypeScript, providing compile-time safety without requiring external build steps or complex configuration files often found in other i18n solutions. It focuses on simplicity and type inference over a feature-rich runtime, making it ideal for projects prioritizing strong typing in their i18n layer.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates defining type-safe translation messages as plain TypeScript objects and using I18nResolver to access them.

import { I18nResolver } from 'i18n-ts';

const en = {
    hello: "Hi!",
    greeting: (name: string) => `Hi ${name}`
};

const de = {
    hello: "Hallo!",
    greeting: (name: string) => `Hallo ${name}`
};

const i18n = {
    en: en,
    de: de,
    default: en
};

// Initialize the resolver with your locales and desired language
const messages = new I18nResolver(i18n, "de").translation;

// TypeScript will infer the precise type of 'messages', providing compile-time safety
console.log(messages.hello); // Output: Hallo!
console.log(messages.greeting("Alice")); // Output: Hallo Alice

// Example of changing the language
const messagesEn = new I18nResolver(i18n, "en").translation;
console.log(messagesEn.hello); // Output: Hi!

view raw JSON →