1.1.3 β€’ Published 1 year ago

homemade-t v1.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Homemade T 🍡

just for you 😎

But how? 🍡

The first thing to do, is importing the homemade-t module.

const { TMaker } = require("homemade-t");
const makeT = new TMaker("en");

makeT makes T functions (obviously). There are several ways of importing data:

const t = makeT.fromJSON(
	{
		// returns a TFunction
		en: {
			"info.helloworld": "Hello world!",
		},
		es: {
			"info.helloworld": "Hola mundo!",
		},
	}
);

or

const [t, setLocale] = makeT.fromJSONFile("./translations.json"); // also returns a TFunction

JSON data format πŸͺ…

{
	// First is language locale (top level)
	"en": {
		// Inside you would put the keys
		"info.hello": "Hello {{adjectives}} {{subject}}!", // "category.key"
		"info.subjects.world": "world",
		"info.adjectives.cool": "cool" // "category.subcategory.key"
	},
	"es": {
		"info.hello": "Hola {{subject}} {{adjectives}}!", // Woah! Adjectives behind???
		"info.subjects.world": "mundo",
		"info.adjectives.cool": "genial"
	}
}

The above data is used as test data below

Usage πŸ«–

// Locale is by default english (defined in init as "en")
console.log(t("info.hello", { subject: t("world") })); // Outputs: Hello world!
console.log(
	t("info.hello", {
		subject: "Monkee πŸ’",
		adjectives: t("info.adjectives.cool"),
	})
); // Outputs: Hello cool Monkee πŸ’!

setLocale("es"); // Change locale to EspaΓ±ol (Spanish)
console.log(t("info.hello", { subject: t("world") })); // Outputs: Hola mundo!
console.log(
	t("info.hello", {
		subject: "Monkee πŸ’",
		adjectives: t("info.adjectives.cool"),
	})
); // Outputs: Hola Monkee πŸ’ genial!

Note that this is only an example and is not a realistic use case.

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago