Topic: Translate user model name generated by Devise

Hi all,

I'm using devise for user management and I'm trying to translate the user model name without success.
I tried several approaches suggested in several sites:

implement a method in the model:

def human_name
    return Utilizador
end 

write the translation in the translation files:

devise.pt.yml:
activerecord:
    models:
      user: Utilizador

pt.yml:
activerecord:
    models:
      user: Utilizador

and nothing... sad

I want this translation because of the error message shown when an error occurs registering a user. Is says

Re: Translate user model name generated by Devise

Are you sure your pt.yml is loaded, it contains the "pt:" tag at the top (see code below), and you indeed use the :pt locale?

pt:
  activerecord:
    models:
      user: Utilizador

Please, note that model internationalization is not devise-specific, see http://guides.rubyonrails.org/i18n.html … ord-models

Re: Translate user model name generated by Devise

Thanks for the reply boomerang!
Answering your questions:

Boomerang wrote:

Are you sure your pt.yml is loaded

I'm not sure. Is there any way to debug this?

Boomerang wrote:

it contains the "pt:" tag at the top

Yes. I didn't write it in my previous post, but I do have it in the file. Here is the complete file:

pt:
  hello: "ola mundo"
  
  activerecord:
    models:
      user: Utilizador
    attributes:
      user:
        login: login      
Boomerang wrote:

and you indeed use the :pt locale?

Yes, using devise, I'm also using the devise.pt.yml file, which contains:

"pt":
  errors:
    messages:
    ...
    not_saved:
        one: "1 erro impediu %{resource} de ser gravado:"
        other: "%{count} erros impediram %{resource} de ser gravado:"
   
    activerecord:
        models:
            user: Utilizador
        attributes:
            user:
                login: login

and the [not_saved][other] message is being shown, so I'm using the "pt" locale.

Thank's for your help. Any clue about this?

Re: Translate user model name generated by Devise

Actually, the message being shown is "2 erros impediram user de ser gravado:" and I want "2 erros impediram Utilizador de ser gravado:"

Re: Translate user model name generated by Devise

There are a few ways to debug if the translation file is loaded.

Step one: make sure something positively pulls it in.  That's usually a

config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]

line in your application.rb file (commented out by default, remove the comment mark) and make sure your pt.yml is in /my/locales.  N.B. I tend to spread my I18n files over many dirs (about one per controller='view group') and put them under config/locales so I use

config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]

Step two: restart your server (remember, this is an app conf, only gets loaded when the server (re)starts).

Step three: open a console ('rails c') and do something like this:

...>p I18n('hello', :locale=>:pt)
...>p I18n('activerecord.models.user', :locale=>:pt)

If it displays 'ola mundo' and 'Utilizador' you're all set.  If not, let me know how far you've got.

Re: Translate user model name generated by Devise

I tried to do all you said:

After configuring my locales I tried a different approach:

I put

<%=t :hello %>

in my index page and it worked, it showed the pt translation. With my locales and with the official location folder only.

Even so, everything seems to be translated but the user model name.

I tried to execute the commands you wrote in the rails console and got

irb(main):001:0> p I18n('hello', :locale=>:pt)
NoMethodError: undefined method `I18n' for main:Object
        from (irb):1
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.6/lib/rails/commands/console.rb:44:in `start'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.6/lib/rails/commands/console.rb:8:in `start'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.6/lib/rails/commands.rb:23:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
irb(main):002:0> I18n('hello', :locale=>:pt)
NoMethodError: undefined method `I18n' for main:Object
        from (irb):2
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.6/lib/rails/commands/console.rb:44:in `start'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.6/lib/rails/commands/console.rb:8:in `start'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.6/lib/rails/commands.rb:23:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'

still no success sad

Re: Translate user model name generated by Devise

Sorry, there is a typo in my suggestion, it should have been:

...>p I18n.t('hello', :locale=>:pt)
...>p I18n.t('activerecord.models.user', :locale=>:pt)

But, more importantly I copied the code from your devise.pt.yml you posted, and realized that it is mis-indented, which 'kills' YAML. Unindent the 'activerecord:' line and everything (hierarchically) below it by one tab stop and I think you're all set.

Re: Translate user model name generated by Devise

You were right, the code was mis-indented (active record was under errors), but I fixed (put it under pt) it and it keeps failing the translation sad

Re: Translate user model name generated by Devise

And the rails console test was successful

Re: Translate user model name generated by Devise

There's only one more thing I can possibly think of: do you by any chance include two files that define activerecord.models.user for :pt?  (It is possible that the console and phusion loads them in different order so what works in one doesn't in the other.)  I can't think of any other way to check this except manually... grep -r activerecord * in locales and check the listed ones by hand...

Last edited by boomerang (2011-07-05 06:02:01)

Re: Translate user model name generated by Devise

I did have 2 files including the activerecord definition. I removed one and still no success.
This last post of yours made me remember another thing maybe I should have said before. I'm using windows.

I'll try to create a new simple project only with devise to test this.
Thanks for your effort anyway.

In any case I really appreciate if you remember any other possible cause for this problem.

thanks