OSDN Git Service

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