Skip to content

fix: improve language detection for IME composition (Chinese input) - #1230

Open
p0ssword wants to merge 1 commit into
pot-app:masterfrom
p0ssword:master
Open

fix: improve language detection for IME composition (Chinese input)#1230
p0ssword wants to merge 1 commit into
pot-app:masterfrom
p0ssword:master

Conversation

@p0ssword

@p0ssword p0ssword commented Jan 9, 2026

Copy link
Copy Markdown

Problem

When typing Chinese characters using an IME (Input Method Editor), the language detection was frequently incorrect, often detecting the text as English instead of Chinese.

Root Cause

The language detection was triggered during the IME composition process (while the user is still typing), leading to detection of incomplete text fragments.

Solution

Implemented IME composition event handling to prevent language detection during composition:

  • Added compositionstart and compositionend event handlers
  • Track IME composition state with isComposingRef flag
  • Skip language detection while IME composition is active
  • Perform language detection immediately after composition completes

Changes

  • Modified SourceArea/index.jsx to add composition event handlers
  • Updated changeSourceText function to check composition state
  • Language detection now waits for complete text input before detection

Testing

✓ Tested with Chinese IME input
✓ Language is now correctly detected as Chinese
✓ Works with dynamic translation feature

- Add compositionstart/compositionend event handlers to track IME composition state
- Prevent language detection while user is composing text with IME
- Use isComposingRef flag to skip detection during IME composition
- Perform language detection immediately after composition completes
- Fix issue where Chinese input was incorrectly detected as English
JasonWues added a commit to JasonWues/gloss that referenced this pull request Aug 16, 2026
Adapts two upstream fixes (pot-app#1178, pot-app#1230) that predate
this fork's Tauri 2 migration and so could not be merged as they stand.

Typing Chinese means typing pinyin, and the textarea holds those raw
letters until a candidate is chosen. Two paths mistook that for text the
user had finished writing:

- With dynamic translate on, every keystroke armed the one second timer,
  so a translation of half-typed syllables would fire mid-word. The timer
  is checked again when it runs, since composition can start after it was
  armed. `compositionend` translates straight away, so the wait is not
  paid twice.
- Enter selects the highlighted candidate, and that keystroke reaches the
  keydown handler before the composition ends. Pressing it therefore
  translated whatever pinyin was in the box. Upstream's patch does not
  cover this one, and it is the worse of the two: dynamic translate is
  off by default, this is not.

Enter is guarded on the event's own `isComposing` rather than the ref,
because the browser sets it on exactly the keystroke in question, whereas
`compositionend` has not run yet at that point. A genuine Enter after the
word is committed reports false and still submits.

Ollama gets `think: false`. Reasoning models otherwise spend their budget
deliberating over a translation and, depending on the server version,
return that reasoning as the message content -- so it arrives as if it
were the translation. `ChatRequest.think` is in the pinned client, 0.6.3.

Verified in the running app by dispatching composition events: Enter
while composing is not treated as submit and Enter afterwards is; four
keystrokes over more than a second while composing issue no request, and
`compositionend` issues one immediately.

Prettier is not run over SourceArea. It was already non-conformant before
this change and reformatting would rewrite about a thousand lines around
a forty line fix; the added lines match the configured style on their own.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.

1 participant