just for you ๐
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
{
// 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
// 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.