I18n.t [:odd, :even], :scope => 'active_record.error_messages' # => ["must be odd", "must be even"]
From 8ad2f93a309e57e67286e0c4426b1ad003b9b33b Mon Sep 17 00:00:00 2001
From: Pratik Naik
-
As part of this solution, every static string in the Rails framework — eg. ActiveRecord validation messages, time and date formats — has been internationalized, so localization of a Rails application means "over-riding" these defaults.
As part of this solution, every static string in the Rails framework — eg. Active Record validation messages, time and date formats — has been internationalized, so localization of a Rails application means "over-riding" these defaults.
Thus, the Ruby I18n gem is split into two parts:
en: hello: "Hello world"
This means, that in the :en locale, the key hello will map to Hello world string. Every string inside Rails is internationalized in this way, see for instance ActiveRecord validation messages in the activerecord/lib/active_record/locale/en.yml file or time and date formats in the activesupport/lib/active_support/locale/en.yml file. You can use YAML or standard Ruby Hashes to store translations in the default (Simple) backend.
This means, that in the :en locale, the key hello will map to Hello world string. Every string inside Rails is internationalized in this way, see for instance Active Record validation messages in the activerecord/lib/active_record/locale/en.yml file or time and date formats in the activesupport/lib/active_support/locale/en.yml file. You can use YAML or standard Ruby Hashes to store translations in the default (Simple) backend.
The I18n library will use English as a default locale, ie. if you don’t set a different locale, :en will be used for looking up translations.
The translations load path (I18n.load_path) is just a Ruby Array of paths to your translation files that will be loaded automatically and available in your application. You can pick whatever directory and translation file naming scheme makes sense for you.
I18n.t :invalid, :scope => [:active_record, :error_messages]
This looks up the :invalid message in the ActiveRecord error messages.
This looks up the :invalid message in the Active Record error messages.
Additionally, both the key and scopes can be specified as dot separated keys as in:
I18n.t [:odd, :even], :scope => 'active_record.error_messages' # => ["must be odd", "must be even"]
Also, a key can translate to a (potentially nested) hash as grouped translations. E.g. one can receive all ActiveRecord error messages as a Hash with:
Also, a key can translate to a (potentially nested) hash as grouped translations. E.g. one can receive all Active Record error messages as a Hash with:
Then User.human_name will return "Dude" and User.human_attribute_name(:login) will return "Handle".
ActiveRecord validation error messages can also be translated easily. ActiveRecord gives you a couple of namespaces where you can place your message translations in order to provide different messages and translation for certain models, attributes and/or validations. It also transparently takes single table inheritance into account.
Active Record validation error messages can also be translated easily. Active Record gives you a couple of namespaces where you can place your message translations in order to provide different messages and translation for certain models, attributes and/or validations. It also transparently takes single table inheritance into account.
This gives you quite powerful means to flexibly adjust your messages to your application’s needs.
Consider a User model with a validates_presence_of validation for the name attribute like this:
class User < ActiveRecord::Base validates_presence_of :name end
The key for the error message in this case is :blank. ActiveRecord will lookup this key in the namespaces:
The key for the error message in this case is :blank. Active Record will lookup this key in the namespaces:
class Admin < User validates_presence_of :name end
Then ActiveRecord will look for messages in this order:
Then Active Record will look for messages in this order: