Taking localization to i18next level

The i18next library is one of the most popular choices for formatting translations in React applications. We decided, that we want to improve the experience by offering an easy integration with Tolgee. Localization is now uniquely accessible as your tr…


This content originally appeared on DEV Community and was authored by Štěpán Granát

The i18next library is one of the most popular choices for formatting translations in React applications. We decided, that we want to improve the experience by offering an easy integration with Tolgee. Localization is now uniquely accessible as your translations can be managed through opensource platform, without wasting precious Dev time.

Why i18next?

The i18next is very versatile and extensible and supports basically all features that you need from i18n library.

I was attracted to i18next mostly because it's React extension supports React elements interpolation through Trans component.

It's basically a way how to use custom elements in translations - it's really helpful when you for example need to have a link in the middle of translation. Normally you would need to have 3 separate translations and use them like this:

<p>
  {t("key_1")}
  <a href="...">{t("key_2")}</a>
  {t("key_3")}
</p>

If we use Trans component we can do this with one translation:

<Trans i18nKey="key_1" components={{ Link: <a href="..." /> }} />

while translation looking like Text 1 <Link>Text 2</Link> Text 3.
We'll get correct result with link inside. This way you can use any React elements and they will just get injected by the translation as child.

Additional tools for i18next

There is also a quite rich family of additional plugins and tools, which can be very useful. One of them is ICU support for i18next, which comes in handy as that is go to format for Tolgee.

Another very useful utilities are static extraction tools, which can scan your code and reliably find all keys that you are using in your project. This can help you make sure that everything is translated or eliminate unused keys.

How to integrate with Tolgee

We provide package @tolgee/i18next, which provides a "bridge" for integration.

So we'll need to install it, together with i18next, ICU parser and tolgee UI:

npm install i18next i18next-icu @tolgee/i18next @tolgee/ui
import i18next from 'i18next';
import ICU from 'i18next-icu';
import { withTolgee } from "@tolgee/i18next";
import { UI } from "@tolgee/ui";

withTolgee(i18n, {
  apiUrl: "https://app.tolgee.io",
  apiKey: ...,
  ui: UI,
})
  .use(ICU)
  .init({
    lng: 'en',
    supportedLngs: ['en', 'fr'],
  })

Now you can manage your tranlsations through Tolgee platform, so anyone can do translate (not just Devs!). You also have the power of in-context translatins, so when you add new key, you can add initial translation directly through your app and quickly take screenshot, so everyone knows where the translation is located.

How it works?

The withTolgee function is using i18next plugin system of backends, processors and also attaches event listeners and modifies default settings. We use our invisible text technique for marking each translation through i18next processor, so in-context functionality works as expected.

TLDR: Tolgee acts as a translations source, using i18next for formatting and language management, while keeping in-context localization working.

I want to try this!

We have example applications for React and Vue and play around with them. If you connect them with Tolgee platform, you can see in-context localization in action.

In-context example

Read more in our documentation.

PS: Check Tolgee.io and give us github stars

Original article was published on our blog.


This content originally appeared on DEV Community and was authored by Štěpán Granát


Print Share Comment Cite Upload Translate Updates
APA

Štěpán Granát | Sciencx (2022-01-17T10:01:36+00:00) Taking localization to i18next level. Retrieved from https://www.scien.cx/2022/01/17/taking-localization-to-i18next-level/

MLA
" » Taking localization to i18next level." Štěpán Granát | Sciencx - Monday January 17, 2022, https://www.scien.cx/2022/01/17/taking-localization-to-i18next-level/
HARVARD
Štěpán Granát | Sciencx Monday January 17, 2022 » Taking localization to i18next level., viewed ,<https://www.scien.cx/2022/01/17/taking-localization-to-i18next-level/>
VANCOUVER
Štěpán Granát | Sciencx - » Taking localization to i18next level. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/17/taking-localization-to-i18next-level/
CHICAGO
" » Taking localization to i18next level." Štěpán Granát | Sciencx - Accessed . https://www.scien.cx/2022/01/17/taking-localization-to-i18next-level/
IEEE
" » Taking localization to i18next level." Štěpán Granát | Sciencx [Online]. Available: https://www.scien.cx/2022/01/17/taking-localization-to-i18next-level/. [Accessed: ]
rf:citation
» Taking localization to i18next level | Štěpán Granát | Sciencx | https://www.scien.cx/2022/01/17/taking-localization-to-i18next-level/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.