From 61c64dc942a1a5fc142aab09f94f51bff0fee962 Mon Sep 17 00:00:00 2001
From: AvnerCohen <%= flash[:notice] %><%=t :hello_world %>
<%= l Time.now, format: :short %>
``` And in our pirate translations file let's add a time format (it's already there in Rails' defaults for English): @@ -495,7 +495,7 @@ I18n.t 'message' The `translate` method also takes a `:scope` option which can contain one or more additional keys that will be used to specify a “namespace” or scope for a translation key: ```ruby -I18n.t :record_invalid, :scope => [:activerecord, :errors, :messages] +I18n.t :record_invalid, scope: [:activerecord, :errors, :messages] ``` This looks up the `:record_invalid` message in the Active Record error messages. @@ -510,9 +510,9 @@ Thus the following calls are equivalent: ```ruby I18n.t 'activerecord.errors.messages.record_invalid' -I18n.t 'errors.messages.record_invalid', :scope => :active_record -I18n.t :record_invalid, :scope => 'activerecord.errors.messages' -I18n.t :record_invalid, :scope => [:activerecord, :errors, :messages] +I18n.t 'errors.messages.record_invalid', scope: :active_record +I18n.t :record_invalid, scope: 'activerecord.errors.messages' +I18n.t :record_invalid, scope: [:activerecord, :errors, :messages] ``` #### Defaults @@ -520,7 +520,7 @@ I18n.t :record_invalid, :scope => [:activerecord, :errors, :messages] When a `:default` option is given, its value will be returned if the translation is missing: ```ruby -I18n.t :missing, :default => 'Not here' +I18n.t :missing, default: 'Not here' # => 'Not here' ``` @@ -529,7 +529,7 @@ If the `:default` value is a Symbol, it will be used as a key and translated. On E.g., the following first tries to translate the key `:missing` and then the key `:also_missing.` As both do not yield a result, the string "Not here" will be returned: ```ruby -I18n.t :missing, :default => [:also_missing, 'Not here'] +I18n.t :missing, default: [:also_missing, 'Not here'] # => 'Not here' ``` @@ -538,7 +538,7 @@ I18n.t :missing, :default => [:also_missing, 'Not here'] To look up multiple translations at once, an array of keys can be passed: ```ruby -I18n.t [:odd, :even], :scope => 'errors.messages' +I18n.t [:odd, :even], scope: 'errors.messages' # => ["must be odd", "must be even"] ``` @@ -546,7 +546,7 @@ Also, a key can translate to a (potentially nested) hash of grouped translations ```ruby I18n.t 'activerecord.errors.messages' -# => { :inclusion => "is not included in the list", :exclusion => ... } +# => { inclusion: "is not included in the list", exclusion: ... } ``` #### "Lazy" Lookup @@ -573,8 +573,8 @@ In many cases you want to abstract your translations so that **variables can be All options besides `:default` and `:scope` that are passed to `#translate` will be interpolated to the translation: ```ruby -I18n.backend.store_translations :en, :thanks => 'Thanks %{name}!' -I18n.translate :thanks, :name => 'Jeremy' +I18n.backend.store_translations :en, thanks: 'Thanks %{name}!' +I18n.translate :thanks, name: 'Jeremy' # => 'Thanks Jeremy!' ``` @@ -587,14 +587,14 @@ In English there are only one singular and one plural form for a given string, e The `:count` interpolation variable has a special role in that it both is interpolated to the translation and used to pick a pluralization from the translations according to the pluralization rules defined by CLDR: ```ruby -I18n.backend.store_translations :en, :inbox => { - :one => 'one message', - :other => '%{count} messages' +I18n.backend.store_translations :en, inbox: { + one: 'one message', + other: '%{count} messages' } -I18n.translate :inbox, :count => 2 +I18n.translate :inbox, count: 2 # => '2 messages' -I18n.translate :inbox, :count => 1 +I18n.translate :inbox, count: 1 # => 'one message' ``` @@ -623,8 +623,8 @@ I18n.l Time.now Explicitly passing a locale: ```ruby -I18n.t :foo, :locale => :de -I18n.l Time.now, :locale => :de +I18n.t :foo, locale: :de +I18n.l Time.now, locale: :de ``` The `I18n.locale` defaults to `I18n.default_locale` which defaults to :`en`. The default locale can be set like this: @@ -665,9 +665,9 @@ For example a Ruby Hash providing translations can look like this: ```ruby { - :pt => { - :foo => { - :bar => "baz" + pt: { + foo: { + bar: "baz" } } } @@ -698,9 +698,9 @@ So, all of the following equivalent lookups will return the `:short` date format ```ruby I18n.t 'date.formats.short' -I18n.t 'formats.short', :scope => :date -I18n.t :short, :scope => 'date.formats' -I18n.t :short, :scope => [:date, :formats] +I18n.t 'formats.short', scope: :date +I18n.t :short, scope: 'date.formats' +I18n.t :short, scope: [:date, :formats] ``` Generally we recommend using YAML as a format for storing translations. There are cases, though, where you want to store Ruby lambdas as part of your locale data, e.g. for special date formats. @@ -734,7 +734,7 @@ Consider a User model with a validation for the name attribute like this: ```ruby class User < ActiveRecord::Base - validates :name, :presence => true + validates :name, presence: true end ``` @@ -764,7 +764,7 @@ For example, you might have an Admin model inheriting from User: ```ruby class Admin < User - validates :name, :presence => true + validates :name, presence: true end ``` @@ -930,7 +930,7 @@ Another example where the default behaviour is less desirable is the Rails Trans To do so, the helper forces `I18n#translate` to raise exceptions no matter what exception handler is defined by setting the `:raise` option: ```ruby -I18n.t :foo, :raise => true # always re-raises exceptions from the backend +I18n.t :foo, raise: true # always re-raises exceptions from the backend ``` Conclusion -- cgit v1.2.3