# General Translation Blog: Compiler-driven context extraction
URL: https://generaltranslation.com/en-GB/blog/paraglide-js-context.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: A deep analysis of your JavaScript for better translation context in Paraglide JS
Authors: Jackson Otto, Taylor Fang
Published: 2026-11-20

There’s a really hard problem that’s still unsolved in internationalisation (i18n). **LLMs need context** to make great translations, but most i18n libraries haven’t been designed to keep content with its surrounding context.

Take [Paraglide JS](https://paraglidejs.com/), a leading open-source i18n library for Vite-based apps which we recently [began maintaining](/blog/paraglide-js). Paraglide uses syntactic conventions that **obfuscate the relationship between content and its usage.**

The original context, however, is **essential to producing an accurate translation** which reflects intended meaning. Should ‘Order’ be translated as *Bestellen, Bestellung,* or *Reihenfolge*? *Pedir, Pedido,* or *Orden*? *下单, 订单,* or *顺序*? It depends on context: whether the word appears as a verb in a button, a noun in a heading, or a sort order in a drop-down.

<OrderSenses />

Extracting context is a non-trivial task.

Without knowing the call site or which files to read, the tool can’t just grep. It needs to work even when a message has been **renamed on import, re-exported, destructured, passed into functions, indexed with a runtime key**, or passed through any other indirection. And it needs to work with every framework Paraglide runs in.

**How can we deduce the call sites of translation functions from arbitrary JavaScript invocations, in order to recover the context around every message?** This has been an unsolved problem in Paraglide JS.

## Design goals

To tackle context extraction, we wanted to design a smooth user experience for devs already using Paraglide. Adding translation context should be an easy integration, and their existing Paraglide workflow should not change. At a high level, we had three major goals.

First, the integration should be **interoperable**. It never changes Paraglide JS and never runs your bundler. It reads source files, detects your Paraglide output directories, and writes to `<locale>.metadata.json`, a format the `gt CLI` already uploads.

Second, the integration should be **unobtrusive** and fully opt-in. The integration adds a bit of CI time, which is unnoticeable for 95% of projects. But for projects with a massive codebase or JavaScript practices which make value tracing inefficient, there is a small difference. (We’re making improvements on speed, but it will never be free, so the trade-off should be opt-in. The alternative also means shipping inside every build of the `gt CLI`, which would add unnecessary bloat.)

Lastly, the integration should be **low-friction**. If a developer wants to add context to their translations, it should be very easy to do so.

With these principles in mind, here’s how we built a performant integration to trace each compiled message through JavaScript.

## Finding where a translation function is called

In a Paraglide JS project, **finding context requires finding the call site**: where the element, component, props, and neighbouring strings are called in the code.

<CallSiteDiff />

We can’t just grep for the call site, because **we don’t know which files to read** (some are Vue or Svelte files the TypeScript compiler can’t parse) or **what the call looks like**. The call could be any of the following:

<CodeDeck label="Ways a message is called">
  <CodeCard>
    ```tsx
    // called
    <Button>{m.order({ count })}</Button>
    ```
  </CodeCard>

  <CodeCard>
    ```tsx
    // called through a method
    <Button>{m.order.parts({ count })}</Button>
    ```
  </CodeCard>

  <CodeCard>
    ```tsx
    // referenced, not called
    <SubmitButton label={m.order} />
    ```
  </CodeCard>

  <CodeCard>
    ```tsx
    // aliased first
    const t = m;
    <Button>{t.order({ count })}</Button>
    ```
  </CodeCard>

  <CodeCard>
    ```tsx
    // destructured
    const { order } = m;
    <Button>{order({ count })}</Button>
    ```
  </CodeCard>

  <CodeCard>
    ```tsx
    // computed key
    <Button>{m["order"]({ count })}</Button>
    ```
  </CodeCard>

  <CodeCard>
    ```tsx
    // conditional key
    <Button>{m[isSubmitting ? "order_pending" : "order"]({ count })}</Button>
    ```
  </CodeCard>

  <CodeCard>
    ```tsx
    // dynamic key, matches every order_* message
    <p>{m[`order_${status}`]()}</p>
    ```
  </CodeCard>

  <CodeCard>
    ```tsx
    // spread into an object
    const labels = { ...m };
    <Button>{labels.order({ count })}</Button>
    ```
  </CodeCard>

  <CodeCard>
    ```tsx
    // stored on a class
    class Checkout { label = m.order; }
    <Button>{checkout.label({ count })}</Button>
    ```
  </CodeCard>

  <CodeCard>
    ```tsx
    // inside a Svelte template
    <button>{m.order({ count })}</button>
    ```
  </CodeCard>
</CodeDeck>

We don’t even know if a given `m` is a Paraglide’s `m` convention for messages; this depends on resolving the import.

The tool thus needs to find the **entire set of relevant source files** that might hold a call site. These can be **in any framework**: Astro, Svelte, Vue, Angular, Marko, or anything else.

So we use each framework’s own tooling to convert its files to TypeScript, then map every result back to the original file and its template element. The first step is thus standardising each file to common TypeScript.

## Tracing a message through your code

We now have to trace values across the code, following each expression back to its origin. Here we implement four pieces: a module graph, usage collector, value evaluator, and call-site and importer indexes.

First, the **module graph** determines whether each import comes from Paraglide. It resolves every import to a real file, follows re-exports and CommonJS, and marks which files are Paraglide output directories. An import is included if its resolution chain terminates in a Paraglide output directory, and is ignored otherwise.

Next, the **usage collector** walks each module and records every expression that might be a message. It separates aliases like `const t = m` from real usages like `t.greeting()`, and records each usage along with the function and template element around it.

The **value evaluator** decides what each recorded expression actually refers to: one message, the whole `m` namespace, or something else. It follows the expression back through aliases, member access, dynamic imports, `require`, spreads and class fields. A computed key like ``m[`err_${code}`]`` can’t be pinned to one message, so it becomes the pattern `err_*` and matches every message with that prefix.

(The actual implementation under the hood is a bit more involved. It keeps track of what each variable can hold at any point, whether it was assigned directly, destructured, or passed in as a parameter or prop. In some cases it unloads modules and import chains to keep memory down. This is a condensed view for clarity purposes.)

Parameters and props are a harder case, since their values live in the caller rather than function. So the tool looks at what callers actually pass, with **call-site and importer indexes** that narrow search to only the modules that import the function or component.

<TracingSteps />

In practice, these pieces put together mean a message can be imported under one name, re-exported from another module under a different one, assigned to a variable, put in an object, destructured back out, or indexed with a runtime key, **and the integration will still find it.**

**The tool isn’t limited by the shape of your code,** and covers 95% of reasonable things people write.

## Writing context around each usage

The tool can now record every usage as its own line **with 10 lines above and below it to capture context**. If the usage was found in TypeScript generated from a `.svelte` or `.vue` file, the position is mapped back first, so the lines come from the original file.

The snippet goes into `<locale>.metadata.json` under the message’s key, grouped by source file path, split into `before`, `target` and `after`.

Any machine translation command (like [`gt translate`](/docs/cli/reference/commands/translate), for instance) can now send context with the messages, and the model translates ‘Order’ knowing the `<button>` it sits in, the `CheckoutSummary` component it belongs to, and the `{count}` prop being passed.

<ContextWindow />

## Try it out

If you use [Paraglide](https://paraglidejs.com/#quick-start) and you’re new to GT, **start with our [quickstart](/docs/overview/get-started)**. For running on its own, there’s also [`npx paraglide-context`](/docs/integrations/paraglide/reference/paraglide-context) or [`collectContext()`](/docs/integrations/paraglide/reference/collect-context).

A few things the tool doesn’t handle yet: aliases declared only in a Next.js, Nuxt or Astro config, inlang projects that use another message format plugin like i18next, and apps that share one inlang project.

This integration reads source files and never runs your build, which is why it works with every bundler and every framework. If you run into any problems, open an issue in the [`gt` repo](https://github.com/generaltranslation/gt/issues) and we’ll address it. We look forward to hearing what you think!

## Postscript: why we didn’t bundle compilers

The first version of the framework transformation was hand-rolled to optimise for performance in CI. It was much faster, but not by enough to justify the cost: far more code to maintain, support lagging behind every new language feature, and none of the parsing work the community has already done.

So we swapped our own AST for oxc-parser and the official compilers of every supported language. The surface area is much smaller, and the plugin works with any language updates.

We can’t know which framework you use, so we actually ship a package with ‘nothing’ that tells you what you need to install. `paraglide-context install <framework>` will add the compiler for you.

On the other hand, owning the transformation would free us to use any tooling, not just JavaScript; in one experiment, a Rust implementation was ~100x faster. As we keep optimising our libraries, we may revisit this.

## Sitemap

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