November 14, 2013

Customize text in form submit buttons with i18n

Problem

In your Rails application you want to have custom text in your form submit buttons for both, create and also update actions and you still want to have just one form template with simple <%= f.submit %> call.

Solution

Use rails-i18n gem and create custom button text for all submit buttons, or just for particular model.

Step by step solution

1) Add this to your Gemfile:

gem 'rails-i18n'

2) Run bundle install.

3) Update your locale file with:

# config/locales/en.yml
en:
  helpers:
    submit:
      create: 'Awesome create button'
      update: 'Even better update button'

Or if you would like to modify it just for particular model, then your locale file should contain this:

en:
  helpers:
    submit:
      model_name:
        create: 'Awesome create button for %{model}'
        update: 'Even better update button %{model}'

4) Restart your rails server.

5) ???

6) Profit!

Hey there!

My name is Patrik Bóna and I am the only programmer at Memberful. This blog is kind of dead, but I just started my own Ruby on Rails screencast. Follow me on Twitter if you want to be notified about my newest videos.