@allakando/allakando-i18n
TypeScript icon, indicating that this package has built-in type declarations

0.0.4 • Public • Published

Allakando i18n

This library contains aims at making internationalization easy and straight forward while also mainting type safety in projects.

Translator

The translatorAPI allows for creating translations files for different languages that are type safe. The translator can be used with pure JS, but in that case it will not provide the same level of rigidity. It is recommended to implement it using TS.

In order to create a translator instance you first need to define what locales are available, as well as what a translations map should look like. You can then write implementations for each locale that matches the translations map template and provide these to the translator class constructor.

import { TranslatorAPI, TBaseTranslationsMap } from "@allakando/allakando-i18n";

type TAvailableLocales = "en" | "sv"
interface MyTranslationsMap extends TBaseTranslationsMap {
	HELLO: () => string,
    THANK_YOU_WE_WILL_BE_IN_TOUCH: (name: string) => string
}

const translationMappings = {
    "en": {
	    HELLO: () => "Hello World!",
        THANK_YOU_WE_WILL_BE_IN_TOUCH: (name: string) => `Thank you ${name}, we will be in touch shortly!`
    },
    "sv": {
        HELLO: () => "Hej Värld!",
        THANK_YOU_WE_WILL_BE_IN_TOUCH: (name: string) => `Tack ${name}, vi kommer att ta höra av oss i närtid!`
    },
}

const translator = new Translator<MyTranslationsMap, TAvailableLocales>(translationMappings, "en") //The second argument is the default locale
translator.translate("HELLO")() // -> "Hello World!"
translator.setLanguage("sv")
translator.translate("THANK_YOU_WE_WILL_BE_IN_TOUCH")("Chris") // -> "Tack Chris, vi kommer att ta höra av oss i närtid!"

API

  • setLanguage(locale: string) -> Sets the active language of the the translator
  • getTranslations() -> Returns a (type safe) object literal of all translations for the current locale
  • enableDebugMode() -> Enables debug mode where the translator will always return the key for the translation rather than the value
  • disableDebugMode() -> Disables debug mode

Readme

Keywords

Package Sidebar

Install

npm i @allakando/allakando-i18n

Weekly Downloads

17

Version

0.0.4

License

MIT

Unpacked Size

8.65 kB

Total Files

7

Last publish

Collaborators

  • allakandoserviceaccount
  • saikat.ghosh