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.
Install the package using npm:
npm install ts-iTranslate
- Importing
import I18n from "ts-iTranslate";
- Define your translation maps for each language:
const translations = {
en: {
greeting: "Hello!",
},
ar: {
greeting: "مرحبًا!",
},
};
- Create an instance of the I18n class with the default language and translations:
const i18n = new I18n("en", translations);
- Set the current language, for example, based on the user's preferred language:
i18n.setLanguage("ar");
- Use the translate function to get localized strings:
const greeting = i18n.translate("greeting");
console.log(greeting); // Outputs "مرحبًا!"