/ts-iTranslate

Effortlessly manage internationalization in TypeScript with ts-iTranslate.

Primary LanguageTypeScript

ts-iTranslate

ts-iTranslate is a lightweight TypeScript library for seamless internationalization (i18n) in web applications. It offers simple language switching and translation support, making it easy to create multilingual user interfaces.

Installation

Install the package using npm:

npm install ts-iTranslate

Usage

  • Importing
import I18n from "ts-iTranslate";

Setting up Translation Maps

  • Define your translation maps for each language:
const translations = {
  en: {
    greeting: "Hello!",
  },
  ar: {
    greeting: "مرحبًا!",
  },
};

Creating an Instance

  • Create an instance of the I18n class with the default language and translations:
const i18n = new I18n("en", translations);

Setting the Current Language

  • Set the current language, for example, based on the user's preferred language:
i18n.setLanguage("ar");

Translating Strings

  • Use the translate function to get localized strings:
const greeting = i18n.translate("greeting");

console.log(greeting); // Outputs "مرحبًا!"

License