📝Starting

We have developed a translation library where you can build your nested dynamic texts.

Surely you have ever needed to have a text in different languages, but were confused about how to implement it, that's ok, we had the same problem.

To put this into context, let's look at a basic usage example.

import { Locale } from '@/core/libs/i18n.lib';

const locale = new Locale('en', ['en'], 'locales');

const text = locale.translate('hello', '{YOUR_NAME}');
// output: Hi Niro Health

This is the simplest mode to use, very good for inserting into test files.

Let's understand how this text was generated.

When we instantiate the 'Locale' class, all languages that do not have their '.json' files in the given folder ('locales' - by default) will be created with a 'hello' property set for testing.

src/core/locales/en.json
{
  "hello": "Hi $1"
}

The use of variables can be done using the identifier '$' followed by a number, for example '$1'.

We can invoke the 'translate' method to bring in our formatted text.

locale.translate('hello', 'GuilhermeSantos001');
// output: Hi GuilhermeSantos001

Notice that the first argument of the method is the property containing the text and the rest of the arguments are the variables that will be used instead of the '$' identifiers. The reason we use identifiers next to numbers is to maintain sequential referencing, i.e., the first variable will be associated with '$1', the second variable will be associated with '$2', and so on.

With this you already structure your texts of simple complexity.

Last updated