如何避免将魔术编码注释置于Ruby 1.9中的每个UTF-8文件之上? [英] How can I avoid putting the magic encoding comment on top of every UTF-8 file in Ruby 1.9?

查看:92
本文介绍了如何避免将魔术编码注释置于Ruby 1.9中的每个UTF-8文件之上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails项目,里面有很多西里尔字符串。



它在Ruby 1.8中工作正常,但Ruby 1.9假定源文件是US-ASCII编码的,除非您提供一个#encoding:utf-8 注释在源文件的顶部。在这一点上,文件不被认为是 US-ASCII



有一个更简单的方法来告诉Ruby这个应用程序是UTF8编码的,请将所有和任何包含的源文件视为UTF8,除非另有声明?






更新: / p>

我写了如何在Ruby 1.9文件中自动插入编码:UTF-8指令,如果需要,它会自动附加编码指令。 p>

解决方案

显式优于隐含。编写编码的名称对于您的文本编辑器,您的解释器以及要查看该文件的任何其他人都是有益的。不同的平台具有不同的默认值 - UTF-8,Windows-1252,Windows-1251等 - 如果您自动选择一个,则您将阻止可移植性或平台集成。要求更明确的编码是一件好事。



将Rails应用程序与GetText集成起来可能是个好主意。那么所有的UTF-8字符串将被隔离到少量的翻译文件,而你的Ruby模块将是干净的ASCII。


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.

解决方案

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.

这篇关于如何避免将魔术编码注释置于Ruby 1.9中的每个UTF-8文件之上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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