Docs
Concepts
Extraction

Extraction

Keeping your localization system up to date can be a challenging task, especially in large or fast-paced projects. It often involves duplicating work and leaves room for potential errors.

Terai's extraction feature simplifies the process of identifying all the strings that require localization in your project, making them ready for translation.

Extraction config

First, you need to configure some options in your terai.config.ts file for the extraction to work. Specifically, you need to set up the projectLocale and outDir options. The projectLocale option represents the locale you're using in your source messages, while the outDir option specifies the output directory for the extracted dictionary that will be created from them.

import { defineConfig } from '@terai/dev'
 
export default defineConfig({
  include: ['./src/**/*.{js,jsx,ts,tsx}'],
  exclude: [],
  projectLocale: 'en-US',
  outDir: './locale',
  // ...rest of options
})

In the above code snippet:

  • Import necessary modules from @terai/dev.
  • Define the target files for which we want to generate translations and the source locale used in our messages.
  • Define the output directory for the locale system.

Running extract

To extract strings for localization in your project using Terai, you need to run the extract command to update all gathered messages from your project:

pnpm terai extract

How it works

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

  1. Terai will analyze the inputs to your project and search for all instances of ts in files.
  2. Each occurrence of a string will be converted into a unique hash (e.g., 78awdk123) based on its content.
  3. The extracted strings will be written to a manifest file (.terai/locale-manifest.json).
  4. Last, the dictionary for your projectLocale will be created (or updated), as it does not require translation.