Ruby on Rails - 生成的文件编码
我发现 RoR 在创建新应用程序时生成的所有文件似乎都是用 Western(ISO-8859-1)编码保存的(我正在 Windows 计算机上本地开发)。因此,我在使用某些特殊字符(例如 £)时遇到问题。
- RoR 是否应该生成框架文件并使用 Western(ISO-8859-1) 编码保存它们?
- 如果没有,我怎样才能让 RoR 生成文件并使用 UTF-8 编码保存它们,从而避免某些字符(例如 £)出现问题?
请参阅 Ruby on Rails - £ 符号问题,了解我之前提出的与以下相关的未解决问题这个问题。
I have discovered that all the files RoR generates when creating a new application seem to be saved with Western(ISO-8859-1) encoding (I am developing locally on a Windows machine). As a result I am having problems when using certain special characters eg £.
- Should RoR be generating the framework files and saving them with Western(ISO-8859-1) encoding?
- If not, how can I get RoR to generate the files and save them with UTF-8 encoding, and so avoid problems with certain characters eg £?
Please see Ruby on Rails - £ sign troubles for a previous unresolved question I asked relating to this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Rails 仅在生成的文件中使用 ASCII 字符。
ASCII 文件既不是 UTF-8 也不是 ISO-8859-1。 ASCII 与这两种编码兼容,但 ASCII 文件不会成为 ISO-8859-1 或 UTF-8 文件除非您向其中添加特殊字符。
添加 £ 字符后保存文件时,应确保将编辑器或 IDE 设置为使用 UTF-8 而不是 ISO-8859-1。您应该在编辑器中寻找配置选项。 Rails对此无能为力。
如果您运行 Ruby 1.9,还请记住在包含特殊字符的文件顶部设置魔术注释(模板中除外)。在Ruby 1.8及之前的版本中,这个注释没有作用。
完全相同的问题会导致您在其他问题中描述的症状。
有关一些背景信息,请参阅这篇(古老但优秀)有关字符编码和 Unicode 的文章。
Rails only uses ASCII characters in generated files.
ASCII files are neither UTF-8 nor ISO-8859-1. ASCII is compatible with both encodings, but an ASCII file doesn't become an ISO-8859-1 or UTF-8 file until you add a special character to it.
When you save a file after adding a £ character, you should make sure to set up your editor or IDE to use UTF-8 instead of ISO-8859-1. You should look for a configuration option in your editor. Rails cannot do anything about it.
If you run Ruby 1.9, also remember to set a magic comment at the top of a file containing special characters (except in templates). In Ruby 1.8 and previous versions, this comment has no effect.
The exact same problem causes the symptoms you describe in your other question.
For some background, see this (old but excellent) article about character encodings and Unicode.