简体   繁体   中英

Chinese Garbled in REPL in scala

When I type in chinese characters in the REPL, only question marks are displayed, as in my second screenshot. How can I fix this? My scala version is 2.9.0.1 and OS windows

在输入中文之前:中国使用搜狗输入法

输入后

无法正确显示中文单词

Even if I start REPL with a property -Dfile.encoding="UTF-8"

You must tell the REPL that you want to use unicode (or whatever encoding). Try

scala -Dfile.encoding="UTF-8"

when opening the REPL.

Ah, you're on Windows. I don't think this is supported at the moment. I some idea of what's going on. See https://issues.scala-lang.org/browse/SI-4711 for the investigation.

The root of the issue is:

  1. jline2 (its jansi dependency) loads a DLL that uses some variant of 8 bits getc to read characters at the console and does not support double byte characters. The API call that could be used instead is ReadConsoleInputW .
  2. The REPL output printing uses the underlying Java System.out which does not print unicode characters reliably - although your first snapshot in your question seems to imply that it works * .

I'm not part of the Scala team, but I tried to fix it and I may have something working more or less. See https://github.com/fusesource/jansi-native/blame/master/src/main/java/org/fusesource/jansi/internal/WindowsSupport.java (based on WriteConsoleW and ReadConsoleInputW and other additions in the REPL ILoop source - the changes are somewhere in my fork on github - not updated in a while.

If you are interested, I may be able to provide an experimental fork of the 2.9.0.1 scala-compiler.jar and jline.jar and you could tell me if it works.

* What is the code that prints :1: xx 2:xx 3:xx 4:x 5:x where x is some chinese character?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM