不兼容的字符编码

不兼容的字符编码

本文介绍了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 文件(它是纯 ASCII)没有问题,但是当网站设置为 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

希望这会对某人有所帮助:)

Hopefully this will help someone :)

这篇关于Ruby on Rails 3,不兼容的字符编码:UTF-8 和 ASCII-8BIT 与 i18n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:46