Rewrite humanize

I have an application with all tables and field names in English, but the whole views now need to be in Portuguese.

Because I’m always using the humanize method, a simple solutions should emerge.

First solution:

class Ticket


    :category > "Categoria",

    :title > "Assunto"


  def self.human_attribute_name(attr)

    HUMANIZED_ATTRIBUTES[attr.to_sym] || super



Setting the human_attribue_name in each model works ok, but if like in my case, you have 20 models and all of them have a description, title,… much duplication…

So I thought in doing something similar to this in my enviroment.rb:

Inflector.inflections do |inflect|

  inflect.plural /^(foo)$/i, '\1ze'

  inflect.singular /^(foo)ze/i, '\1'


But for the humanized method instead of pluralize. Yah It would make sense that way, but rails doesn’t provide that feature. You can go here to find a patch for getting that functionality into your rails, I hope they get that into the core… anyway If didn’t want to download the patch, how do I solve the problem?

Maybe a rubbish solution but perfect to get what I was needing, translate my views with few lines of code! What I’ve done? Just added those lines into my environment.rb

class String

  def humanize

    {:movie              => "Filme",

     :movies             => "Filmes",

     :name               => "Nome",

     :title              => "Título",

     :synopsis           => "Sinopse",

     :genre              => "Genero",

     :author             => "Autor",

     :authors            => "Autores"


    }[self.gsub(/_id$/, "").to_sym] || super



I simply redefined the humanize method for whole strings. It works perfectly in my case, because I’m always calling humanize in my views.

keep humanizing the world!


comments powered by Disqus