Skip to content

tianci-sh/XPTranslateText

Repository files navigation

Xposed Translate Text

Auto-translate app text prioritizing Local Cache and on-device ML Kit (local server), then Gemini API, with fallback to the Free Google API.

✨ What's New in 2.0

  • Prioritize on-device translation via a built-in local server using Google ML Kit.
  • Added translation support for android.text.StaticLayout$Builder to reduce UI jank by replacing text synchronously when possible.

πŸ“¦ Installation & Setup

Requirements

  • LSPosed or another Xposed Framework variant must be installed and enabled.

Installation Steps

  1. Install the downloaded APK from the link above.
  2. Open the LSPosed Manager app, navigate to Modules, and enable XPTranslateText.
  3. Select the apps you want to translate from the module settings in LSPosed.
  4. Open the XPTranslateText app, enable the "Local Translation Server" switch, and choose your source/target languages.
  5. Kill your app and restart it.

After restart, the selected apps should display translated text automatically.

πŸ› οΈ Hook Methods & Translation Workflow

  • android.widget.TextView & Custom Components:

    • Automatically translates text set via the setText() method.
    • Translation pipeline:
      1. Local Translation Cache (memory + SQLite) to avoid repeated work.
      2. Local ML Kit Server (on-device, 127.0.0.1:18181) with short timeouts to keep UI responsive.
      3. Gemini API (gemini-2.0-flash-lite) as network fallback.
      4. Free Google API as the final fallback.
  • android.text.StaticLayout$Builder:

    • Intercepts StaticLayout.Builder.build() and attempts a synchronous replacement when translations are readily available (cache/DB) or quickly obtained from the local ML Kit server.
    • If not immediately available, it returns the original layout and prefetches translations asynchronously for subsequent renders.
    • Spans and formatting are preserved.
  • android.webkit.WebView:

    • Performs real-time translation of visible webpage text via a JS bridge.
    • Pipeline: Local ML Kit Server β†’ Gemini API β†’ Free Google API (no caching for WebView).

βœ… Compatibility

  • Tested and confirmed working on:
    • Android 13 with LSPosed (v1.9.2-it(7024))
    • Android 15 with LSPosed (v1.9.2-it(7024))
    • Android 16 with LSPosed (v1.9.2-it(7412))

🎬 Live Demo (Reddit Translation)

Live Reddit translation via on-device ML Kit

πŸ–ΌοΈ Before & After Comparison

Before After

πŸ“ Project Structure

XPTranslateText/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ key.jks              # Generated by GitHub Actions
β”‚   └── build.gradle
β”œβ”€β”€ gradlew
β”œβ”€β”€ gradlew.bat
β”œβ”€β”€ keystore.properties      # Local-only, not committed
└── settings.gradle

🧩 Local ML Kit Server

  • Runs as a foreground service on 127.0.0.1:18181.
  • Endpoint: /translate?src=xx&dst=yy&q=...
    • src=auto enables automatic language detection (ML Kit Language ID).
  • Models are downloaded on-demand and kept on-device; last-used times are tracked to help with maintenance.

Star History

Star History Chart

About

xposed module translate text by MLKit / gemini2.0 / google api

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages