ADR-0014: Changing the chat's language at runtime
- Status: Accepted. Amends the
configureallowlist 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 intowidget.js?bot=, which has English and Polish today. - It adds
?lang=to the iframe's URL. The route resolves the locale (localeForinserver/handler.ts), andpublicConfig()inserver/public-config.tsbuilds the texts for it. getChatConfig({ locale })does the same for a chat inside a page (ADR-0011).Rootkeys 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.localeinsendMessage), 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'slocale(canonicalLocale); anything else is dropped by the loader and ignored by the iframe.data-follow-langon the script tag watches<html lang>with aMutationObserverand switches on every change. An explicitconfigure({ locale })wins over it until the nextlangchange.- The loader does the switch:
- It recomputes the launcher's texts from the inlined look (English and Polish today; other languages fall back to English, as on first load).
- It rebuilds
embedUrlwith the new?lang=. - 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.receivedwithoutcome: '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
getChatConfigis called with another locale, becauseRootkeys it by locale. That already works; step 1 only documents it, together with the samestoppedevent. - Details settled while building it:
localeis applied on its own: aconfigurecall with onlylocalekeeps the greeting, starters and context of the previous call.- A connected chat gets a
localemessage over the bridge: it stops the answer under way (so thestoppedevent 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 uselocation.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-langacts only when thelangattribute's value changes. - A loader without the bot's look (
data-botwithout?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.textsalso accepts{ en: {...}, pl: {...} }, one set of overrides per language. The flat form stays valid and means "the bot's locale", or English forauto.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
enset, and the launcher gets an entry for each language with a set. A set's key wins over thetitle,greeting,placeholderandlauncherLabelshortcuts for its language. -
GET <basePath>/config/<bot>?lang=returns the public configuration for that language, built bypublicConfig()like the iframe's andgetChatConfig'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
localemessage, so the loader switches the iframe without reloading it.Update (2026-11-17), as built: the step 1
localemessage gainsinPlace: 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 answersContent-Language, the language the texts are written in, which becomes the chat document'slang. -
React keys the store without the locale and calls
setLocalewhen 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):
localeinaskdare('configure')anddata-follow-lang(step 1);- the per-language form of
widget.texts, the/config/<bot>endpoint andsetLocale(step 2).
- ADR-0008 changes:
configureaccepts 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, withoutdata-follow-lang. Some sites setlangper section or for a translation plugin; an automatic switch would surprise them. Following is opt-in.