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

查看:24
本文介绍了从 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 字符,即使我将输出通过管道传输到一个单独的文件并在文本编辑器中打开它,?显示.

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 (nightly build) 和 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 的答案以及使用另一个项目(在 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 会更好.如果考虑到这一点,我在寻找答案时遇到了一个讨论 在有关此问题的 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天全站免登陆