Firebird on Rails encoding accent right

So today, I had a great achivment ! (A personal happyness 😛 )

The problem:

I HAVE to use a Rails App with Firebird DB 1.5 encoding: NONE and it has accents !

The deal : Portuguese is the language we Speak in Brazil ( Brasil), it has a lot of accents (acentos) so to get the right encode on the web as most programers are aware of, we gotta use charset= ISO-8859-1 or UTF-8 and as long as my tables is shared with Delphi App that are using encoding: none, as the newer app programmer I have to adapt 😛

During research I found this guy: http://www.botvector.net/2007/11/encoding-problems.html, and his job seems great.. but I not looking fwd to translate the DB, still, I will use his Ruby function

Solution: set a LIB file like this:

lib/conversor.rb:

  def iconvert(str, encoding_from="iso-8859-1//ignore", encoding_to = "utf-8")
    i = Iconv.new encoding_to, encoding_from
    utf_str = ""
    begin
      utf_str < e
      utf_str << e.success
      ch, str = e.failed.split(//, 2)
      utf_str << "?"
      logger.info "FAIL !! char:#{ch}"
      retry
    end
    return utf_str
  end
  
  def ireverse(str)
    iconvert(str, "utf-8", "iso-8859-1//ignore" )
  end

At the App Controller:

class ApplicationController < ActionController::Base
  helper :all 
  require 'iconv'
  require 'conversor.rb'
...

So now everytime you have to display data from the database use: iconvert(String) !

To save the data define in your model for each String or Text field:

i.e.
 #view/uf/index.html.erb
  	 iconvert(uf.nome) 


# "nome" is the field i am retro converting
  def nome=(valor)
    write_attribute(:nome, ireverse(valor))
  end

I spent a lot of hours on this research.. I wish i knew how to create plugins..
*Maybe you have to download the Iconv class if your Ruby isn't new enough
**First page on google ! for "rails firebird encoding", "rails firebird acento" yay!

Advertisements

3 thoughts on “Firebird on Rails encoding accent right

  1. Pingback: Firebird News » Firebird on Rails encoding accent right

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s