34

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')

and a es.yml file :

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 :

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.

Did some knows how to make it works ?

Thanks for your help.

Nicolas Guillaume
  • 8,160
  • 6
  • 35
  • 44
  • This is sort of off topic, but Ryan Bates just posted a railscast (http://railscasts.com/episodes/256-i18n-backends) on i18n today. If anyone sees this comment the week of 3/6/2011, then in a way this comment is sort of relevant to you. – aarona Mar 07 '11 at 18:04
  • 2
    James has a pretty nice tutorial on his blog, coverings almost all aspects of string encoding in Ruby 1.9. To understand what "external" and "internal" encoding mean, read it at: http://blog.grayproductions.net/articles/ruby_19s_three_default_encodings – Nicolas Jul 29 '11 at 18:21
  • Here's an awesome explanation of encoding on Rails: http://yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails – B Seven Aug 30 '11 at 19:09

5 Answers5

47

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.

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/

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 :)

Nicolas Guillaume
  • 8,160
  • 6
  • 35
  • 44
  • 2
    Thanks a bunch!! Had the same problem, and I had everything alright except I was using the mysql gem instead of mysql2. – Amokrane Chentir Mar 09 '11 at 23:38
  • 1
    Same thing happened to me, but I only noticed it when a source code file had a funny ' character in it. I had both mysql and mysql2 gems in my gemfile, I had to remove the old mysql gem. (and alter my database.yml file) – DGM Mar 10 '11 at 03:33
  • 1
    Dude, you saved my day or maybe a week! Thanks! – zigomir Apr 03 '11 at 15:28
  • 1
    I am getting same error but when using rails cache. Here is my question http://stackoverflow.com/questions/11114872/rails-character-encoding-issue-while-using-cache – MKumar Jun 27 '12 at 07:58
  • AHHHH! Thanks! Also have to upgrade to mysql2 gem and update database.yml accordingly. Worked for me too. – jpgeek Aug 30 '12 at 11:42
  • I am getting this error on Ruby 2.0.0p576 (but not 2.0.0 p481) when putting a string to a Protocol Buffers message. – Paul Nov 19 '14 at 05:15
23

I solved most of the problems by combining many solutions:

  • Make sure application.rb has this line: config.encoding = "utf-8".
  • Make sure you are using 'mysql2' gem
  • Putting # encoding: utf-8 at the top of any file containing utf-8 characters.
  • Add the following two lines above the <App Name>::Application.initialize! line 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

Besi
  • 22,579
  • 24
  • 131
  • 223
Ritesh Kumar
  • 2,183
  • 2
  • 20
  • 32
0

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*')
dusan
  • 9,104
  • 3
  • 35
  • 55
flunder
  • 504
  • 6
  • 9
  • It sounds a little bit CPU expensive and painful if you have to do it on all the strings... – Nicolas Guillaume Apr 10 '12 at 17:26
  • Wish i could have got any other options to stop this error, but they didn't work for me. I haven't noticed any cpu increase, – flunder Apr 11 '12 at 13:45
  • know what I'll try the top answer solution again lateron just because and report back – flunder Apr 11 '12 at 13:47
  • Is your problem comming from characters in the DB or in the view files? – Nicolas Guillaume Apr 13 '12 at 00:22
  • hey haven't found time :/ I've run into this problem while scraping really and only then ~ so before it hits any database. – flunder Apr 13 '12 at 16:54
  • That might help, make sure you have the meta in your html header (haml in my case): %meta{'http-equiv' => 'content-type', 'content' => 'text/html; charset=utf-8'} That way the browser knows that this is utf-8. – Nicolas Guillaume Apr 14 '12 at 22:48
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.

Jai Chauhan
  • 4,035
  • 3
  • 36
  • 62
noodl
  • 17,143
  • 3
  • 57
  • 55
  • Oups sry about the exemple. I just took a bad one with no UTF-8 char :p Whatever... i do have config.encoding = "utf-8" in my application.rb and Encoding.default_external = Encoding::UTF_8 in my environment.rb. Thanks :) – Nicolas Guillaume Nov 15 '10 at 21:12
  • It can be very hard to determine where the incompatible data is coming from.. I'd try bisecting your templates in terms of data source.. So try a template with only I18n stuff, another with only data from the database, same for your cache if you have one, etc. If you're using MySQL then try switching to the mysql2 driver and make sure that config/database.yml specifies encoding: utf-8 – noodl Nov 15 '10 at 21:28
  • Thanks :) Ok so, I checked and can get and display i18n strings from the MySQL DB. But i can't get strings from the i18n file and so far, I don't use cache. – Nicolas Guillaume Nov 16 '10 at 12:10
0

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

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

Fábio Batista
  • 25,002
  • 3
  • 56
  • 68