typelingo
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

TypeLingo

Build and use fully type-safe translations in your app.

Introduction to TypeLingo

TypeLingo is a library that allows you to build and use fully type-safe translations in your app. It is built on top of the TypeScript type system and is designed to be used with the TypeScript programming language.

Features

  • [x] Fully type-safe translations
  • [x] No dependencies
  • [x] Well-tested and production-ready
npm install typelingo

Usage

First create an instance of TypeLingo

import { TypeLingo } from "typelingo";

const locales = ["en", "ba", "de"] as const;
const currentLocale = "en";

const typelingo = new TypeLingo({
  locales,
  currentLocale,
});

Then define your translations

const greet = typelingo.create({
  en: "Hello",
  ba: "Zdravo",
  de: "Hallo",
} as const);

console.log(greet.get({})); // Hello

Variables in translations

const greet = typelingo.create({
  en: "Hello, {name}",
  ba: "Zdravo, {name}",
  de: "Hallo, {name}",
} as const);

console.log(greet.get({ name: "Aldin" })); // Hello, Aldin

Variations in translations

const greet = typelingo
  .create({
    en: "Hello, {name}!",
    ba: "Zdravo, {name}!",
    de: "Hallo, {name}!",
  } as const)
  .variation(({ name }) => name === "Aldin", {
    en: "Hello, {name}! You are {age} years old",
    ba: "Zdravo, {name}! Ti imaš {age} godina",
    de: "Hallo, {name}! Du bist {age} Jahre alt",
  } as const);

console.log(greet.get({ name: "John", age: 18 })); // Hello, John!
console.log(greet.get({ name: "Aldin", age: 18 })); // Hello, Aldin! You are 18 years old

Dynamic locale change

import { TypeLingo } from "typelingo";

const locales = ["en", "ba", "de"] as const;
const currentLocale = "en";

const typelingo = new TypeLingo({
  locales,
  currentLocale,
});

const greet = typelingo.create({
  en: "Hello",
  ba: "Zdravo",
  de: "Hallo",
} as const);

console.log(greet.get({})); // Hello

typelingo.changeLocale("ba");

console.log(greet.get({})); // Zdravo

Locales can also be passed as an parameter to the get method.

console.log(greet.get({}, "de")); // Hallo

Package Sidebar

Install

npm i typelingo

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

8.47 kB

Total Files

9

Last publish

Collaborators

  • devcamdzic