我有一个 Rails 项目,其中有很多西里尔字符串。
它在 Ruby 1.8 上运行良好,但 Ruby 1.9 假定源文件是 US-ASCII 编码的,除非您在源文件顶部提供 #encoding: utf-8
注释。此时,这些文件不被视为 US-ASCII
。
有没有更简单的方法来告诉 Ruby“此应用程序是 UTF8 编码的。除非另有声明,否则请将所有和任何包含的源文件视为 UTF8”?
更新:
我写了“如何在 Ruby 1.9 文件中自动插入编码:UTF-8 指令",如果需要,它会自动附加编码指令。
I have a Rails project with a lot of Cyrillic strings in it.
It worked fine on Ruby 1.8, but Ruby 1.9 assumes source files are US-ASCII-encoded unless you provide an # encoding: utf-8
comment at the top of the source file. At that point the files are not considered US-ASCII
.
Is there a simpler way to tell Ruby "This application is UTF8-encoded. Please consider all and any included source files as UTF8 unless declared otherwise"?
UPDATE:
I wrote "How to insert the encoding: UTF-8 directive automatically in Ruby 1.9 files" which appends the encoding directive automatically if it's needed.
发布评论
评论(7)
我认为您可以
-E utf-8
命令行参数来ruby
,或者RUBYOPT
环境变量设置为"- E utf-8"
I think you can either
-E utf-8
command line argument toruby
, orRUBYOPT
environment variable to"-E utf-8"
在我看来,显式并不总是比隐式更好。
当您使用的几乎所有源代码都兼容 UTF-8 时,您可以使用 Ruby 的
-Ku
命令行选项轻松避免添加神奇的编码注释。不要将
-K
选项的“u
”参数与-U
选项混淆。然后,仅在需要它的脚本中设置魔术编码注释。请记住,约定优于配置!
您可以设置环境变量
RUBYOPT=-Ku
请参阅 Ruby 的命令行选项:http:// /www.manpagez.com/man/1/ruby/。
In my opinion, explicit is not always better than implicit.
When almost all the source you use is UTF-8 compatible, you can easily avoid putting the magic encoding comment by using Ruby's
-Ku
command-line options.Don't confuse the "
u
" parameter of the-K
options with-U
options.Then, set the magic encoding comment only in scripts that need it. Remember, convention over configuration!
You can set the environment variable
RUBYOPT=-Ku
See Ruby's command-line options at http://www.manpagez.com/man/1/ruby/.
显式的比隐式的好。写出编码的名称对于您的文本编辑器、解释器和任何其他想要查看该文件的人都有好处。不同的平台有不同的默认值 - UTF-8、Windows-1252、Windows-1251 等 - 如果您自动选择其中之一,则会妨碍可移植性或平台集成。需要更明确的编码是一件好事。
将 Rails 应用程序与 GetText 集成可能是个好主意。然后,您的所有 UTF-8 字符串将被隔离到少量翻译文件中,并且您的 Ruby 模块将是干净的 ASCII。
Explicit is better than implicit. Writing out the name of the encoding is good for your text editor, your interpreter, and anyone else who wants to look at the file. Different platforms have different defaults -- UTF-8, Windows-1252, Windows-1251, etc. -- and you will either hamper portability or platform integration if you automatically pick one over the other. Requiring more explicit encodings is a Good Thing.
It might be a good idea to integrate your Rails app with GetText. Then all of your UTF-8 strings will be isolated to a small number of translation files, and your Ruby modules will be clean ASCII.
有一个 gem 可以在 Rails 项目中需要它的每个文件的顶部设置魔术注释: https:// github.com/m-ryan/magic_encoding
您只需安装它并在项目的根目录中运行
magic_encoding
,问题就解决了。There's a gem that sets the magic comment on top on every file that needs it in a Rails project : https://github.com/m-ryan/magic_encoding
You just install it and run
magic_encoding
in the root of your project, problem solved.这不是直接的答案,但根据您的编码环境,您可以让编辑器来处理事情。例如,Emacs 的
ruby-mode
具有变量ruby-insert-encoding-magic-comment
:我确信其他编辑也有类似的事情。当然,这仍然意味着向每个文件添加神奇的注释,但至少编辑器会自动为您完成此操作,而无需您记住。
Not a direct answer, but depending on your coding environment you can let the editor take care of things. Emacs'
ruby-mode
for example has the variableruby-insert-encoding-magic-comment
:I'm sure there's something similar for other editors. Sure, it still means adding the magic comment to every file, but at least the editor does it for you automatically instead of you having to remember.
确保所有文件(源文件和资产)在运行时都使用您首选的编码加载的唯一万无一失(且干燥!)的 1.9 方法是使用 -E 命令行参数。
所有其他方法都有缺点,具体取决于您的系统(例如,无法设置 ENV 变量、首先加载的第三方代码导致不适合使用
Encoding.default_external
,...)。我的生产服务器使用以下包装脚本:(
确保调整路径)
The only foolproof (and DRY!) 1.9 way of ensuring that all your files (source and assets) are loaded with your preferred encoding at run-time is to use the -E command line argument.
All the other approaches have drawbacks depending on your system (e.g. impossible to set ENV vars, third-party code loaded first making unsuitable to use
Encoding.default_external
, ...).My production servers use the following wrapper script:
(make sure to adapt the path)
我没有遇到这么多,但是当我需要确保 UTF-8 时,我使用 $KCODE 全局。尝试将其放入您的环境中。rb:
$KCODE = 'UTF8'
另外,您确定您的编辑器正在以 UTF-8 保存文件吗?
I don't run into this much, but when I need to ensure UTF-8, I use the $KCODE global. Try putting this in your environment.rb:
$KCODE = 'UTF8'
Also, are you certain that your editor is saving files in UTF-8?