0

I have this model:

class Coupon
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :id

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end
end

How can I add i18n translations to this model? Specifically:

  • How do I translate its model name?
  • How do I translate its attributes?
  • How do I provide custom translation for its submit button?

I am using SimpleForm.

Nerian
  • 15,901
  • 13
  • 66
  • 96

1 Answers1

2

In your config/locales folder, create an activerecord.nl.yml (e.g. for Dutch), then in that file put the active record translations as follows

nl:
  activerecord: 
    models: 
      coupon: translation
      coupons: translation

As for the labels and buttons, create a simple-form.nl.yml file, containing

nl:
  buttons: &buttons
    submit: translation
    ...

and

  labels: &labels
    name: translation
    ...

at the end of that file, put

  simple_form:
    buttons:
      defaults:
        <<: *buttons
    labels:
      defaults:
        <<: *labels

by splitting up the list of labels and buttons from the simple form section, you can also use them elsewhere in your views

Danny
  • 5,945
  • 4
  • 32
  • 52
  • Nice trick, using variables. Nonetheless it doesn't seem to be working for me. Neither the attributes nor the model name is being translated. Here is the test code https://github.com/Nerian/i18n-test-app – Nerian Nov 24 '13 at 22:18
  • Also, notice that I want the custom submit translation just for one model, not for all. – Nerian Nov 24 '13 at 22:19
  • I don't see the simple_form code in your yml file? It is the simple form label part that takes care of the translation of the labels. For buttons, I normally use something like = f.button :submit, t('buttons.submit') – Danny Nov 25 '13 at 19:15
  • Mmm, I would have thought that specifying the translation for the model attributes will be enough for simple_form to figure out that it should default to those for the labels. It does work like that when I am using Mongoid. In Mongoid I can translate mongoid.attributes.book_keeping_entry.balance and Simpleform will use that for the label. – Nerian Nov 26 '13 at 15:52
  • Have a look at https://github.com/plataformatec/simple_form#i18n It also states that "In addition, Simple Form will fallback to default human_attribute_name from Rails when no other translation is found for labels." I think you're referring to this? I've never used that – Danny Nov 27 '13 at 07:21