Repository navigation
Add opt-in note translation (LibreTranslate) — closes #1055 - #1122
Open
tegar07257 wants to merge 1 commit into
Open
tegar07257 wants to merge 1 commit into
tegar07257 wants to merge 1 commit into
Conversation
…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
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.
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:
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.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 theSimpleCacheFactoryProviderpattern) owning the settings store, cache and service.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
nsec,ncryptsec, bare hex private keys) is masked before translation, so a note quoting a secret never forwards it to the endpoint.Test plan
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).Could not dispatch a message to the daemon). The pure-Kotlin core (sanitizer, detector, cache, models, service) was compiled standalone withkotlincand 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.