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
+6 -3
View File
@@ -1,4 +1,5 @@
import { atom } from 'jotai';
import { isSupportedTargetLang } from '../utils/translation/langUtils';
const STORAGE_KEY = 'settings';
export type DateFormat =
@@ -409,10 +410,12 @@ export const getSettings = (): Settings => {
saved.ringtoneId === 'none'
? saved.ringtoneId
: defaultSettings.ringtoneId,
// Coerce persisted target language to a non-empty string; anything else
// (missing/wrong type) falls back to the default.
// Coerce persisted target language to a curated, supported code; anything
// else (missing/wrong type/unknown) falls back to the default so the hook
// never targets a language the engine can't produce.
translateTargetLang:
typeof saved.translateTargetLang === 'string' && saved.translateTargetLang.trim()
typeof saved.translateTargetLang === 'string' &&
isSupportedTargetLang(saved.translateTargetLang)
? saved.translateTargetLang
: defaultSettings.translateTargetLang,
autoTranslate: