aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source/i18n.md
diff options
context:
space:
mode:
authorAvnerCohen <israbirding@gmail.com>2012-10-11 16:38:31 +0200
committerAvnerCohen <israbirding@gmail.com>2012-10-11 16:38:31 +0200
commit61c64dc942a1a5fc142aab09f94f51bff0fee962 (patch)
treec3af67a92a5ea56c1e5d6851c34dd7629a5187ce /guides/source/i18n.md
parentf638ef951b4d185b2bfd3d0d8be241cb23001890 (diff)
downloadrails-61c64dc942a1a5fc142aab09f94f51bff0fee962.tar.gz
rails-61c64dc942a1a5fc142aab09f94f51bff0fee962.tar.bz2
rails-61c64dc942a1a5fc142aab09f94f51bff0fee962.zip
1.9 hash syntax for guides, work-in-progress
Diffstat (limited to 'guides/source/i18n.md')
-rw-r--r--guides/source/i18n.md66
1 files changed, 33 insertions, 33 deletions
diff --git a/guides/source/i18n.md b/guides/source/i18n.md
index eda1881e73..e916bda630 100644
--- a/guides/source/i18n.md
+++ b/guides/source/i18n.md
@@ -205,7 +205,7 @@ The most usual way of setting (and passing) the locale would be to include it in
This approach has almost the same set of advantages as setting the locale from the domain name: namely that it's RESTful and in accord with the rest of the World Wide Web. It does require a little bit more work to implement, though.
-Getting the locale from `params` and setting it accordingly is not hard; including it in every URL and thus **passing it through the requests** is. To include an explicit option in every URL (e.g. `link_to( books_url(:locale => I18n.locale))`) would be tedious and probably impossible, of course.
+Getting the locale from `params` and setting it accordingly is not hard; including it in every URL and thus **passing it through the requests** is. To include an explicit option in every URL (e.g. `link_to( books_url(locale: I18n.locale))`) would be tedious and probably impossible, of course.
Rails contains infrastructure for "centralizing dynamic decisions about the URLs" in its [`ApplicationController#default_url_options`](http://api.rubyonrails.org/classes/ActionController/Base.html#M000515, which is useful precisely in this scenario: it enables us to set "defaults" for [`url_for`](http://api.rubyonrails.org/classes/ActionController/Base.html#M000503) and helper methods dependent on it (by implementing/overriding this method).
@@ -215,7 +215,7 @@ We can include something like this in our `ApplicationController` then:
# app/controllers/application_controller.rb
def default_url_options(options={})
logger.debug "default_url_options is passed options: #{options.inspect}\n"
- { :locale => I18n.locale }
+ { locale: I18n.locale }
end
```
@@ -238,14 +238,14 @@ If you don't want to force the use of a locale in your routes you can use an opt
```ruby
# config/routes.rb
-scope "(:locale)", :locale => /en|nl/ do
+scope "(:locale)", locale: /en|nl/ do
resources :books
end
```
With this approach you will not get a `Routing Error` when accessing your resources such as `http://localhost:3001/books` without a locale. This is useful for when you want to use the default locale when one is not specified.
-Of course, you need to take special care of the root URL (usually "homepage" or "dashboard") of your application. An URL like `http://localhost:3001/nl` will not work automatically, because the `root :to => "books#index"` declaration in your `routes.rb` doesn't take locale into account. (And rightly so: there's only one "root" URL.)
+Of course, you need to take special care of the root URL (usually "homepage" or "dashboard") of your application. An URL like `http://localhost:3001/nl` will not work automatically, because the `root to: "books#index"` declaration in your `routes.rb` doesn't take locale into account. (And rightly so: there's only one "root" URL.)
You would probably need to map URLs like these:
@@ -303,7 +303,7 @@ You most probably have something like this in one of your applications:
```ruby
# config/routes.rb
Yourapp::Application.routes.draw do
- root :to => "home#index"
+ root to: "home#index"
end
```
@@ -381,7 +381,7 @@ You can use variables in the translation messages and pass their values from the
```erb
# app/views/home/index.html.erb
-<%=t 'greet_username', :user => "Bill", :message => "Goodbye" %>
+<%=t 'greet_username', user: "Bill", message: "Goodbye" %>
```
```yaml
@@ -398,7 +398,7 @@ OK! Now let's add a timestamp to the view, so we can demo the **date/time locali
# app/views/home/index.html.erb
<h1><%=t :hello_world %></h1>
<p><%= flash[:notice] %></p
-<p><%= l Time.now, :format => :short %></p>
+<p><%= l Time.now, format: :short %></p>
```
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