fix(translation): address review findings

Follow-up hardening from two review passes on the on-device translation
feature:

- Privacy (HIGH): the translation cache is decrypted message plaintext,
  but logout did not clear it (unlike the search index), leaving up to
  300 cleartext bodies in localStorage on shared devices. Add
  clearTranslationCache() and call it from both logout paths
  (logoutClient and the server-forced SessionLoggedOut handler).
- Edited messages (MEDIUM): the cache key was eventId:target with no
  content dependence, so an edit reused the pre-edit translation. Fold a
  content fingerprint into the key, and re-arm the auto-translate
  one-shot when the text changes.
- Settings (LOW): coerce a persisted translateTargetLang to a supported
  curated code so the hook never targets a language the engine can't
  produce (previously only the UI clamped it).
- Chinese (LOW): restore canonical BCP-47 case (zh-Hant / zh-Hans) at
  the Translator API boundary, since normalizeLang lowercases the script
  subtag for internal keys.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-18 16:18:27 -04:00
co-authored by Claude Opus 4.8
parent ecb7b1a7fb
commit c77ab346d3
7 changed files with 60 additions and 13 deletions
+9 -5
View File
@@ -49,7 +49,9 @@ export const useMessageTranslation = (eventId: string, text: string): MsgTransla
const [active, setActive] = useAtom(msgTranslationActiveAtomFamily(eventId));
const [cache, setCache] = useAtom(translationCacheAtom);
const key = makeCacheKey(eventId, targetLang);
// The key includes a fingerprint of `text`, so an edit (same event id, new
// body) misses the old entry and re-translates instead of showing stale text.
const key = makeCacheKey(eventId, targetLang, text);
const cached = findTranslation(cache, key);
const [status, setStatus] = useState<MsgTranslationStatus>('idle');
@@ -72,11 +74,13 @@ export const useMessageTranslation = (eventId: string, text: string): MsgTransla
// Auto-translate: when enabled, flip `active` on for foreign messages whose
// model is already downloaded (no gesture/download needed). Messages needing
// a first-time download keep the manual chip (which provides the gesture).
const autoTried = useRef(false);
// Store the text we last auto-tried so an edit (new text) re-triggers auto
// detection instead of being suppressed by a one-shot flag.
const autoTriedText = useRef<string | undefined>(undefined);
useEffect(() => {
if (!supported || !autoTranslate || active || cached || autoTried.current || !eventId) return;
if (!text.trim()) return;
autoTried.current = true;
if (!supported || !autoTranslate || active || cached || !eventId) return;
if (!text.trim() || autoTriedText.current === text) return;
autoTriedText.current = text;
let alive = true;
(async () => {
const from = await engine.detect(text);