foliwe83
foliwe83

Reputation: 590

Translating select option in rails I18n

How can I translate the below code in rails I18n

f.select :option,[["This is a Scam","Scam"],["This is a Spam","Spam"],["This is a bot","Bot"]]

Upvotes: 1

Views: 2724

Answers (1)

cnnr
cnnr

Reputation: 1307

Try this:

config/locales/es.yml

es:
  helpers:
    model:
      select_attr:
        values:
          scam: 'estafa'
          spam: 'correo no deseado'
          bot: 'larva del moscardón'

config/locales/en.yml

en:
  helpers:
    model:
      select_attr:
        values:
          scam: 'Scam'
          spam: 'Spam'
          bot: 'Bot'

And modify your select helper to

= f.select :option, [
                     [I18n.t("helpers.model.select_attr.values.scam", 'Scam')], 
                     [I18n.t("helpers.model.select_attr.values.spam", 'Spam')], 
                     [I18n.t("helpers.model.select_attr.values.bot", 'Bot')]
                    ]

Rails set options values according to your selected locale (en or es) as in the example above.

First attribute of the option's inner array is a label, which will be show to user. Second - is an option value, which be saved to database field option.

More info you can read at ActionView::Helpers::FormOptionsHelper docs page.

Upvotes: 2

Related Questions