Matthias
Matthias

Reputation: 1953

Rails: How to do i18n on a dropdown menu in simple_form

I have a simple_form that has a dropdown menu with a collection of options. I wish to have the options saved as integers, but the text shown in the dropdown as a string based on the locale. As follows:

<option value="1">Option 1</option>
<option value="2">Option 2</option>

I currently have the options defined as class methods in MyModel, as follows:

def self.options
 [['Option 1', 1], ['Option 2', 2]]
end

Without i18n I had the following working:

f.input :dropdown, collection: MyModel::boolean, include_blank: false

Adding the locales, I tried

f.input :dropdown, collection: t(MyModel::boolean, scope: 'simple_form'), include_blank: false

But this raises an error:

translation missing: en.simple_form.Option 1.1

It looks as if it looks for both the array key and value in the translation.

Any suggestions how to make i18n work for the collection?

Upvotes: 0

Views: 868

Answers (1)

bkunzi01
bkunzi01

Reputation: 4561

Since simpleform collections accept procs you can call a block on the label_method that will format your string as needed which may be what you're looking for:

f.input :dropdown, collection: MyModel::boolean, include_blank: false, :label_method => lambda { |item| t(item.last) }

Upvotes: 1

Related Questions