Перейти к основному содержимому

🌐 Internalization (i18next)

Explained (TL;DR)

What is internationalization (i18n)?

Internationalization (i18n) is the process of designing and developing software so it can be adapted for users of different cultures and languages.

Internationalization doesn’t just involve enabling different languages—it also entails adapting the software to accept different forms of data and settings to match local customs and process them correctly.

The W3C Group defines internationalization as follows:

Internationalization is the design and development of a product, application, or document content that enables easy localization for target audiences that vary in culture, region, or language.

Internationalization usually includes:

  • Designing and developing in a way that removes barriers to localization or international deployment. This includes such things as enabling the use of Unicode, ensuring the proper handling of legacy character encodings where appropriate, taking care over the concatenation of strings, avoiding dependence in code of user-interface string values, etc.
  • Providing support for features that may not be used until localization occurs. For example, adding markup in your DTD to support bidirectional text, or for identifying language. Or adding to CSS support for vertical text or other non-Latin typographic features.
  • Enabling code to support local, regional, language, or culturally related preferences. Typically, this involves incorporating predefined localization data and features derived from existing libraries or user preferences. Examples include date and time formats, local calendars, number formats, and numeral systems, sorting and presentation of lists, handling of personal names and forms of address, etc.
  • Separating localizable elements from source code or content, such that localized alternatives can be loaded or selected based on the user’s international preferences as needed.

What is localization (l10n)?

Although similar at first sight, localization isn’t the same as internationalization. Localization is the act of adapting software—from content, UI layout, colors, and images to measurement units, date formats, and currencies—to suit a specific locale. The term "locale" defines a geographical region with a specific language (e.g., fr_CA means French from Canada, fr_FR means French from France, etc.).

Internationalization always comes first, and it’s what makes localization easier. In many ways, internationalization can be thought of as building the structure of a piece of software so that it can be adjusted for different target markets, and localization is the process of actually doing so for a specific market. Both are integral parts of a localization strategy.

The W3C Group refers to localization as follows:

“Localization refers to the adaptation of a product, application, or document content to meet the language, cultural and other requirements of a specific target market (a locale). Localization is sometimes written as l10n, where 10 is the number of letters between l and n. Often thought of only as a synonym for translation of the user interface and documentation, localization is often a substantially more complex issue.

Localization can entail customization related to:

  • Numeric, date, and time formats
  • Use of currency
  • Keyboard usage
  • Collation and sorting
  • Symbols, icons, and colors
  • Text and graphics containing references to objects, actions, or ideas that, in a given culture, may be subject to misinterpretation or viewed as insensitive
  • Varying legal requirements, and many more things…

Localization may even necessitate a comprehensive rethinking of logic, visual design, or presentation if the way of doing business (e.g., accounting) or the accepted paradigm for learning (e.g., focus on individual vs. group) in a given locale differs substantially from the originating culture.”

Introduction

When your bot becomes popular, people in different regions of the world start using it. There is a need to provide them with the functionality of your bot, in their native language. This guide will help you solve this problem with i18next

Why i18next?

As you can see, we offer integration of third-party solutions, unlike other bot frameworks and libraries that usually offer their own solutions for this, and for good reason. i18next is a very popular and flexible solution, it is quite difficult to create something similar and of sufficient quality, as well as to maintain it.

Its features and flexibility are incomparable to any offered solution in other libraries and frameworks, and its popularity and good documentation makes it easy to integrate and work with it.

Installation

First you need to install i18next:

npm install i18next --save

Integration

To integrate i18next into our bot, we first need to create a file where we place the code that will create middleware for us. We can place the file anywhere.

i18n.js
Loading...

Now we need to import and initialize i18next, and register our middleware.

предупреждение

Locales and initialization run asynchronously. This must be done before launching our bot so that all locales are successfully loaded before they are used.

Here is an example of how this can be done:

index.js
Loading...

The example above shows a basic example of how you can use i18next for your bots

подсказка

If you notice that the debug mode is enabled in the example, it is very useful during development, as it displays various information, for example, about missing keys, events, and so on.

More

Of course, the possibilities are not limited to the example shown above. i18next supports many formats, here are some of them for example:

i18next also supports its own middleware, for example:

If you want, you can create your own plugins, using this guide

Docs (i18next)

You can read more about basics, APIs, plugins, post processors, formatting, context, pluralize and formatting (numbers, currency, dates, relative time) and other in the official documentation:

Sources of information