Skip to content

i18n

xtyle.i18n

The i18n (internationalization) allows you to handle translations in your project effectively. With xtyle.translations, you can register translation object, and later, you can access those translations using xtyle.i18n.

Register Translations

To set up translations, you need to use the xtyle.translations method. It takes an object as input.

/* Set Translations */
xtyle.translations({
  fr: {
    greetings: {
      hello: "Bonjour!",
    },
  },
});

In this example, we're registering a single translation for the French language. You can add more translations for different languages by extending the xtyle.translations object with additional language codes and their respective translations.

Access Translations

The xtyle.i18n method takes a single argument, which is a dot-separated string representing the path to the translation you want to retrieve.

The format of the string is path_to.translation_key.

Here's an example of how to use xtyle.i18n to retrieve the French greeting we registered earlier:

/* Preview */
console.log("i18n: ", xtyle.i18n("fr.greetings.hello"));

In this case, the output will be:

i18n: Bonjour!

By using xtyle.i18n, you can easily access the translations you registered globally throughout your project and display content in different languages based on the user's preference or the context of your application.