Ruby on Rails 3,不兼容的字符编码:UTF-8 和 ASCII-8BIT 与 i18n
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4188677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby on Rails 3, incompatible character encodings: UTF-8 and ASCII-8BIT with i18n
提问by Nicolas Guillaume
I've got some troubles with the couple Rails 3.0.1, Ruby 1.9.2 and my website localization.
我在 Rails 3.0.1、Ruby 1.9.2 和我的网站本地化方面遇到了一些麻烦。
The problem is quite simple, i've got something like that in a view :
问题很简单,我有这样的观点:
f.input :zip_code, :label => I18n.t('labels.zip_code')
and a es.yml file :
和一个 es.yml 文件:
es:
labels:
zip_code: "Este código postal no es valido."
There are no troubles with the en.yml file (it's pure ASCII) but when the website is set with i18n.locale == 'es'I get this error :
en.yml 文件(它是纯 ASCII)没有问题,但是当网站设置为i18n.locale == 'es'我收到此错误时:
incompatible character encodings: UTF-8 and ASCII-8BIT
I have been looking around for quite a while but didn't found a way to use my UTF-8 translation files.
我已经环顾了很长一段时间,但没有找到使用我的 UTF-8 翻译文件的方法。
Did some knows how to make it works ?
有人知道如何使它起作用吗?
Thanks for your help.
谢谢你的帮助。
回答by Nicolas Guillaume
Ok so problem solved after some hours of googling...
好的,经过几个小时的谷歌搜索后问题解决了......
There was actually two bugs in my code. The first one was a file encoding error and the second was the problem with the MySQL Data base configuration.
我的代码中实际上有两个错误。第一个是文件编码错误,第二个是 MySQL 数据库配置问题。
First, to solve the error caused by MySQL I used this two articles :
首先,为了解决MySQL引起的错误,我使用了这两篇文章:
http://www.dotkam.com/2008/09/14/configure-rails-and-mysql-to-support-utf-8/
http://www.dotkam.com/2008/09/14/configure-rails-and-mysql-to-support-utf-8/
http://www.rorra.com.ar/2010/07/30/rails-3-mysql-and-utf-8/
http://www.rorra.com.ar/2010/07/30/rails-3-mysql-and-utf-8/
Second, to solve the file encoding problem I added these 2 lines in my config/environment.rb
其次,为了解决文件编码问题,我在 config/environment.rb 中添加了这两行
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
Hopefully this will help someone :)
希望这会帮助某人:)
回答by Ritesh Kumar
I solved most of the problems by combining many solutions:
我通过结合许多解决方案解决了大部分问题:
- Make sure
application.rbhas this line:config.encoding = "utf-8". - Make sure you are using 'mysql2' gem
- Putting
# encoding: utf-8at the top of any file containing utf-8 characters. Add the following two lines above the
<App Name>::Application.initialize!line inenvironment.rb:Encoding.default_external = Encoding::UTF_8 Encoding.default_internal = Encoding::UTF_8
- 确保
application.rb有这一行:config.encoding = "utf-8"。 - 确保您使用的是“mysql2” gem
- 放在
# encoding: utf-8任何包含 utf-8 字符的文件的顶部。 <App Name>::Application.initialize!在 in行上方添加以下两行environment.rb:Encoding.default_external = Encoding::UTF_8 Encoding.default_internal = Encoding::UTF_8
http://rorguide.blogspot.com/2011/06/incompatible-character-encodings-ascii.html
http://rorguide.blogspot.com/2011/06/incompatible-character-encodings-ascii.html
回答by flunder
Using this unpack function helped me sort this out finally, try this if you get the can't convert error message:
使用此解包功能帮助我最终解决了这个问题,如果您收到无法转换的错误消息,请尝试此操作:
myString.unpack('U*').pack('U*')
回答by noodl
Make sure you have config.encoding = "utf-8"in your config/application.rb. Also, your example translation file doesn't match the key you're searching for (com_nameand first_name) but I suppose that could just be a typo.
确保您config.encoding = "utf-8"的config/application.rb. 此外,您的示例翻译文件与您正在搜索的键 (com_name和first_name)不匹配,但我认为这可能只是一个错字。
回答by Fábio Batista
Are you sure your es.ymlfile was saved as UTF-8?
您确定您的es.yml文件已保存为 UTF-8 格式吗?
If you're on Windows, use http://notepad-plus-plus.org/to make sure.
如果您使用的是 Windows,请使用http://notepad-plus-plus.org/进行确认。

