How to use basic translation features in i18next
I'm JS developer with 13 years of professional experience. I'm always happy to teach my craft.
Search for a command to run...
I'm JS developer with 13 years of professional experience. I'm always happy to teach my craft.
No comments yet. Be the first to comment.
I’ve been working in programming for the last 16 years. Let’s see what changes I see in our industry that are enabled by generative AI. Rapid prototyping The last few months, I’ve been working on a set of WordPress plugins, related to event organizat...
Writing unit tests takes time and effort. Nonetheless, many teams insist on writing them anyway—that’s because of the benefits they bring to a project. Those benefits are mainly the following: fast feedback—unit tests speed up each iteration of twea...

Pure functions are the perfect case for unit testing. For a given input, we always expect the same output—there is no internal state involved. Let’s take a look at a few examples and some simple tests that check if the methods work as expected. Jasmi...

Let’s say you have a job interview in a few days. How should you prepare for it so that you can make an informed decision about joining the company, as well as make sure that your interests are taken care of? Prepare your questions An interview is a ...

Creating example projects is a common way of showing your skills to potential employers. Let’s take a look at what’s important to keep in mind when building personal projects with an eye toward impressing prospective employers. Simplicity Building ap...

I'll show you how to use basic translation features in i18next:
We start with the code in the previous step. It's already set up for the node & browser use.
The complete code to be placed in in src/index.js:
import i18next from "i18next";
const en = {
translation: {
hello_world: "hello world",
nested: {
key: "This key is was read from nested object",
},
great: "Hello {{name}}",
},
};
i18next
.init({
lng: "en", // if you're using a language detector, do not define the lng option
resources: {
en,
},
})
.then((t) => {
console.log(t("hello_world"));
console.log(t("nested.key"));
console.log(t("great", { name: "Marcin" }));
});
It allows us to organize our keys in some logical structure. For example, we could have something like:
{
"dialogBox": {
"close": "Close"
"ok": "OK"
},
"error": {
"notEnoughSpace": "Not enough space"
}
}
The basic feature of any i18n library. It allows us to put placeholders in the translation & set the value in the runtime.
The code in action:
$ node src/index.js
hello world
This key is was read from nested object
Hello Marcin
In this article, we have seen how to use basic translation features from i18next.