Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I18n for model-specific Rails submit button

I've found that Rails allows for generic i18n of submit buttons via the following in config/locales/en.yml:

en:
  helpers:
    submit:
      create: "Create %{model}"
      submit: "Save %{model}"
      update: "Update %{model}"

However, I'm looking to update the create value only for one specific model. I'd like the text to read as "Upload %{model}" or just "Upload". How can I make this change for just one model (e.g.: a Photo model)?

like image 935
Matt Huggins Avatar asked Nov 25 '12 00:11

Matt Huggins


2 Answers

Those labels can be customized using I18n under the +helpers.submit+ key 
and using %{model} for translation interpolation:

  en:
    helpers:
      submit:
        create: "Create a %{model}"
        update: "Confirm changes to %{model}"

It also searches for a key specific to the given object:

  en:
    helpers:
      submit:
        post:
          create: "Add %{model}"

Source @ actionview/lib/action_view/helpers/form_helper.rb

like image 167
Chris Salzberg Avatar answered Oct 23 '22 13:10

Chris Salzberg


If you use the i18n-debug gem, the rails server will print translations look-up attempts to the console, like:

[i18n-debug] en.helpers.submit.post.create => nil
like image 22
Jason Avatar answered Oct 23 '22 12:10

Jason