# filipeps/traduzir-paginas-web architecture

> Architecture diagram of the GitHub repository filipeps/traduzir-paginas-web, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/filipeps/traduzir-paginas-web
- Repository: https://github.com/filipeps/traduzir-paginas-web

## Overview

1. This is a browser extension for real-time page translation, not a standalone app or server. The architecture is split into browser-run pieces: background logic, content scripts injected into tabs, and extension UI pages for popup/options.

2. The central orchestration lives in `src/background/background.js`, which coordinates translation requests, engine selection, and messaging between UI and page scripts. It is backed by `translationService.js` for provider-specific translation calls and `translationCache.js` to avoid repeated remote requests. `textToSpeech.js` is a separate background capability for audio playback.

3. The page-level work happens in `src/contentScript/`. `pageTranslator.js` is the main DOM translator that modifies page text in place. Supporting scripts handle specific states and modes: `showTranslated.js`, `showOriginal.js`, and `translateSelected.js`. `checkScriptIsInjected.js` prevents duplicate injection, which matters because the extension modifies live pages repeatedly.

4. Content scripts are the boundary between the extension and arbitrary websites. They can read and rewrite page content, but only inside the permissions the browser grants. This is why the repo also includes CSS assets for translated/original states and a small mobile popup UI under `contentScript/html` and `popupMobile.js`.

5. The user-facing controls are in `src/popup/` and `src/options/`. The popup subsystem contains separate entry points for translating a document, translating selected text, and an “improve translation” flow. The options page stores configuration such as engine choice, language preferences, dark mode, and behavioral settings.

6. The translation engines are abstracted as provider integrations rather than being hardwired into the UI. The repo and build artifacts show support for multiple providers and platform-specific packaging; the readme explicitly calls out Google and Yandex, while the code/build tree also includes provider-related assets for Bing, DeepL, and Libre. The background layer is the provider-agnostic boundary, while provider-specific code stays behind it.

7. Language and locale support is a first-class subsystem. All UI strings live under `src/_locales/*/messages.json`, and `src/lib/i18n.js` is the shared runtime access layer. Auxiliary scripts in `extra/` validate locales and generate language metadata, which suggests localization is part of the release pipeline rather than manual maintenance.

8. Shared runtime helpers live in `src/lib/`. These include platform detection, config handling, browser API error normalization, language metadata, and a generic utilities module. This library layer keeps the background, popup, options, and content scripts from duplicating browser-specific glue.

9. The build and distribution structure is browser-extension oriented. `src/manifest.json` and `src/chrome_manifest.json` define extension packaging, while `dist/firefox` and `dist/chromium` hold release-specific artifacts like update manifests and registry files. `gulpfile.js`, `polyfill.js`, and the build instructions indicate a bundled build process that prepares browser-targeted outputs from the source tree.

10. There is no backend service in this repository. External communication is limited to the translation providers and any related service endpoints they require. The privacy model in the readme reflects that: the extension does not collect user data itself, but page content must be sent to the chosen translation engine to perform the translation.

11. Important architectural boundaries for a diagram are: browser UI surfaces, background coordination, page injection/DOM rewriting, shared libraries, localization data, and external translation APIs. The extension is essentially a client-side orchestrator that transforms arbitrary web pages in place while preserving the original content for toggling and recovery.

Main entry points: [`src/background/background.js`](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/background/background.js) (Background), [`src/contentScript/pageTranslator.js`](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/contentScript/pageTranslator.js) (Page Translator), [`src/lib/stuff.js`](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/lib/stuff.js) (Shared Lib), [`gulpfile.js`](https://github.com/filipeps/traduzir-paginas-web/blob/master/gulpfile.js) (Build Pipeline)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_browser_ui["Browser UI"]
  node_popup_ui["Popup UI<br/>extension popup<br/>[popup.js]"]
  node_popup_doc["Document Flow<br/>popup flow"]
  node_popup_text["Text Flow<br/>popup flow"]
  node_popup_improve["Improve Flow<br/>popup flow"]
  node_options_page["Options Page<br/>settings ui<br/>[options.js]"]
end

subgraph group_runtime["Runtime"]
  node_background_js["Background<br/>orchestrator<br/>[background.js]"]
  node_translation_service["Translation API<br/>provider adapter"]
  node_translation_cache[("Cache")]
  node_text_to_speech["Text to Speech<br/>audio feature<br/>[textToSpeech.js]"]
  node_page_translator["Page Translator<br/>DOM translator<br/>[pageTranslator.js]"]
  node_inject_guard["Inject Guard<br/>safety check"]
  node_page_modes["Page Modes<br/>content actions<br/>[showTranslated.js]"]
  node_mobile_popup["Mobile Popup<br/>in-page UI<br/>[popupMobile.js]"]
end

subgraph group_shared["Shared Core"]
  node_i18n_runtime["i18n Runtime<br/>locale access<br/>[i18n.js]"]
  node_shared_lib["Shared Lib<br/>helpers<br/>[stuff.js]"]
  node_locales[("Locales<br/>message catalogs<br/>[messages.json]")]
end

subgraph group_build["Build & Release"]
  node_manifest["Manifests<br/>[manifest.json]"]
  node_build_pipeline["Build Pipeline<br/>bundling<br/>[gulpfile.js]"]
  node_release_artifacts["Release Artifacts<br/>distribution<br/>[updates.xml]"]
end

subgraph group_external["External Services"]
  node_provider_apis{{"Translation Providers<br/>external api"}}
end

node_popup_ui -->|"send actions"| node_background_js
node_popup_doc -->|"request translate"| node_background_js
node_popup_text -->|"request translate"| node_background_js
node_popup_improve -->|"refine text"| node_background_js
node_options_page -->|"read config"| node_shared_lib
node_popup_ui -->|"labels"| node_i18n_runtime
node_options_page -->|"labels"| node_i18n_runtime
node_background_js -->|"delegate"| node_translation_service
node_background_js -->|"cache lookups"| node_translation_cache
node_background_js -->|"audio"| node_text_to_speech
node_translation_service -->|"remote calls"| node_provider_apis
node_page_translator -->|"translate text"| node_translation_service
node_page_translator -->|"guard injection"| node_inject_guard
node_page_translator -->|"mode changes"| node_page_modes
node_page_modes -->|"controls"| node_mobile_popup
node_background_js -->|"messaging"| node_page_translator
node_background_js -->|"status updates"| node_popup_ui
node_shared_lib -->|"uses"| node_i18n_runtime
node_shared_lib -->|"loads strings"| node_locales
node_manifest -->|"packaging"| node_build_pipeline
node_build_pipeline -->|"outputs"| node_release_artifacts
node_build_pipeline -->|"consumes"| node_manifest

click node_background_js "https://github.com/filipeps/traduzir-paginas-web/blob/master/src/background/background.js"
click node_translation_service "https://github.com/filipeps/traduzir-paginas-web/blob/master/src/background/translationService.js"
click node_translation_cache "https://github.com/filipeps/traduzir-paginas-web/blob/master/src/background/translationCache.js"
click node_text_to_speech "https://github.com/filipeps/traduzir-paginas-web/blob/master/src/background/textToSpeech.js"
click node_page_translator "https://github.com/filipeps/traduzir-paginas-web/blob/master/src/contentScript/pageTranslator.js"
click node_inject_guard "https://github.com/filipeps/traduzir-paginas-web/blob/master/src/contentScript/checkScriptIsInjected.js"
click node_page_modes "https://github.com/filipeps/traduzir-paginas-web/blob/master/src/contentScript/showTranslated.js"
click node_mobile_popup "https://github.com/filipeps/traduzir-paginas-web/blob/master/src/contentScript/popupMobile.js"
click node_popup_ui "https://github.com/filipeps/traduzir-paginas-web/blob/master/src/popup/popup.js"
click node_popup_doc "https://github.com/filipeps/traduzir-paginas-web/blob/master/src/popup/popup-translate-document.js"
click node_popup_text "https://github.com/filipeps/traduzir-paginas-web/blob/master/src/popup/popup-translate-text.js"
click node_popup_improve "https://github.com/filipeps/traduzir-paginas-web/blob/master/src/popup/improve-translation.js"
click node_options_page "https://github.com/filipeps/traduzir-paginas-web/blob/master/src/options/options.js"
click node_i18n_runtime "https://github.com/filipeps/traduzir-paginas-web/blob/master/src/lib/i18n.js"
click node_shared_lib "https://github.com/filipeps/traduzir-paginas-web/blob/master/src/lib/stuff.js"
click node_locales "https://github.com/filipeps/traduzir-paginas-web/blob/master/src/_locales/en/messages.json"
click node_manifest "https://github.com/filipeps/traduzir-paginas-web/blob/master/src/manifest.json"
click node_build_pipeline "https://github.com/filipeps/traduzir-paginas-web/blob/master/gulpfile.js"
click node_release_artifacts "https://github.com/filipeps/traduzir-paginas-web/blob/master/dist/chromium/updates.xml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_popup_ui,node_popup_doc,node_popup_text,node_popup_improve,node_options_page toneBlue
class node_background_js,node_translation_service,node_translation_cache,node_text_to_speech,node_page_translator,node_inject_guard,node_page_modes,node_mobile_popup toneAmber
class node_i18n_runtime,node_shared_lib,node_locales toneMint
class node_manifest,node_build_pipeline,node_release_artifacts toneRose
class node_provider_apis toneIndigo
```

## Components

### Browser UI

Extension pages the user interacts with directly.

- [Popup UI](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/popup/popup.js): extension popup · User-facing popup entry point for translation actions. · `src/popup/popup.js`
- [Document Flow](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/popup/popup-translate-document.js): popup flow · Popup flow for translating a whole document. · `src/popup/popup-translate-document.js`
- [Text Flow](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/popup/popup-translate-text.js): popup flow · Popup flow for translating selected text. · `src/popup/popup-translate-text.js`
- [Improve Flow](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/popup/improve-translation.js): popup flow · Popup flow for improving an existing translation. · `src/popup/improve-translation.js`
- [Options Page](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/options/options.js): settings ui · Configuration UI for engine choice, languages, theme, and behavior. · `src/options/options.js`

### Runtime

Injected and background code that runs inside the browser.

- [Background](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/background/background.js): orchestrator · Central message router for translation requests, engine selection, caching, and page/UI coordination. · `src/background/background.js`
- [Translation API](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/background/translationService.js): provider adapter · Provider-agnostic layer that calls remote translation engines. · `src/background/translationService.js`
- [Cache](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/background/translationCache.js): request cache · Avoids repeated remote translation calls for the same content. · `src/background/translationCache.js`
- [Text to Speech](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/background/textToSpeech.js): audio feature · Background capability for speech playback. · `src/background/textToSpeech.js`
- [Page Translator](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/contentScript/pageTranslator.js): DOM translator · Main content-script translator that rewrites page text in place. · `src/contentScript/pageTranslator.js`
- [Inject Guard](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/contentScript/checkScriptIsInjected.js): safety check · Prevents duplicate script injection into live pages. · `src/contentScript/checkScriptIsInjected.js`
- [Page Modes](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/contentScript/showTranslated.js): content actions · Show translated, show original, and translate-selected flows. · `src/contentScript/showTranslated.js`
- [Mobile Popup](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/contentScript/popupMobile.js): in-page UI · Small content-script popup surface for mobile interactions. · `src/contentScript/popupMobile.js`

### Shared Core

Common helpers and data used across extension surfaces.

- [i18n Runtime](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/lib/i18n.js): locale access · Shared runtime access layer for localized strings. · `src/lib/i18n.js`
- [Shared Lib](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/lib/stuff.js): helpers · Common browser, config, language, and utility helpers used across extension surfaces. · `src/lib/stuff.js`
- [Locales](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/_locales/en/messages.json): message catalogs · All UI string catalogs and language metadata used by the extension. · `src/_locales/en/messages.json`

### Build & Release

Packaging, localization tooling, and distribution artifacts.

- [Manifests](https://github.com/filipeps/traduzir-paginas-web/blob/master/src/manifest.json): extension manifests · Browser-extension packaging definitions for Chromium and Firefox. · `src/manifest.json`
- [Build Pipeline](https://github.com/filipeps/traduzir-paginas-web/blob/master/gulpfile.js): bundling · Bundling and release preparation via build scripts and polyfills. · `gulpfile.js`
- [Release Artifacts](https://github.com/filipeps/traduzir-paginas-web/blob/master/dist/chromium/updates.xml): distribution · Browser-specific release outputs and update metadata. · `dist/chromium/updates.xml`

### External Services

Third-party translation providers used through the background layer.

- **Translation Providers**: external api · External engines such as Google, Yandex, Bing, DeepL, and Libre used for remote translation.

## Connections

- Popup UI → Background: send actions · Popup initiates translation and configuration requests through background messaging.
- Document Flow → Background: request translate · Document translation flow delegates orchestration to background.
- Text Flow → Background: request translate · Selected-text flow delegates orchestration to background.
- Improve Flow → Background: refine text · Improve-translation flow routes through background coordination.
- Options Page → Shared Lib: read config · Options UI uses shared helpers for settings and platform behavior.
- Popup UI → i18n Runtime: labels · Popup UI pulls localized strings at runtime.
- Options Page → i18n Runtime: labels · Options UI uses shared localization access.
- Background → Translation API: delegate · Background hands off provider-specific translation work.
- Background → Cache: cache lookups · Background checks and stores repeated translations.
- Background → Text to Speech: audio · Background exposes speech playback capability.
- Translation API → Translation Providers: remote calls · Translation service calls out to external provider endpoints.
- Page Translator → Translation API: translate text · Content script requests translated text through the background boundary.
- Page Translator → Inject Guard: guard injection · Prevents duplicate page-script installation.
- Page Translator → Page Modes: mode changes · Translator cooperates with show-original and show-translated behavior.
- Page Modes → Mobile Popup: controls · Page mode actions can be surfaced in the mobile in-page popup.
- Background → Page Translator: messaging · Background coordinates with injected page translators in tabs.
- Background → Popup UI: status updates · Background notifies extension UI about translation state.
- Shared Lib → i18n Runtime: uses · Shared helpers provide the runtime localization layer.
- Shared Lib → Locales: loads strings · Shared runtime resolves localized message catalogs.
- Manifests → Build Pipeline: packaging · Extension manifests are assembled into browser-specific builds.
- Build Pipeline → Release Artifacts: outputs · Build process produces release bundles and update metadata.
- Build Pipeline → Manifests: consumes · Bundling is driven by extension manifest definitions.
