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

问题描述:

我对Rails 3.0.1,Ruby 1.9.2和我的网站本地化有些麻烦。

I've got some troubles with the couple Rails 3.0.1, Ruby 1.9.2 and my website localization.

问题很简单,在一个视图中有类似的东西:

The problem is quite simple, i've got something like that in a view :

f.input :zip_code, :label => I18n.t('labels.zip_code')

和一个es.yml文件:

and a es.yml file :

es:
  labels:
    zip_code: "Este código postal no es valido."

en.yml文件没有任何麻烦 i18n.locale =='es'我收到此错误:

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 :

incompatible character encodings: UTF-8 and ASCII-8BIT

我一直在环顾四周但是没有找到使用我的UTF-8翻译文件的方法。

I have been looking around for quite a while but didn't found a way to use my UTF-8 translation files.

有人知道如何使它工作吗?

Did some knows how to make it works ?

感谢您的帮助。

好的,所以问题解决了几个小时的谷歌搜索...

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

我的代码中实际上有两个错误。第一个是文件编码错误,第二个是MySQL数据库配置的问题。

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 :

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 /

其次,为了解决文件编码问题,我在我的config / environment.rb中添加了这两行。

Second, to solve the file encoding problem I added these 2 lines in my config/environment.rb

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

希望这将帮助某人:)