Skip to content
michaek edited this page Jun 15, 2012 · 5 revisions

The default initializer says "Try to avoid setting labels for models and attributes, use ActiveRecord I18n API instead." That's good advice! But what does it mean?

To start with, there's the documentation from Rails itself: http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models

Their example config will cause the user login field to display with "Handle" as its label:

en:
  activerecord:
    models:
      user: Dude
    attributes:
      user:
        login: "Handle"

Awesome!

But what about something like the help text? It would be cool to have something as robust as human_attribute_name to fetch the help text for the field, but something simpler could work as well.

In your initializer (rails_admin.rb), you could place:

config.models do
  edit do
    fields do
      help do
        model = self.abstract_model.model_name.underscore
        field = self.name
        model_lookup = "admin.help.#{model}.#{field}".to_sym
        field_lookup = "admin.help.#{field}".to_sym
        I18n.t model_lookup, :help => help, :default => [field_lookup, help]
      end
    end
  end
end

That will add a configuration option to all your model to use the value from your localization, with the default help text as a default (and passed to the translation, for good measure) so you can do something like:

en:
  admin:
    help:
      user:
        login: "Enter your handle, please. %{help}"

Or if you wanted to set the message for the field across all models:

en:
  admin:
    help:
      login: "Enter your handle, please. %{help}"

Enjoy!

Clone this wiki locally