0.0.4 • Published 7 months ago

@allakando/allakando-i18n v0.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

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
0.0.4

7 months ago

0.0.3

7 months ago

0.0.2

7 months ago