Skip to content

Translate notes to user's preferred language - #1118

Open
alkeke45-cpu wants to merge 1 commit into
PrimalHQ:mainfrom
alkeke45-cpu:feature/translate-notes-1055
Open

alkeke45-cpu wants to merge 1 commit into
PrimalHQ:mainfrom
alkeke45-cpu:feature/translate-notes-1055

Conversation

@alkeke45-cpu

Copy link
Copy Markdown

Closes #1055

What this adds

Inline translation of text notes into the user's preferred language, directly in the note footer — the X-style flow requested in #1055. This is the Android counterpart of the translation feature proposed for the web app in PrimalHQ/primal-web-app#133 (PR PrimalHQ/primal-web-app#241), porting the same service logic to Kotlin so the platforms behave identically.

Unlike the LibreTranslate-based approaches, this needs zero user setup: no API key, no endpoint configuration, no backend.

How it works

  • A Translate action (new globe icon) appears in the note footer, above the actions row of every text note. The translation renders inline with a "Translated from {language}" header and a Show/Hide translation toggle.
  • Target language is the user's preferred language from the device/app locale (Locale.getDefault()), normalized for the translation endpoint (en-us → en, zh-CN kept, nb → no, …).
  • Provider: the keyless Google Translate gtx endpoint — no API key, no backend. 15s timeout (connect/read/write/call) with one retry on HTTP 429/5xx; failures degrade gracefully to a subtle "Translation failed. Try again." hint and never crash or block the feed.
  • Nostr entities are protected before translation: nostr: URIs, bech32 IDs (npub/note/nevent/naddr/nprofile), URLs, hashtags, Lightning invoices and Cashu tokens are replaced with placeholders and restored afterwards, so mentions, links and invoices are never mangled.
  • Long notes are chunked at ≤1000 chars on paragraph/sentence/word boundaries; results are cached in-memory keyed by an FNV-1a content hash + target language (200-entry bound), so re-renders and scroll-backs never refetch.
  • UI strings go through string resources (note_translate_*, accessibility_translate_note).

Files

  • app/src/main/kotlin/net/primal/android/notes/translation/NoteTranslationService.kt (new) — translation service: entity protection, chunking, bounded cache, language normalization, gtx client on OkHttp + kotlinx.serialization.
  • app/src/main/kotlin/net/primal/android/notes/translation/NoteTranslation.kt (new) — result model.
  • app/src/main/kotlin/net/primal/android/notes/feed/note/ui/NoteTranslationSection.kt (new) — Translate action + inline translated block with header and Show/Hide toggle.
  • app/src/main/kotlin/net/primal/android/notes/feed/note/FeedNoteCard.kt — renders the section in the note footer between the content and the actions row, aligned with the content indent.
  • app/src/main/kotlin/net/primal/android/core/compose/icons/primaliconpack/FeedTranslate.kt (new) — globe icon in the existing 16dp icon-pack format.
  • app/src/main/res/values/strings.xml — new note_translate_* strings.

Verification

  • Implemented against the app's existing patterns (Hilt-era app module, OkHttp, CommonJson from net.primal.core.utils.serialization, AppTheme, icon-pack format) and structurally reviewed end-to-end: every import resolves to a real class in this repo, call-site signatures match, brace/paren balance and the 120-char line limit verified, strings.xml re-parsed as valid XML.
  • A full Gradle build was not run: the build environment used for this contribution has no JDK/Android SDK installed, so :app:compileDebugKotlin could not be executed there. The change is additive (6 files, +600/−0) and touches no existing signatures, so CI should give a clean signal.
  • The translation logic is a line-for-line port of the web service that was logic-tested with mocked fetch (entity round-trip exact; 4,000-char note chunked ×4 and reassembled losslessly; cache hit avoids refetch).

Submitted for the Lightning Bounties reward on this issue (50,000 sats).

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