# General Translation Integrations: Adding source code context
URL: https://generaltranslation.com/en-US/docs/integrations/paraglide/guides/adding-source-context.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: How to send the code around each Paraglide JS message to translation as context.

`@generaltranslation/paraglide-js` finds every usage of a Paraglide JS message in your app and saves the code around it. A message like "Order" then reaches translation with the code that shows whether it is a button, a heading, a noun, or a verb.

It reads your source files. It does not change Paraglide JS, and it never runs your bundler, so it works however your app is built.

## Before you start [#before-start]

You need:

- Node.js 20 or later.
- Paraglide JS 1, 2, or 3, compiled at least once so its output directory exists.
- Messages stored in the inlang message format (`plugin.inlang.messageFormat`), such as `messages/en.json`.
- Message files that you translate with the CLI. (See the [Quickstart](/docs/integrations/paraglide/quickstart)).
- For components that are not JavaScript, TypeScript, or JSX, the framework's language tool installed in your app. (See [Supported file formats](/docs/integrations/paraglide/reference/file-formats)).

<Callout type="info">
  Source context is not currently attached when using the `jsonSchema` rule in the [Quickstart](/docs/integrations/paraglide/quickstart#quickstart). The CLI warns that the metadata was not attached and translates your messages without source context. Keep that rule to protect variant declarations and selectors; do not remove it to enable source context.
</Callout>

## Add context [#add-context]

Install the package, turn on collection, then translate as usual.

### 1. Install `@generaltranslation/paraglide-js`

<Tabs items={['npm', 'yarn', 'bun', 'pnpm']}>
  <Tab value="npm">
    ```bash
    npm install --save-dev @generaltranslation/paraglide-js
    ```
  </Tab>

  <Tab value="yarn">
    ```bash
    yarn add --dev @generaltranslation/paraglide-js
    ```
  </Tab>

  <Tab value="bun">
    ```bash
    bun add --dev @generaltranslation/paraglide-js
    ```
  </Tab>

  <Tab value="pnpm">
    ```bash
    pnpm add --save-dev @generaltranslation/paraglide-js
    ```
  </Tab>
</Tabs>

### 2. Turn on collection

Set `parsingFlags.includeSourceCodeContext` to `true` in the existing `json` entry of your `gt.config.json`, and keep the rest of that entry as it is. (See [Source context](/docs/cli/reference/formats/json-files#source-context)).

```json title="gt.config.json"
{
  "files": {
    "json": {
      "include": ["messages/[locale].json"],
      "parsingFlags": {
        "includeSourceCodeContext": true
      }
    }
  }
}
```

[`gt translate`](/docs/cli/reference/commands/translate), [`gt stage`](/docs/cli/reference/commands/stage), [`gt upload`](/docs/cli/reference/commands/upload), and [`gt setup`](/docs/cli/reference/commands/setup) then collect the context before they read your files. Each message file of your base locale gets a metadata file next to it. Every used message lists the code around its usages, by source file:

```json title="messages/en.metadata.json"
{
  "greeting": {
    "sourceCode": {
      "src/Header.tsx": [
        {
          "before": "import { m } from './paraglide/messages.js';\n…",
          "target": "  return <h1>{m.greeting({ username })}</h1>;",
          "after": "}\n…"
        }
      ]
    }
  }
}
```

<Callout type="info">
  Warnings name what was skipped and how to fix it: an unresolved import alias, a missing language tool, or a directory that was not recognized as Paraglide JS output. A warning does not stop translation. Install a missing language tool with [`paraglide-context install`](/docs/integrations/paraglide/reference/install). (See [Resolving imports](/docs/integrations/paraglide/guides/resolving-imports)).
</Callout>

The CLI collects with the package defaults. To set `--include`, `--alias`, `--radius`, or another option, leave `includeSourceCodeContext` off and run [`paraglide-context`](/docs/integrations/paraglide/reference/paraglide-context) before each CLI command instead. To see how many usages a run finds, run `npx paraglide-context --verbose`.

### 3. Translate your messages

Run [`gt translate`](/docs/cli/reference/commands/translate). It collects the context, then sends the metadata entries that match your messages as [keyed metadata](/docs/cli/reference/keyed-metadata). With a `jsonSchema` rule on the message files, the messages are translated without that metadata.

```bash
npx gt translate
```

<Callout type="info">
  New context alone does not retranslate a message. A message is translated again when its source text changes.
</Callout>

## Next steps

- /docs/integrations/paraglide/guides/resolving-imports
- /docs/integrations/paraglide/guides/adding-file-formats

## Sitemap

See the full [sitemap](https://generaltranslation.com/sitemap.md) for all pages.
