Skip to content

Add opt-in note translation (LibreTranslate) — closes #1055 - #1122

Open
tegar07257 wants to merge 1 commit into
PrimalHQ:mainfrom
tegar07257:feat/note-translation-1055
Open

tegar07257 wants to merge 1 commit into
PrimalHQ:mainfrom
tegar07257:feat/note-translation-1055

Conversation

@tegar07257

Copy link
Copy Markdown

Closes #1055

Summary

Adds opt-in, on-demand note translation to the Android app, mirroring the web implementation (PrimalHQ/primal-web-app#133, PR #240) and following the design direction from this issue's discussion:

  • A Translate button appears under a note's content — the bar lives in FeedNoteCard, which renders notes in both the feed and the thread screen — only when the feature is enabled in Settings and the note looks translatable. Translation runs only on an explicit tap — never automatically.
  • The result is shown under the original text with a "Translated from X" label and a Hide translation / Show translation toggle. Nothing is translated or sent anywhere while the feature is off.
  • The endpoint is a user-configured LibreTranslate-compatible server (Settings → Translation): endpoint URL, optional API key, and target language, with a Test connection action that loads the endpoint's language list. There is no hard-coded third-party translation service and no dependency on undocumented endpoints.

Design

New package net.primal.android.notes.translation, structured after the web port:

  • TranslationSanitizer — before anything is sent to the endpoint, Nostr/Lightning entities are replaced with opaque placeholders and restored afterwards: nostr: URIs, npub/nsec/note/nevent/nprofile/naddr/ncryptsec, bare bech32, Lightning invoices (lnbc/lntb/lnbcrt/lnsb), LNURL, Cashu tokens, URLs, hashtags, mentions, #[n] references, bare 64-char hex, and e-mail addresses. Placeholders are salted when a note's own text resembles a placeholder, so restore can never rewrite user text.
  • LanguageDetector — lightweight local detection (script ranges + stopwords for 10 Latin-script languages) used only to avoid offering translation for notes already in the target language, and as a fallback label when the provider does not report a source language.
  • TranslationCache — bounded in-memory LRU cache (200 entries / ~1M chars), keyed by a hash of endpoint + target language + API key + content.
  • TranslationService / LibreTranslateProvider — orchestration and a Ktor-based LibreTranslate client (POST /translate, GET /languages). The feature uses its own HTTP client, deliberately without the app's default configuration, because the default config logs request bodies and translation requests can carry the user's API key.
  • NoteTranslationManager — app-wide entry point (Hilt @Singleton + @EntryPoint, following the SimpleCacheFactoryProvider pattern) owning the settings store, cache and service.
  • Settings live in a dedicated Settings → Translation screen (following the Content Display settings pattern), persisted locally only: non-secret fields in private preferences, the API key exclusively in EncryptedSharedPreferences (Jetpack Security, already in the app's version catalog) — if the keystore is unavailable the key is simply not persisted (fail closed, never plaintext) — and the API key field is masked in the UI.

Privacy & safety

  • The feature is off by default and translation is tap-only.
  • An API key is only ever sent over HTTPS or to a loopback address; the host is validated with a real URI parse (userinfo tricks are rejected). Otherwise the key is withheld and the user gets a clear error.
  • The translation HTTP client never follows redirects, so a redirect cannot forward the request body (and key) to a host the user did not configure.
  • Secret-key material (nsec, ncryptsec, bare hex private keys) is masked before translation, so a note quoting a secret never forwards it to the endpoint.

Test plan

  • New unit tests under app/src/test/kotlin/net/primal/android/notes/translation/: sanitizer (masking/restore round-trips incl. placeholder-collision cases, eligibility rules), language detector, cache (keying incl. API key, LRU + char-budget eviction and accounting), and the service (fake provider: masking end-to-end, cache hits, endpoint-security rules incl. userinfo bypass attempts, error mapping).
  • Honest limitation: the tests could not be executed with Gradle in the author's sandbox — the environment intercepts outbound TCP from Java processes, so the Gradle daemon cannot start (Could not dispatch a message to the daemon). The pure-Kotlin core (sanitizer, detector, cache, models, service) was compiled standalone with kotlinc and exercised by a behavioral harness mirroring the unit tests: 75/75 checks pass. The Compose/Hilt/Ktor wiring follows existing in-repo patterns but has not been compiled by Gradle; CI will be the first full build.

🤖 Prepared with the help of an AI assistant; the design follows the web implementation in PrimalHQ/primal-web-app#240.

…en protection

Implements the Android counterpart of primal-web-app PR PrimalHQ#240 for issue
PrimalHQ#1055: a Translate action under feed notes, backed by a user-configured
LibreTranslate-compatible endpoint.

- Masking of nostr: URIs, bech32 entities, #[n] refs, URLs, Lightning
  invoices/LNURLs, Cashu tokens, bc1 addresses, hashtags, mentions and
  emoji codes before translation, restored afterwards (with a
  second-chance pass for provider-mangled placeholders)
- Offline language detection (script + stopwords) to hide the action for
  notes already in the target language and to label results
- Bounded in-memory result cache keyed by endpoint|target|content hash
- Settings screen (Settings -> Translation): enable toggle, endpoint,
  optional API key (device-only storage), target language with picker
  from the endpoint's /languages, connection test, cache management
- Translation only ever runs on an explicit tap; cancellable; graceful
  errors (disabled / not configured / unreachable / provider error)
- Unit tests for sanitizer, detector, cache and service orchestration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Translate notes to user's preferred language

1 participant