Jerome
Jerome

Reputation: 6217

Rails3 using i18n strings in model file

In an application, strings need to be generated in a user's language and then passed onto another server/application. Thus the model can identify

  def user_language
    self.user.idioma.code.downcase
  end

but then generating a string based on that language and reading the locale's yaml file is the challenge:

  def description
    user_language.products.name
  end

I realize that the model has no idea to go and look-up a locale file. How can that be invoked?

Upvotes: 3

Views: 741

Answers (1)

Jerome
Jerome

Reputation: 6217

The answer is simpler (and as I expected, rather succinct)

  def description
    I18n.t('products.name', :locale => user_language)
  end

Note the capital 'I' in I18n. I stayed stuck on this for quite a while. Most references I've seen use 1st character lowercase => i18n, but that generates an

undefined local variable or method `i18n'

Upvotes: 5

Related Questions