Update rails i18n

This commit is contained in:
Rico Sta. Cruz 2015-04-16 20:41:43 +08:00
parent 9e06910aa0
commit e7c1204553
1 changed files with 230 additions and 53 deletions

View File

@ -5,61 +5,135 @@ layout: default
### Basics ### Basics
t('my.messages.hello') ```rb
t('my.messages.hello')
t(:hello, scope: 'my.messages') # same as 'my.messages.hello'
t(:hello, scope: [:my, :messages]) t(:hello, scope: 'my.messages')
t(:hello, scope: [:my, :messages])
t('my.messages.hello', default: "Hello") t('my.messages.hello', default: "Hello")
```
### Interpolation
```rb
t('hello', name: "John")
```
```yml
hello: "Hello %{name}"
```
### Lazy lookup
```yml
en:
books:
index:
title: "Título"
```
```rb
# from the 'books/index' view
t('.title')
```
### Plural ### Plural
I18n.backend.store_translations :en, inbox: { ```rb
t(:inbox, count: 1) #=> 'one message'
t(:inbox, count: 2) #=> '2 messages'
```
```yml
inbox:
one: 'one message', one: 'one message',
other: '%{count} messages' other: '%{count} messages'
} ```
I18n.translate :inbox, count: 1 # => 'one message'
I18n.translate :inbox, count: 2 # => '2 messages' ## Localizing
### Time ### Time
en: ```rb
date: l(Time.now)
formats: l(Time.now, format: :short)
default: "%Y-%m-%d" ```
short: "%b %d"
```yml
en:
time: time:
formats: formats:
default: "%a, %d %b %Y %H:%M:%S %z" default: "%a, %d %b %Y %H:%M:%S %z"
short: "%d %b %H:%M" short: "%d %b %H:%M"
```
I18n.l Time.now ### Date
I18n.l Time.now, format: :short
### ActiveRecord ```rb
l(Date.today)
```
```yml
en:
date:
formats:
default: "%Y-%m-%d"
short: "%b %d"
```
## ActiveRecord
### Attributes
```rb
User.model_name.human #=> "User"
Child.model_name.human(count: 2) #=> "Children"
User.human_attribute_for :name #=> "Name"
```
```yml
en:
activerecord: activerecord:
models:
user: "User"
child:
one: "Child"
other: "Children"
attributes: attributes:
user: user:
# activerecord.attributes.<model>.<field>
name: "Name" name: "Name"
email: "Email"
```
### Errors
```yml
activerecord:
errors: errors:
models: models:
venue: venue:
attributes: attributes:
name: name:
blank: "Please enter a name." blank: "Please enter a name."
```
t 'blank', scope: Possible scopes (in order):
activerecord.errors.models.[model_name].attributes.[attribute_name]
activerecord.errors.models.[model_name]
activerecord.errors.messages
errors.attributes.[attribute_name]
errors.messages
helpers.submit.[model]: ```yml
create: "Create a %{model}" activerecord.errors.models.[model_name].attributes.[attribute_name].[error]
update: "Update %{model}" activerecord.errors.models.[model_name].[error]
activerecord.errors.messages.[error]
errors.attributes.[attribute_name].[error]
errors.messages.[error]
```
Where `[error]` can be:
```yml
validates
confirmation - :confirmation confirmation - :confirmation
acceptance - :accepted acceptance - :accepted
presence - :blank presence - :blank
@ -69,9 +143,112 @@ layout: default
uniqueness - :taken uniqueness - :taken
format - :invalid format - :invalid
numericality - :not_a_number numericality - :not_a_number
```
activerecord.errors.template.header: ```rb
one: "1 error prohibited this %{model} from being saved" error_messages_for(...)
```
### Labels
```
helpers:
# helpers.label.<model>.<field>
label:
post:
body: "Your body text"
```
### Submit
```yml
helpers:
submit:
# helpers.submit.<action>
create: "Create a %{model}"
update: "Confirm changes to %{model}"
# helpers.submit.<model>.<action>
article:
create: "Publish article"
update: "Update article"
```
## Numbers
```rb
number_to_delimited(2000) #=> "2,000"
number_to_currency(12.3) #=> "$12.30"
number_to_percentage(0.3) #=> "30%"
number_to_rounded(3.14, precision: 0) #=> "3"
number_to_human(12_000) #=> "12 Thousand"
number_to_human_size(12345) #=> "12.3 kb"
```
### Delimited
```rb
number_to_delimited(n)
```
```yml
number:
format:
separator: '.'
delimiter: ','
precision: 3
significant: false
strip_insignificant_zeroes: false
```
### Currencies
```rb
number_to_currency(n)
```
```yml
number:
currency:
format:
format: "%u%n" # %u = unit, %n = number
unit: "$"
separator: '.'
delimiter: ','
precision: 3
# (see number.format)
```
### Percentage
```rb
number_to_percentage(n)
```
```yml
number:
percentage:
format:
format: "%n%"
# (see number.format)
```
## Programmatic access
```rb
I18n.backend.store_translations :en, ok: "Ok"
I18n.locale = :en
I18n.default_locale = :en
I18n.available_locales
I18n.translate :ok # aka, I18n.t
I18n.localize date # aka, I18n.l
```
## Reference
### Reference
* http://guides.rubyonrails.org/i18n.html * http://guides.rubyonrails.org/i18n.html
* http://rails-i18n.org/wiki
* https://github.com/svenfuchs/i18n