Skip to content
vendethiel edited this page Dec 12, 2014 · 5 revisions

Using the ActiveModel I18n

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!

Other I18n Text

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}"

It's worth noting that because this method configures all the fields, you might want to put any custom model configuration before the global block or many things (like field visibility) will be configured with their defaults. Someone who knows more about the RailsAdmin internals than I do could probably clarify this!

Enjoy!

Clone this wiki locally