1.1.0 • Published 6 months ago
typelingo v1.1.0
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
- Fully type-safe translations
- No dependencies
- 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