Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

Keep the translation target list when the language bundle cannot be loaded

Claudee71dccf11efc
diff --git a/docs/architecture.md b/docs/architecture.md
index 7300bdb63..bd4b45192 100644
--- a/docs/architecture.md
+++ b/docs/architecture.md
@@ -543,7 +543,8 @@ its token budget, since they have no finish reason); the card also claims its re
before it waits for the language bundle, so a double click starts only one call, and **More…** takes a newer id on the same wait, so an action clicked just before it never reaches the model. Selections
over 4,000 characters skip the card and use the side-panel flow. The card draws the answer as DOM
nodes (paragraphs, lists, bold, italic and code), never as HTML, and takes the source
-language from the browser's own detector, with no network call. A follow-up typed in a card
+language from the browser's own detector, with no network call. If the language bundle cannot be
+loaded, the card runs on its built-in English text and still lists the translation targets. A follow-up typed in a card
continues in the side panel as a custom question. In Chrome the background opens the panel
only when none is showing in that window: re-running `sidePanel.setOptions()` + `open()` on an
open panel closes it, so each panel reports its visibility over a long-lived port
diff --git a/src/chrome/src/content/selection-shortcut.js b/src/chrome/src/content/selection-shortcut.js
index a1acf5870..6e4f60a87 100644
--- a/src/chrome/src/content/selection-shortcut.js
+++ b/src/chrome/src/content/selection-shortcut.js
@@ -201,7 +201,14 @@
}
function applyLocalization() {
- if (!localization || !shadow) return;
+ if (!shadow) return;
+ if (!localization) {
+ // The language bundle did not load. The card still runs on the built-in English
+ // text, and its target list has to exist too: Translate asks for a target when the
+ // text is already in the interface language.
+ populateTargetLanguages();
+ return;
+ }
const strings = localization.strings;
host.lang = localization.locale;
host.dir = localization.dir;
@@ -1300,6 +1307,7 @@
cardStatus: cardState?.status || '',
cardText: cardState?.text || '',
cardError: cardState?.error || '',
+ cardTargetOptions: cardParts ? cardParts.target.options.length : 0,
cardSide,
cardRect: card && !card.hidden ? card.getBoundingClientRect().toJSON() : null,
cardSourceLabel: cardParts && !cardParts.lang.hidden ? cardParts.source.textContent : '',
diff --git a/src/firefox/src/content/selection-shortcut.js b/src/firefox/src/content/selection-shortcut.js
index a1acf5870..6e4f60a87 100644
--- a/src/firefox/src/content/selection-shortcut.js
+++ b/src/firefox/src/content/selection-shortcut.js
@@ -201,7 +201,14 @@
}
function applyLocalization() {
- if (!localization || !shadow) return;
+ if (!shadow) return;
+ if (!localization) {
+ // The language bundle did not load. The card still runs on the built-in English
+ // text, and its target list has to exist too: Translate asks for a target when the
+ // text is already in the interface language.
+ populateTargetLanguages();
+ return;
+ }
const strings = localization.strings;
host.lang = localization.locale;
host.dir = localization.dir;
@@ -1300,6 +1307,7 @@
cardStatus: cardState?.status || '',
cardText: cardState?.text || '',
cardError: cardState?.error || '',
+ cardTargetOptions: cardParts ? cardParts.target.options.length : 0,
cardSide,
cardRect: card && !card.hidden ? card.getBoundingClientRect().toJSON() : null,
cardSourceLabel: cardParts && !cardParts.lang.hidden ? cardParts.source.textContent : '',
diff --git a/test/fixtures/run.mjs b/test/fixtures/run.mjs
index be5132c08..57f9779bb 100644
--- a/test/fixtures/run.mjs
+++ b/test/fixtures/run.mjs
@@ -363,7 +363,7 @@ async function collectSmd(page, mode = 'auto') {
}, mode);
}
-async function setupSelectionShortcut(page, sourcePath, { enabled = true, requiresManualOpen = false, locale = 'en', promiseDetect = false, holdLocalization = false } = {}) {
+async function setupSelectionShortcut(page, sourcePath, { enabled = true, requiresManualOpen = false, locale = 'en', promiseDetect = false, holdLocalization = false, failLocalization = false } = {}) {
await page.setViewportSize({ width: 360, height: 280 });
await page.setContent(`<!doctype html>
<style>body{margin:0;font:18px/1.5 sans-serif} #copy{position:absolute;right:2px;bottom:2px;width:210px}</style>
@@ -390,6 +390,7 @@ async function setupSelectionShortcut(page, sourcePath, { enabled = true, requir
if (message.type === 'WB_SELECTION_SHORTCUT_LOCALIZATION') {
window.__selectionLocalizationRequests += 1;
if (window.__selectionLocalizationGate) await window.__selectionLocalizationGate;
+ if (${failLocalization ? 'true' : 'false'}) throw new Error('The language bundle is unavailable.');
const locale = String(message.locale || 'en').toLowerCase().split('-')[0];
return { ok: true, ...(window.__selectionLocalizations[locale] || window.__selectionLocalizations.en) };
}
@@ -2174,6 +2175,23 @@ for (const [label, sourcePath, manualOpen] of [
}
});
+ test(`${label}: Translate still offers a target list when the language bundle cannot be loaded`, async (page) => {
+ await setupSelectionShortcut(page, sourcePath, { requiresManualOpen: manualOpen, failLocalization: true });
+ await page.evaluate(() => { window.__selectionDetectedLanguage = 'en'; });
+ await openInlineCard(page, 'translate');
+ let state = await waitForCardStatus(page, 'idle');
+ const options = await page.evaluate(() => window.__webbrainSelectionShortcut.getState().cardTargetOptions);
+ if (state.cardTargetLabel !== 'Translate to' || !(options > 20)) {
+ throw new Error(`the English fallback card should still list target languages: ${JSON.stringify({ state, options })}`);
+ }
+ await page.evaluate(() => window.__webbrainSelectionShortcut.chooseTarget('es'));
+ state = await waitForCardStatus(page, 'ready');
+ const messages = await page.evaluate(() => window.__selectionInlineMessages);
+ if (messages.length !== 1 || messages[0].language !== 'es') {
+ throw new Error(`choosing a target should start the translation: ${JSON.stringify(messages)}`);
+ }
+ });
+
test(`${label}: Proofread follows the interface language and direction`, async (page) => {
await setupSelectionShortcut(page, sourcePath, { requiresManualOpen: manualOpen, locale: 'ar' });
await openInlineCard(page, 'proofread');