# Add a Language Switcher to a Wagtail Site

Yes. An agent turns on locale-prefixed URLs, adds a header switcher that links each page's translations, sets lang, dir and hreflang tags, tests it, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-language-switcher/wagtail/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads what the site already has for languages before it adds a switcher. In a site shaped like wagtail/bakerydemo, `requirements/base.txt` pins `wagtail>=8.0,<8.1` on `Django>=6.0,<6.1`, and `bakerydemo/settings/base.py` already sets `WAGTAIL_I18N_ENABLED = True`, `USE_I18N = True` and `WAGTAIL_CONTENT_LANGUAGES = LANGUAGES` with `en`, `de` and `ar`, and installs `wagtail.locales` and `wagtail.contrib.simple_translation`. So editors can copy a page for translation in the admin, but nothing on the front end knows: `MIDDLEWARE` has no `LocaleMiddleware`, `bakerydemo/urls.py` includes `wagtail_urls` at the root without `i18n_patterns`, and `bakerydemo/templates/base.html` hardcodes `<html lang="en">` with no `dir` even though Arabic is one of the three languages.

The pieces to build on are in `bakerydemo/base/templatetags/navigation_tags.py`, which already defines `get_site_root`, `top_menu` and `breadcrumbs` as template tags with their templates under `templates/tags/`, and in `templates/includes/header.html`, where the desktop navigation, a search form and a `data-theme-toggle` button sit in one `.navigation` block. `base.html` renders a canonical link and Open Graph tags from `page.full_url`, and `bakerydemo/base/tests/test_seo.py` tests exactly those tags with `WagtailPageTestCase`, a `Site` created in `setUpTestData` and a `HomePage` published under the root. `.github/workflows/ci.yml` runs `manage.py check`, `makemigrations --check` and `manage.py test` with `bakerydemo.settings.test`, and `make lint-server` runs `ruff`, `curlylint` and `djhtml`.

The agent adds `django.middleware.locale.LocaleMiddleware` after `SessionMiddleware` and before `CommonMiddleware`, and wraps the `search/` route and the `wagtail_urls` include in `i18n_patterns(..., prefix_default_language=False)` so English URLs stay as they are and German and Arabic pages live under `/de/` and `/ar/`; the admin, documents, images, API and sitemap routes stay outside the prefix. A `language_switcher` inclusion tag joins `navigation_tags.py`: given the current page, it queries `page.get_translations(inclusive=True).live().public()`, orders by the `LANGUAGES` order, and renders `templates/tags/language_switcher.html` as a `<nav aria-label>` of links with each locale's display name in its own language and `aria-current` on the active one; on a view with no page it falls back to the `set_language` form. `header.html` includes it beside the theme toggle, in both the desktop and the mobile navigation.

`base.html` becomes `<html lang="{{ LANGUAGE_CODE }}" dir="{% if LANGUAGE_BIDI %}rtl{% else %}ltr{% endif %}">` and gains one `<link rel="alternate" hreflang>` per live translation plus `x-default` pointing at the default locale, next to the canonical link. Tests go in `bakerydemo/base/tests/test_i18n.py` in the same style as `test_seo.py`: create the `de` and `ar` `Locale` rows, copy the home page with `copy_for_translation`, publish, and assert the switcher links, the `hreflang` set, `lang="ar"` with `dir="rtl"` on the Arabic page, and that `/de/` resolves while `/en/` does not. The agent runs `python manage.py check`, `python manage.py makemigrations --check --noinput`, `python manage.py test` and `make lint-server`, and keeps the output.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add a front-end language switcher to this Wagtail site and open a pull request. Read requirements/base.txt, bakerydemo/settings/base.py, bakerydemo/urls.py, bakerydemo/templates/base.html, bakerydemo/templates/includes/header.html, bakerydemo/base/templatetags/navigation_tags.py, bakerydemo/templates/tags/top_menu.html and bakerydemo/base/tests/test_seo.py first. Add LocaleMiddleware after SessionMiddleware. Wrap the search route and the wagtail_urls include in i18n_patterns with prefix_default_language False, leaving admin, documents, images, API and sitemap routes unprefixed. Add a language_switcher inclusion tag that lists the current page's live public translations in LANGUAGES order with aria-current on the active one and falls back to the set_language form on non-page views, and include it in both navigations in header.html. Set lang and dir on the html element from LANGUAGE_CODE and LANGUAGE_BIDI and add hreflang alternate links plus x-default in base.html. Write bakerydemo/base/tests/test_i18n.py in the style of test_seo.py covering the links, hreflang, rtl for Arabic and the unprefixed default language. Run manage.py check, makemigrations --check, manage.py test and make lint-server, and paste the output in the pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that edits `settings/base.py` and `urls.py`, adds a tag and a template under `templatetags/` and `templates/tags/`, changes the `<html>` element and head of `base.html`, includes the switcher twice in `header.html`, and adds `test_i18n.py`. The description lists the URL changes as a table of before and after paths so a reviewer sees that English URLs did not move, and pastes the `check`, `makemigrations --check`, test and lint output.

The repository's `CI` workflow runs `lint-server`, `lint-client` and `test-server` on the pull request, and the `Docker build` workflow builds the image and runs the tests again inside it, so the new tests run twice as checks.

A reviewer checks that the switcher hides languages a page has not been translated into rather than linking to a 404, that the canonical URL on a German page is the `/de/` one, that the admin at `/admin/` has no prefix, and that the Arabic page renders right-to-left. They merge, and editors use the translation menu they already have to fill the other languages.

## Run it on every Wagtail site with content in more than one language

A team running several Wagtail sites sends one `POST /o/{org}/sessions` per repository with this prompt and the repository in `resources`. Each run reads that site's own `LANGUAGES`, templates and tags, so a site that already uses `i18n_patterns` gets the switcher and the `hreflang` tags only, and each run opens its own pull request.

Send a `request_id` with each call so a retry returns the run already started, and set a `webhook_url` on the key so `run.completed` arrives with `session_url` when each turn ends and your handler posts the pull request where the web team reads it.

## Why run it on Rush?

Wagtail's translation model is already in the site; the front end is what is missing, and getting it right means URL routing, middleware order, a template tag, head tags for search engines and a right-to-left check, each of which is easy to get subtly wrong. In a Rush sandbox the agent runs the site's own test suite and lint, renders the German and Arabic pages, and keeps every command in the run's record beside the pull request.

On a laptop this is a day of reading the i18n docs for Django and Wagtail and switching between settings, URLs, templates and tests. The agent does it in one pass with the repository's conventions, and a follow-up such as "add a `Content-Language` header too" wakes the parked sandbox and lands on the same branch.

## Frequently asked questions

### Will existing English URLs change?

No. `prefix_default_language=False` keeps the default language at the root, so every link, canonical URL and search result the site has today stays valid. Only German and Arabic pages get `/de/` and `/ar/` prefixes, and `test_i18n.py` asserts that `/en/` does not resolve.

### What does the switcher show on an untranslated page?

Only the languages that page is live in, from `get_translations(inclusive=True).live().public()`. A page that exists in English alone shows a single, current entry rather than links to pages that do not exist. On views without a page, such as search results, it falls back to Django's `set_language` form.

### Does the admin move under a language prefix?

No. The `admin/`, `django-admin/`, `documents/`, `images/`, `api/` and `sitemap.xml` routes stay outside `i18n_patterns`, so editors' bookmarks and the API clients keep working. Only the `search/` route and the Wagtail page serving route are wrapped.

### Why change lang and dir on the html element?

Because `base.html` says `lang="en"` on every page, which is wrong for a German or Arabic one, and Arabic needs `dir="rtl"` for the text to lay out correctly. `LANGUAGE_CODE` and `LANGUAGE_BIDI` come from `LocaleMiddleware` through the `i18n` context processor, so the values follow the URL prefix.

## More Wagtail jobs

- [Add Page Caching to a Wagtail Site](https://getrush.ai/run/add-page-caching/wagtail/)
- [Add Pagination to a Wagtail Blog Index](https://getrush.ai/run/add-pagination/wagtail/)
- [Add Reading Time to a Wagtail Blog](https://getrush.ai/run/add-reading-time/wagtail/)

## More jobs like this: Hand off a long task

- [Show a Last Updated Date from Git History on Eleventy Posts](https://getrush.ai/run/add-last-modified-dates/eleventy/)
- [Add Last Updated Dates to an mdBook Site](https://getrush.ai/run/add-last-updated-dates/mdbook/)
- [Show Last Updated Dates on MkDocs Pages](https://getrush.ai/run/add-last-updated-dates/mkdocs/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
