简体   繁体   English

中文在scala中用REPL乱码

[英]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. 当我在REPL中输入中文字符时,只显示问号,如我的第二个屏幕截图所示。 How can I fix this? 我怎样才能解决这个问题? My scala version is 2.9.0.1 and OS windows 我的scala版本是2.9.0.1和操作系统窗口

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

输入后

无法正确显示中文单词

Even if I start REPL with a property -Dfile.encoding="UTF-8" 即使我用属性-Dfile.encoding="UTF-8"启动REPL

You must tell the REPL that you want to use unicode (or whatever encoding). 您必须告诉REPL您要使用unicode(或任何编码)。 Try 尝试

scala -Dfile.encoding="UTF-8"

when opening the REPL. 打开REPL时。

Ah, you're on Windows. 啊,你在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. 有关调查,请参阅https://issues.scala-lang.org/browse/SI-4711

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. jline2(它的jansi依赖项)加载一个DLL,该DLL使用8位getc一些变量来读取控制台上的字符,并且不支持双字节字符。 The API call that could be used instead is ReadConsoleInputW . 可以使用的API调用是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 * . REPL输出打印使用底层的Java System.out ,它不能可靠地打印unicode字符 - 尽管你问题中的第一个快照似乎暗示它有效*

I'm not part of the Scala team, but I tried to fix it and I may have something working more or less. 我不是Scala团队的成员,但我试图修复它,我可能会有更多或更少的工作。 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. 请参阅https://github.com/fusesource/jansi-native/blame/master/src/main/java/org/fusesource/jansi/internal/WindowsSupport.java (基于WriteConsoleWReadConsoleInputW以及REPL ILoop源中的其他添加ILoop - 更改是在github上的fork中的某个地方 - 暂时没有更新。

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. 如果您有兴趣,我可以提供2.9.0.1 scala-compiler.jar和jline.jar的实验分支,您可以告诉我它是否有效。

* What is the code that prints :1: xx 2:xx 3:xx 4:x 5:x where x is some chinese character? * 打印的代码是什么:1: xx 2:xx 3:xx 4:x 5:x其中x是一些中文字符?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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