从Scala解释器打印Unicode [英] Printing Unicode from Scala interpreter

查看:169
本文介绍了从Scala解释器打印Unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Scala解释器时(即在命令行上运行命令"scala"),我无法正确打印unicode字符.当然可以正确打印a-z,A-Z等,但是例如€或ƒ打印为?.

When using the scala interpreter (i.e. running the command 'scala' on the commandline), I am not able to print unicode characters correctly. Of course a-z, A-Z, etc. are printed correctly, but for example € or ƒ is printed as a ?.

print(8364.toChar)

导致?而不是€. 可能是我做错了.我的终端支持utf-8字符,即使将输出通过管道传输到单独的文件并在texteditor中打开它,它也支持utf-8字符.显示.

results in ? instead of €. Probably I'm doing something wrong. My terminal supports utf-8 characters and even when I pipe the output to a seperate file and open it in a texteditor, ? is displayed.

这一切都是在Mac OS X(Snow Leopard,10.6.2)和Scala 2.8(每晚构建)和Java 1.6.0_17)上发生的.

This is all happening on Mac OS X (Snow Leopard, 10.6.2) with Scala 2.8 (nightly build) and Java 1.6.0_17)

推荐答案

我找到了问题的原因,并找到了使之按预期工作的解决方案. 正如我在发布问题并阅读Calum的答案以及在Mac上使用另一个项目(使用Java)在Mac上进行编码的问题后已经怀疑的那样,问题的原因是Mac OS X使用的默认编码.当您启动scala解释程序,它将为指定平台使用默认编码.在Mac OS X上,这是Macroman;在Windows上,可能是CP1252.您可以通过在scala解释器中键入以下命令来进行检查:

I found the cause of the problem, and a solution to make it work as it should. As I already suspected after posting my question and reading the answer of Calum and issues with encoding on the Mac with another project (which was in Java), the cause of the problem is the default encoding used by Mac OS X. When you start scala interpreter, it will use the default encoding for the specified platform. On Mac OS X, this is Macroman, on Windows it is probably CP1252. You can check this by typing the following command in the scala interpreter:

scala> System.getProperty("file.encoding");
res3: java.lang.String = MacRoman

根据scala帮助测试,可以使用-D选项提供Java属性.但是,这对我不起作用.我最终设置了环境变量

According to the scala help test, it is possible to provide Java properties using the -D option. However, this does not work for me. I ended up setting the environment variable

JAVA_OPTS="-Dfile.encoding=UTF-8"

运行scala后,上一条命令的结果将给出以下结果:

After running scala, the result of the previous command will give the following result:

scala> System.getProperty("file.encoding")
res0: java.lang.String = UTF-8

现在,打印特殊字符可以按预期进行:

Now, printing special characters works as expected:

print(0x20AC.toChar)               
€

因此,这不是Scala中的错误,而是默认编码的问题.我认为,最好在所有平台上默认使用UTF-8.在寻求答案的过程中,我遇到了 discussion 在此问题的Scala邮件列表中.在第一则消息中,建议在file.encoding报告Macroman时在Mac OS X上默认使用UTF-8,因为UTF-8是Mac OS X上的默认字符集(让我想知道为什么默认设置了file.encoding对Macroman来说,可能是Mac OS 10发行之前的继承?).我认为该提案不会成为Scala 2.8的一部分,因为Martin Odersky 写道,最好将它们保留在Java中(即尊重file.encoding属性).

So, it is not a bug in Scala, but an issue with default encodings. In my opinion, it would be better if by default UTF-8 was used on all platforms. In my search for an answer if this is considered, I came across a discussion on the Scala mailing list on this issue. In the first message, it is proposes to use UTF-8 by default on Mac OS X when file.encoding reports Macroman, since UTF-8 is the default charset on Mac OS X (keeps me wondering why file.encoding by defaults is set to Macroman, probably this is an inheritance from Mac OS before 10 was released?). I don't think this proposal will be part of Scala 2.8, since Martin Odersky wrote that it is probably best to keep things as they are in Java (i.e. honor the file.encoding property).

这篇关于从Scala解释器打印Unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆