Docs
Concepts
Translation

Translation

Terai simplifies the process of translating messages in your JavaScript application. You can use the terai translate command by setting up a translator in your terai.config.ts file. This guide will walk you through the steps to configure and use the translator effectively.

Translation config

First, you need to configure the translator in your terai.config.ts file. Terai provides integrations with some major translation service providers, or you can opt-in for configuring a custom translator. Also, you need to set up the locale targets for which you want to generate translations.

import { defineConfig, createGoogleCloudTranslator } from '@terai/dev'
 
const translator = createGoogleCloudTranslator({
  // your Google Cloud crendentials
})
 
export default defineConfig({
  outLocales: ['es-ES', 'ja-JP'],
  outDir: './locale',
  translator,
  // ...rest of options
})

In the above code snippet:

  • Import necessary modules from @terai/dev.
  • Use the createGoogleCloudTranslator function to set up the translator.
  • Pass the translator to the Terai config.
  • Define the target locales for which we want to generate translations.

Running extract

Before you can generate translations, you need to run the extract command to update all gathered messages from your project. This step ensures that Terai has a list of messages (dictionary) to translate.

pnpm terai extract

Running translate

Once your translator is configured and sources messages are extracted, you can easily manage translations by running the translate script. Terai will handle the translation process and generate the necessary translation files according to the specified config.

pnpm terai translate

How it works

Whenever you run the translte command, Terai will execute the following steps:

  1. Terai will gather all locale dictionaries, except from that defined as projectLocale.
  2. The cache is checked, and a new dictionary with those non-translated messages will be created.
  3. Each dictionary will be passed down to the translator function.
  4. The translated dictionaries will be written to the outDir folder