> For the complete documentation index, see [llms.txt](https://developers.weglot.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.weglot.com/cms/craft-cms/architecture.md).

# Architecture

How the Weglot plugin turns a Craft request into a translated page.

This page explains what happens under the hood when a visitor requests a translated URL. You don't need to know any of this to use the plugin — it is here for developers who want to understand or debug its behavior.

### Language-prefixed URLs

Weglot serves each destination language under a path prefix, for example `/fr/about` for the French version of `/about`. These prefixed URLs are **virtual**: the plugin registers URL rules that map them to its own router, and no extra entries are created in Craft. The router resolves the request against your original Craft route, then the response is translated on the way out.

### The translation pipeline

```mermaid
flowchart TD
    A[Visitor requests /fr/about] --> B[URL rule maps it to Weglot router]
    B --> C[Router resolves the original Craft route]
    C --> D[Craft renders the page in the original language]
    D --> E{Content type?}
    E -->|HTML| F[Send HTML to the Weglot API for translation]
    E -->|JSON / XML| G[Translate supported fields]
    F --> H[Rewrite internal links to add the language prefix]
    G --> H
    H --> I[Inject hreflang tags, switcher and scripts]
    I --> J[Return the translated page]
```

### Post-processing

After the Weglot API returns the translated HTML, the plugin:

* rewrites internal links (`<a>`, forms, canonical, `next`/`prev`, and similar) so they keep the current language prefix;
* injects the `hreflang` alternate tags in the `<head>` for SEO;
* injects the language switcher and Weglot's front-end script;
* maps translated URL slugs both ways, so a translated slug resolves back to the right Craft entry.

### Caching

The plugin caches the data it fetches from Weglot (your project options, available languages, slug maps) using Craft's cache, under keys prefixed with `weglot_`. This avoids calling the Weglot API on every request. If configuration changes don't appear, clearing the Craft cache forces a refresh — see **Troubleshooting**.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://developers.weglot.com/cms/craft-cms/architecture.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
