OSDN Git Service

Regular updates
[twpd/master.git] / rails-i18n.md
1 ---
2 title: i18n
3 category: Rails
4 ---
5
6 ```rb
7 t('my.messages.hello')
8
9 # same as 'my.messages.hello'
10 t(:hello, scope: 'my.messages')
11 t(:hello, scope: [:my, :messages])
12
13 t('my.messages.hello', default: "Hello")
14 ```
15 {:.light}
16
17 ```yml
18 en:
19   my:
20     messages:
21       hello: "Hello"
22 ```
23
24 ### Interpolation
25
26 ```rb
27 t('hello', name: "John")
28 ```
29 {:.light}
30
31 ```yml
32 hello: "Hello %{name}"
33 ```
34
35 ### Lazy lookup
36
37 ```rb
38 # from the 'books/index' view
39 t('.title')
40 ```
41 {:.light}
42
43 ```yml
44 en:
45   books:
46     index:
47       title: "Título"
48 ```
49
50 ### Plural
51
52 ```rb
53 t(:inbox, count: 1)  #=> 'one message'
54 t(:inbox, count: 2)  #=> '2 messages'
55 ```
56 {:.light}
57
58 ```yml
59 inbox:
60   one: 'one message',
61   other: '%{count} messages'
62 ```
63
64 ## Localizing
65
66 ### Time
67
68 ```rb
69 l(Time.now)
70 l(Time.now, format: :short)
71 ```
72 {:.light}
73
74 ```yml
75 en:
76   time:
77     formats:
78       default: "%a, %d %b %Y %H:%M:%S %z"
79       long: "%B %d, %Y %H:%M"
80       short: "%d %b %H:%M"
81 ```
82
83 ### Date
84
85 ```rb
86 l(Date.today)
87 ```
88 {:.light}
89
90 ```yml
91 en:
92   date:
93     formats:
94       default: "%Y-%m-%d" # 2015-06-25
95       long: "%B %d, %Y"   # June 25, 2015
96       short: "%b %d"      # Jun 25
97 ```
98
99 ## ActiveRecord
100
101 ### Model names
102
103 ```rb
104 User.model_name.human            #=> "User"
105 Child.model_name.human(count: 2) #=> "Children"
106 ```
107 {:.light}
108
109 ```yml
110 en:
111   activerecord:
112     models:
113       user: "User"
114       child:
115         one: "Child"
116         other: "Children"
117 ```
118
119 ### Attributes
120
121 ```rb
122 User.human_attribute_for :name   #=> "Name"
123 ```
124 {:.light}
125
126 ```yml
127 en:
128   activerecord:
129     attributes:
130       user:
131         # activerecord.attributes.<model>.<field>
132         name: "Name"
133         email: "Email"
134 ```
135
136 ### Error messages
137
138 ```rb
139 error_messages_for(...)
140 ```
141 {:.light}
142
143 ```yml
144 activerecord:
145   errors:
146     models:
147       venue:
148         attributes:
149           name:
150             blank: "Please enter a name."
151 ```
152
153 Possible scopes (in order):
154
155 ```yml
156 activerecord.errors.models.[model_name].attributes.[attribute_name].[error]
157 activerecord.errors.models.[model_name].[error]
158 activerecord.errors.messages.[error]
159 errors.attributes.[attribute_name].[error]
160 errors.messages.[error]
161 ```
162
163 Where `[error]` can be:
164
165 ```yml
166 validates
167   confirmation - :confirmation
168   acceptance   - :accepted
169   presence     - :blank
170   length       - :too_short (%{count})
171   length       - :too_long (%{count})
172   length       - :wrong_length (%{count})
173   uniqueness   - :taken
174   format       - :invalid
175   numericality - :not_a_number
176 ```
177
178 ### Form labels
179
180 ```rb
181 form_for @post do
182   f.label :body
183 ```
184 {:.light}
185
186 ```yml
187 helpers:
188   # helpers.label.<model>.<field>
189   label:
190     post:
191       body: "Your body text"
192 ```
193
194 ### Submit buttons
195
196 ```rb
197 form_for @post do
198   f.submit
199 ```
200 {:.light}
201
202 ```yml
203 helpers:
204   submit:
205     # helpers.submit.<action>
206     create: "Create a %{model}"
207     update: "Confirm changes to %{model}"
208
209     # helpers.submit.<model>.<action>
210     article:
211       create: "Publish article"
212       update: "Update article"
213 ```
214  
215
216 ## Numbers
217
218 ```rb
219 number_to_delimited(2000)             #=> "2,000"
220 number_to_currency(12.3)              #=> "$12.30"
221 number_to_percentage(0.3)             #=> "30%"
222 number_to_rounded(3.14, precision: 0) #=> "3"
223 number_to_human(12_000)               #=> "12 Thousand"
224 number_to_human_size(12345)           #=> "12.3 kb"
225 ```
226 {:.light}
227
228 ### Delimited
229
230 ```rb
231 number_to_delimited(n)
232 ```
233 {:.light}
234
235 ```yml
236 number:
237   format:
238     separator: '.'
239     delimiter: ','
240     precision: 3
241     significant: false
242     strip_insignificant_zeroes: false
243 ```
244
245 ### Currencies
246
247 ```rb
248 number_to_currency(n)
249 ```
250 {:.light}
251
252 ```yml
253 number:
254   currency:
255     format:
256       format: "%u%n" # %u = unit, %n = number
257       unit: "$"
258       separator: '.'
259       delimiter: ','
260       precision: 3
261       # (see number.format)
262 ```
263
264 ### Percentage
265
266 ```rb
267 number_to_percentage(n)
268 ```
269 {:.light}
270
271 ```yml
272 number:
273   percentage:
274     format:
275       format: "%n%"
276       # (see number.format)
277 ```
278
279 ## Programmatic access
280
281 ```rb
282 I18n.backend.store_translations :en, ok: "Ok"
283 I18n.locale = :en
284 I18n.default_locale = :en
285
286 I18n.available_locales
287
288 I18n.translate :ok   # aka, I18n.t
289 I18n.localize date   # aka, I18n.l
290 ```
291 {:.light}
292
293 ## Reference
294
295  * http://guides.rubyonrails.org/i18n.html
296  * http://rails-i18n.org/wiki
297  * https://github.com/svenfuchs/i18n
298  * https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml