i18n-web: Tool for String Externalization and Internationalizing the Web

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
Search for a command to run...

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
No comments yet. Be the first to comment.
Traditional SSR is great, but it has a massive buffering problem. Let's look into the SSR Streaming and learn how it could be a game changer.

Learn how to build maintainable, reusable forms in React using design patterns like custom hooks, compound components, and context for scalable CRUD

Learn how JavaScript variables work, including let vs const, primitive and reference types, pass by value, and how JavaScript stores data in memory.

A beginner-friendly introduction to JavaScript covering what it is, how it works, and how to set up your JavaScript environment

Learn how the promises are handled internally using event loop. This concept is essentials to debug and work with async programming.

GreenRoots Blog - A Blog by Tapas Adhikary
186 posts
Educator | YouTuber | Building ReactPlay | Let's Connect
i18n-web is a simple tool that helps in externalizing the strings in a JavaScript-based Application such that, Internationalization(i18n) can be achieved easily. It has the additional capability of parameterizing the strings to get the dynamic content Internationalized.
yarn add i18n-web
npm install i18n-web
Before we go over the usage of the tool, let us understand some of the fundamentals at a high level.
String Externalization means, instead of writing the user(or customer) facing strings in source files(.html, .js, .java etc), we keep them in an external file like .properties, .json etc and load from there. This is to help Internationalization (i18n).
In Software, Internationalization (i18n) is the process to support various local languages like English(en), Spanish(es), German(de), etc.
In
i18n, 18 stands for the number of letters between the firstiand the lastnin the word internationalization, a usage coined at Digital Equipment Corporation.
All the browsers come with the in-built support of languages which can be used to identify the local language to support the application.
A Web Application may have the need of supporting multiple languages based on targeted users. If the Application Strings are Externalized outside of the source files, it is easy and flexible to support Internationalization(i18n).
Let's consider, all the application strings are in a file called en.json and this file can be loaded into the application to retrieve the strings when the app is running in the English Language.
{
'username': 'User Name',
'password': 'Password',
'hasBlog': '{0} has a blog named, {1}. This is on {2}.'
}
Now there could be an equivalent es.json file which can be loaded into the application when the browser-supported language is Spanish instead of English.
{
'username': 'Nombre de usuario',
'password': 'Contraseña',
'hasBlog': '{0} tiene un blog llamado {1}. Esto está en {2}.'
}
The tool i18n-web helps in externalizing the string and thus, internationalizing your web app with few quick and easy steps.
i18n at the same level as the node_modules folder of your app.en.js, es.js, de.js etc file to contain your application-specific strings externalized. You must add all required language .js files that your app would support.Here is an example of the en.js and es.js file.
en.js
// en.js
const en = {
'username': 'User Name',
'password': 'Password',
'hasBlog': '{0} has a blog named, {1}. This is on {2}.'
}
export { en };
es.js
// es.js
const es = {
'username': 'Nombre de usuario',
'password': 'Contraseña',
'hasBlog': '{0} tiene un blog llamado {1}. Esto está en {2}.'
}
export { es };
Create another file called, index.js where you can aggregate the all modules and export together like this:
export { en } from './en.js';
export { es } from './es.js';
myapp
└── i18n
└── en.js
└── es.js
└── de.js
└── fr.js
└── index.js
└── node_modules
In your UI Code, import it as,
import i18n from 'i18n-web';
Use it like:
// When no parameters. Just Key is passed
console.log(i18n('usename'));
// Output:
// 'User Name' for English
// 'Nombre de usuario' for Spanish
// With parameters.
const params = ['Tapas', 'greenroos', 'JavaScript'];
let hasBlog = i18n('hasBlog', ...params);
console.log(hasBlog);
// Output:
// 'Tapas has a blog named, greenroots. This is on JavaScript.' for English and
// 'Tapas tiene un blog llamado greenroots. Esto está en JavaScript.' for Spanish


Here are the links to some useful resources,
That's all for now. Please use the i18n-web package with your web application. Feel free to spread the words about i18n-web and you are welcome to Contribute to the project 👍.
If it was useful to you, please Like/Share so that, it reaches others as well. Please hit the Subscribe button at the top of the page to get an email notification on my latest posts.
You can @ me on Twitter (@tapasadhikary) with comments, or feel free to follow.