Ruby on Rails 3,不兼容字符编码:UTF-8和ASCII-8BIT与i18n。

时间:2023-01-06 13:17:07

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 :

这是没有问题的。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.

谢谢你的帮助。

5 个解决方案

#1


47  

Ok so problem solved after some hours of googling...

好了,在google了几个小时之后问题就解决了。

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.

我的代码中有两个bug。第一个是文件编码错误,第二个是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 :)

希望这能帮助到某人

#2


23  

I solved most of the problems by combining many solutions:

我通过结合许多解决方案来解决大部分问题:

  • Make sure application.rb has this line: config.encoding = "utf-8".
  • 确保应用程序。rb有这一行:config。编码=“utf - 8”。
  • Make sure you are using 'mysql2' gem
  • 确保使用了“mysql2”gem。
  • Putting # encoding: utf-8 at the top of any file containing utf-8 characters.
  • 将#编码:utf-8在任何包含utf-8字符的文件的顶部。
  • Add the following two lines above the <App Name>::Application.initialize! line in environment.rb:

    在< appname >::Application.initialize中添加以下两行。在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

#3


0  

Are you sure your es.yml file was saved as UTF-8?

你确定你的名字吗?yml文件被保存为UTF-8?

If you're on Windows, use http://notepad-plus-plus.org/ to make sure.

如果您在Windows上,请使用http://notepad-plus.org/来确保。

#4


0  

Using this unpack function helped me sort this out finally, try this if you get the can't convert error message:

使用这个unpack函数帮助我最终解决了这个问题,如果你得到了不能转换错误信息的方法:

myString.unpack('U*').pack('U*')

#5


0  

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_name and first_name) but I suppose that could just be a typo.

确保配置。在你的config/application.rb中编码= "utf-8"。另外,您的示例翻译文件与您搜索的键(com_name和first_name)不匹配,但我认为这可能只是一个输入错误。

#1


47  

Ok so problem solved after some hours of googling...

好了,在google了几个小时之后问题就解决了。

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.

我的代码中有两个bug。第一个是文件编码错误,第二个是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 :)

希望这能帮助到某人

#2


23  

I solved most of the problems by combining many solutions:

我通过结合许多解决方案来解决大部分问题:

  • Make sure application.rb has this line: config.encoding = "utf-8".
  • 确保应用程序。rb有这一行:config。编码=“utf - 8”。
  • Make sure you are using 'mysql2' gem
  • 确保使用了“mysql2”gem。
  • Putting # encoding: utf-8 at the top of any file containing utf-8 characters.
  • 将#编码:utf-8在任何包含utf-8字符的文件的顶部。
  • Add the following two lines above the <App Name>::Application.initialize! line in environment.rb:

    在< appname >::Application.initialize中添加以下两行。在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

#3


0  

Are you sure your es.yml file was saved as UTF-8?

你确定你的名字吗?yml文件被保存为UTF-8?

If you're on Windows, use http://notepad-plus-plus.org/ to make sure.

如果您在Windows上,请使用http://notepad-plus.org/来确保。

#4


0  

Using this unpack function helped me sort this out finally, try this if you get the can't convert error message:

使用这个unpack函数帮助我最终解决了这个问题,如果你得到了不能转换错误信息的方法:

myString.unpack('U*').pack('U*')

#5


0  

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_name and first_name) but I suppose that could just be a typo.

确保配置。在你的config/application.rb中编码= "utf-8"。另外,您的示例翻译文件与您搜索的键(com_name和first_name)不匹配,但我认为这可能只是一个输入错误。