Skip to content
askdareDocumentation

    ADR-0014: Changing the chat's language at runtime

    • Status: Accepted. Amends the configure allowlist of ADR-0008.
    • Date: 2026-11-11

    Context

    A bot set to locale: 'auto' picks its language once, when the chat loads:

    • The loader reads the page's <html lang> (or the browser's language) and picks the launcher's texts from the look inlined into widget.js?bot=, which has English and Polish today.
    • It adds ?lang= to the iframe's URL. The route resolves the locale (localeFor in server/handler.ts), and publicConfig() in server/public-config.ts builds the texts for it.
    • getChatConfig({ locale }) does the same for a chat inside a page (ADR-0011). Root keys its store by base path, bot and locale.

    Sites that switch language without a page load (a language menu in a single-page app, a lang attribute set by an i18n library) keep a chat in the old language. The visitor's conversation must not be lost when the language changes.

    Two facts make a switch cheap:

    • The conversation doesn't depend on the language. The resume key in the browser's storage is askdare:<bot>:resume (plus the identity key), with no locale in it.
    • The server takes the locale with every message (body.locale in sendMessage), so the next answer follows the new language without any server state.

    Decision

    Only bots with locale: 'auto' switch. A bot with a fixed locale ignores a switch and logs a console warning that names the bot, because its owner chose the language on purpose.

    Step 1 (0.3): reload in the new language

    • askdare('configure', { locale }) sets the chat's language. The value is a BCP 47 tag, checked the same way as the bot's locale (canonicalLocale); anything else is dropped by the loader and ignored by the iframe.
    • data-follow-lang on the script tag watches <html lang> with a MutationObserver and switches on every change. An explicit configure({ locale }) wins over it until the next lang change.
    • The loader does the switch:
      1. It recomputes the launcher's texts from the inlined look (English and Polish today; other languages fall back to English, as on first load).
      2. It rebuilds embedUrl with the new ?lang=.
      3. If the iframe exists, it reloads it. The chat restores the conversation from the resume token as after any reload, and the earlier configure() call is sent again over the new port, as now.
    • An answer that is still streaming is cut off in the browser. The chat reports message.received with outcome: 'stopped' (ADR-0013) before the reload. The server finishes the turn and settles its cost as for any disconnect (ADR-0004), so the answer is stored in the conversation; a history loaded before the turn ends may not show it yet.
    • React keeps re-creating the store when getChatConfig is called with another locale, because Root keys it by locale. That already works; step 1 only documents it, together with the same stopped event.
    • Details settled while building it:
      • locale is applied on its own: a configure call with only locale keeps the greeting, starters and context of the previous call.
      • A connected chat gets a locale message over the bridge: it stops the answer under way (so the stopped event goes out before Firefox and Safari cut the stream off as a failure), then reloads itself with the new ?lang=. A chat that hasn't connected yet has nothing under way, and the loader reloads it. Both use location.replace(), so a switch adds no entry to the page's history. Step 2 handles the same message in place.
      • Until the reloaded chat says it's ready, the launcher shows on phones again and the loader watches for a chat that doesn't come back, as on first load.
      • The same language again (after canonicalization) does nothing; a tag that isn't valid is dropped with a console warning. data-follow-lang acts only when the lang attribute's value changes.
      • A loader without the bot's look (data-bot without ?bot=) can't tell a fixed locale: it reloads, and the server keeps the bot's locale.

    Step 2 (0.4): switch in place

    • Per-language texts. widget.texts also accepts { en: {...}, pl: {...} }, one set of overrides per language. The flat form stays valid and means "the bot's locale", or English for auto.

      Update (2026-11-17), as built: the flat form keeps applying in every language, as it did before, so no bot changes behavior. A visitor gets the set for their tag, else one for the same language; a language without a built-in dictionary gets the en set, and the launcher gets an entry for each language with a set. A set's key wins over the title, greeting, placeholder and launcherLabel shortcuts for its language.

    • GET <basePath>/config/<bot>?lang= returns the public configuration for that language, built by publicConfig() like the iframe's and getChatConfig's, so the "no secrets" test covers it.

    • The store gets setLocale(locale). It fetches that configuration, swaps the texts and the locale, and keeps the messages, the draft and any turn in progress. The client's locale becomes mutable, so the next message carries the new one.

    • The bridge gets a locale message, so the loader switches the iframe without reloading it.

      Update (2026-11-17), as built: the step 1 locale message gains inPlace: 1. A loader that sends it keeps the chat connected; without it (a loader cached from before step 2), or when the chat can't load the new texts, the chat reloads as in step 1. GET /config/<bot> also answers Content-Language, the language the texts are written in, which becomes the chat document's lang.

    • React keys the store without the locale and calls setLocale when the prop changes, so a switch no longer resets the chat.

    Not decided here

    More built-in dictionaries than English and Polish, and right-to-left layouts, come later. Each needs its own texts and visual baselines.

    Consequences

    • New public API (ADR-0006):
      • locale in askdare('configure') and data-follow-lang (step 1);
      • the per-language form of widget.texts, the /config/<bot> endpoint and setLocale (step 2).
    • ADR-0008 changes: configure accepts texts, starters, page context and the locale. The locale is not a secret and changes only what the visitor reads and the language the server asks for. The client still never chooses the model, instructions or tools (ADR-0004).
    • Step 1 costs a reload: the history request (about 2 s, ADR-0003) runs again, and an answer in progress disappears from view. Step 2 removes both.
    • A conversation can mix languages. Earlier answers stay in the language they were written in; only new answers and the interface follow the switch.
    • Fixed-locale bots behave as now. The warning tells a site owner why the switch did nothing.

    Alternatives considered

    • Switch in place from the start. It needs per-language texts, a new endpoint and a mutable client at once. Step 1 ships the visible behavior with only loader changes, and step 2 removes the reload later without changing the page's API.
    • Every bot switches, fixed locales included. It overrides a choice the site owner made, for instance a bot whose knowledge base exists in one language only.
    • A new conversation per language. Simpler, but visitors lose their conversation for clicking a language menu.
    • Ship every dictionary in the iframe and translate in the browser. It grows the 50 KB iframe budget with every language and puts bot texts for languages the visitor never uses into every page load.
    • Follow <html lang> always, without data-follow-lang. Some sites set lang per section or for a translation plugin; an automatic switch would surprise them. Following is opt-in.