feat(i18n): move all UI strings and prompts to chrome.i18n - #2
Open
RedCreepster wants to merge 1 commit into
Open
feat(i18n): move all UI strings and prompts to chrome.i18n#2RedCreepster wants to merge 1 commit into
RedCreepster wants to merge 1 commit into
Conversation
The extension had two parallel translation mechanisms: an inline dictionary in i18n.js and _locales/*/messages.json. Only the manifest and the context menu read _locales, so most of its keys were dead, the two catalogues had already drifted apart, and everything the extension drew on the page was hard-coded Chinese. Everything now reads from _locales via chrome.i18n.getMessage: - i18n.js becomes a thin wrapper over chrome.i18n and keeps the data-i18n attribute API; i18nGet now supports substitutions and returns the key when a message is missing, so gaps stay visible - localize content.js, pdf-content.js, popup.js, prompt-manager.js, popup.html, viewer.js, translator.js and background.js - localize the system prompts, so the prompt sent to the model follows the extension's UI language - grow the catalogue from 51 to 143 keys, identical across en, zh_CN, ja and ko Fixes found along the way: - popup.js and prompt-manager.js are classic scripts in one document, so a top-level `const t` in both would have thrown a redeclaration SyntaxError and broken the popup; they call i18nGet directly - viewer.js derived its toggle state from the button label, which the localization itself would have broken; use an explicit flag - viewer.html was missing the Русский option that getDefaultTargetLang can return, leaving the select empty on a ru locale - align aboutTitle on "Easy Translator" and split shortcutHint, which had drifted between the two catalogues Language option values stay as they are: they are endonyms and double as the language name sent to the model, so changing them would break stored settings. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The extension had two parallel translation mechanisms that had drifted apart:
chrome.i18n+_locales/— read only bymanifest.jsonand the two context menu items inbackground.js. The other ~45 keys inmessages.jsonwere never read.i18n.js— 4 languages hard-coded in JS, wired up throughdata-i18nattributes, loaded only bypopup.htmlandviewer.html.Everything the extension drew on the page was hard-coded Chinese, because
i18n.jsis not a content script: the selection tooltip, the sidebar, the full-page translation banner, PDF overlays, popup status messages and all error text.The two catalogues had also diverged —
shortcutHintwas one sentence in_localesbut split in two ini18n.js, andaboutTitleread "Universal Translator" / "万能翻訳機" / "만능 번역기" instead of "Easy Translator".What changed
Everything now reads from
_locales/throughchrome.i18n.getMessage.i18n.jsbecomes a thin wrapper overchrome.i18n. Thedata-i18n/-placeholder/-titleattribute API is unchanged;i18nGet(key, ...subs)now supports substitutions and returns the key itself when a message is missing, so a gap stays visible instead of collapsing into an empty label.content.js,pdf-content.js,popup.js,prompt-manager.js,popup.html,viewer.js,translator.js,background.js.promptNatural,promptFormal,promptBatch,promptBatchSuffix,promptSrcAuto), so the prompt sent to the model follows the extension's UI language.en,zh_CN,ja,ko.Content scripts call
chrome.i18ndirectly via a small local helper rather than pullingi18n.jsintocontent_scripts, so no extra script is injected into every page.Bugs fixed along the way
popup.js+prompt-manager.jsare classic scripts in the same document. A top-levelconst talias in both would have thrownSyntaxError: Identifier 't' has already been declaredand broken the popup entirely — they calli18nGetdirectly instead.viewer.jsderived its toggle state fromtoggleBtn.textContent.includes(origLabel). Localization itself would have broken this; replaced with an explicitpanelsVisibleflag.viewer.htmlhad noРусскийoption even thoughgetDefaultTargetLang()returns it for arulocale, leaving the select empty.Deliberately unchanged
<option value="中文">values. These are endonyms and double as the language name interpolated into the prompt. Changing them would break stored settings and the prompt.Verification
node --checkpasses on all 8 JS files; all locale files andmanifest.jsonare valid JSON.data-i18n*,i18nGet(,t(,__MSG_*__) resolves, and that no key is unused.chrome.i18nplaceholder substitution across all 4 locales: substitutions expand correctly, and the progress-bar regex inupdateBanner(/(\d+)\/(\d+)/) still matches the intendeddone/totalpair in every language.Not loaded in Chrome — needs a manual check of the popup, the selection tooltip, full-page translation and the PDF viewer.
Note
There is no
rulocale, so a Russian browser falls back toenviadefault_locale. Easy to add if wanted.