Michał Górski
Michał Górski

Reputation: 111

What's the way to translate model attributes in rails with mongoid?

I've problem with mongoid and model translations. When I'm trying use mongoDB on my model I haven't idea to translate attributes and model name. It's normally in *.yml files but in this time this doesn't work. Any ideas?

Upvotes: 11

Views: 4007

Answers (4)

adrianthedev
adrianthedev

Reputation: 646

If you use engines you have o namespace the model

en:
  activerecord:
    models:
      'my_engine/mymodel':
        one: TranslatedMyModel
        other: TranslatedMyModels
    attributes:
      'my_engine/mymodel':
        myattribute: translated attribute

or if you use mongoid

en:
  mongoid:
    models:
      'my_engine/mymodel':
        one: TranslatedMyModel
        other: TranslatedMyModels
    attributes:
      'my_engine/mymodel':
        myattribute: translated attribute

I solved it using comments from this issue.

Upvotes: 0

Pavel Maksimenko
Pavel Maksimenko

Reputation: 171

Variant with "activemodel" does not work for me. But.

This variant worked for me:

 en:
  mongoid:
    errors:
      models:
        user:
          attributes:
            email:
              blank: "You have to give me your e-mail address"
              not_found: "e-mail address not found in list of members"
              #...
    attributes:
      user:
        email: "Email address"
        name: "Your nickname"
        #...

From here

Upvotes: 17

Abhi
Abhi

Reputation: 3611

Use like this:

mongoid:
  attributes:
    article:
      title: "Article title"

Check this one: https://gist.github.com/lurkermike/1596505

Upvotes: 0

Leonardo Cardoso
Leonardo Cardoso

Reputation: 121

Try this in the yml file (config/locales/pt-BR.yml in my case):

 activemodel:
    attributes:
      [model_name]:
        [attribute1]: "[translation1]"
        [attribute2]: "[translation2]"
        [attribute3]: "[translation3]"

Worked for me, using mongoid 2.0.0.beta.17 and rails 3.0.0

Upvotes: 7

Related Questions