Skip to content

Neemek/homemade-t

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

24 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

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.

Releases

No releases published

Packages

No packages published