Skip to content

denostack/intlit

Repository files navigation

Intlit

Build Coverage License Language Typescript
JSR version NPM Version Downloads

Elevate your internationalization (i18n) workflow with Intlit: gettext-compatible formatting, simplified plural handling, and first-class TypeScript support.

Features

  • Simple and intuitive API
  • Full TypeScript support for type safety
  • Unified key for singular and plural forms (unlike gettext which often requires separate msgid and msgid_plural)
  • Includes pluralization support (plugins can be optionally added for more features)
  • Easy to integrate with existing projects

Installation

npm install intlit
import { Formatter } from "intlit";

Usage

Basic Usage

const formatter = new Formatter({
  locale: "en",
});

const text = formatter.format("Hello World");

console.log(text); // Output: Hello World

Handling Plurals

Intlit provides support for handling plural forms in different languages, making it easy to create grammatically correct translations.

First, let's see how to set up Formatter instances for English, Korean, and Arabic, including specific message translations for Korean and Arabic. The pluralization capability is available by default.

const formatter = new Formatter({
  locale: "en",
});

// With count = 1 (singular)
console.log(
  formatter.format("You have {count} file{count.other:}s{/}.", { count: 1 }),
); // Output: You have 1 file.

// With count = 2 (plural)
console.log(
  formatter.format("You have {count} file{count.other:}s{/}.", { count: 2 }),
); // Output: You have 2 files.
const messages = {
  "You have {count} file{count.other:}s{/}.": "{count}개의 파일이 있습니다.",
};

const formatter = new Formatter({
  locale: "ko",
  messages,
});

// With count = 1
console.log(
  formatter.format("You have {count} file{count.other:}s{/}.", { count: 1 }),
); // Output: 1개의 파일이 있습니다.
const messages = {
  "You have {count} file{count.other:}s{/}.":
    "{count.zero:}لا يوجد لديك ملفات.{.one:}لديك ملف واحد.{.two:}لديك ملفان.{.few:}لديك {_} ملفات قليلة.{.many:}لديك {_} ملفات كثيرة.{.other:}لديك {_} ملفات.{/}",
};

const formatter = new Formatter({
  locale: "ar",
  messages: messages,
});

// Arabic has multiple plural forms (zero, one, two, few, many, other). 🫢

console.log(
  formatter.format("You have {count} file{count.other:}s{/}.", { count: 0 }),
); // Output: لا يوجد لديك ملفات.

console.log(
  formatter.format("You have {count} file{count.other:}s{/}.", { count: 1 }),
); // Output: لديك ملف واحد.

console.log(
  formatter.format("You have {count} file{count.other:}s{/}.", { count: 2 }),
); // Output: لديك ملفان.

console.log(
  formatter.format("You have {count} file{count.other:}s{/}.", { count: 3 }),
); // Output: لديك 3 ملفات قليلة.

console.log(
  formatter.format("You have {count} file{count.other:}s{/}.", { count: 11 }),
); // Output: لديك 11 ملفات كثيرة.

console.log(
  formatter.format("You have {count} file{count.other:}s{/}.", { count: 100 }),
); // Output: لديك 100 ملفات.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published