-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslides_internationalization.html
More file actions
546 lines (520 loc) · 40.2 KB
/
slides_internationalization.html
File metadata and controls
546 lines (520 loc) · 40.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Slides for
Internationalization — Ruby on Rails Guides
</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" data-turbo-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print">
<link rel="stylesheet" type="text/css" href="stylesheets/highlight.css" data-turbo-track="reload">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="stylesheets/reset.css">
<link rel="stylesheet" href="stylesheets/reveal.css">
<link rel="stylesheet" href="stylesheets/myslide.css" id="theme">
<link rel="stylesheet" href="stylesheets/code.css">
<script src="javascripts/clipboard.js" data-turbo-track="reload"></script>
<script src="javascripts/slides.js" data-turbo-track="reload"></script>
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>Internationalization</h1><p>This guide will give you an overview
of the problems you face when writing an app
that's used in many countries, in many languages.
After reading it you should be familiar with:</p>
<ul>
<li>the concepts of Internationalization and Localisation</li>
<li>the Rails I18n gem</li>
<li>how Rails handles some of the most important topics
<ul>
<li>handling dates</li>
<li>handling numbers and plurals</li>
</ul></li>
</ul>
<p>You can use the <a href="https://gitlab.mediacube.at/bjelline/statement_shirts">Demo App</a> to try out the concepts step by step.</p>
<p><small>Slides - use arrow keys to navigate, esc to return to page view, f for fullscreen</small></p>
</section>
<section><a class='slide_break' href='internationalization.html#slide-0'>▻</a>
<h2 id="internationalization-and-localization"><a class="anchorlink" href="#internationalization-and-localization"><span>1</span> Internationalization and Localization</a></h2><p>Internationalization is the process of designing a software application so that it can be adapted to various languages and regions. It is often written as I18n (to avoid typing 18 letters).</p><p>Localization is the process of adapting internationalized software for a specific region or language by translating text and adding locale-specific components. It is often written as L10n (to avoid typing 10 letters).</p><p>Localization (which is potentially performed multiple times, for different locales) uses the infrastructure or flexibility provided by internationalization (which is ideally performed only once before localization, or as an integral part of ongoing development).</p></section>
<section><a class='slide_break' href='internationalization.html#slide-1'>▻</a>
<h2 id="the-big-picture"><a class="anchorlink" href="#the-big-picture"><span>2</span> The big picture</a></h2><p>When developing web applications for different countries, cultures, languages
we are faced with many different problems:</p>
<ul>
<li>Different cultures will have very different ideas of what a "good" website looks like. See <a href="https://www.youtube.com/watch?v=ER3534JJucc">Jenny Shen's talk at concat</a></li>
<li>Different writing systems will use a diffrent amount of space, leading to diffrent layouts</li>
<li>Writing direction (left-to-right, right-to-left or top-down) will need to be handled by HTML. See <a href="https://www.w3.org/International/questions/qa-html-dir">w3c</a></li>
<li>The app might need to handle different currencies and methods of payment</li>
</ul>
</section>
<section><a class='slide_break' href='internationalization.html#slide-2'>▻</a>
<h2 id="focus-on-language"><a class="anchorlink" href="#focus-on-language"><span>3</span> Focus on Language</a></h2><p>Internationalization is mostly concerned with language and translation.
Even if we focus on indo-european languages we will find differences:</p>
<ul>
<li>Example: Plurals in Polish <a href="https://www.tiktok.com/@stach_mat/video/7325101589084130592">Polnisch für Anfänger by stach_mat, Teil 5</a></li>
</ul>
<p>Going from a mono-lingual to a multi-lingual app will
add another layer of abstraction that will make both programming
and testing harder.</p></section>
<section><a class='slide_break' href='internationalization.html#slide-3'>▻</a>
<h2 id="i18n-in-rails"><a class="anchorlink" href="#i18n-in-rails"><span>4</span> I18n in Rails</a></h2><p>Rails Apps are ready for Internationalization. To get started,
find the file <code>config/locale/en.yml</code>. It only contains one example
of a translation string:</p><div class="interstitial code">
<pre><code class="highlight yml"><span class="na">en</span><span class="pi">:</span>
<span class="na">hello</span><span class="pi">:</span> <span class="s2">"</span><span class="s">Hello</span><span class="nv"> </span><span class="s">world"</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="en:
hello: "Hello world"
">Copy</button>
</div>
<p>This means, that in the <code>:en</code> locale, the key <code>hello</code> will map to the <code>Hello world</code> string</p><p>The <a href="https://gitlab.mediacube.at/bjelline/statement_shirts">Demo App</a> is already
prepared to switch between locales through folders:</p>
<ul>
<li><a href="http://localhost:3000/">http://localhost:3000/</a> - Home page and order page - english</li>
<li><a href="http://localhost:3000/de/">http://localhost:3000/de/</a> - Home page and order page - german</li>
<li><a href="http://localhost:3000/dk/">http://localhost:3000/dk/</a> - Home page and order page - danish</li>
<li><a href="http://localhost:3000/es/">http://localhost:3000/es/</a> - Home page and order page - spanish</li>
<li><a href="http://localhost:3000/shirts">http://localhost:3000/shirts</a> - List of all shirts - english</li>
<li><a href="http://localhost:3000/de/shirts">http://localhost:3000/de/shirts</a> - List of all shirts - english</li>
<li><a href="http://localhost:3000/dk/shirts">http://localhost:3000/dk/shirts</a> - List of all shirts - danish</li>
<li><a href="http://localhost:3000/en/shirts">http://localhost:3000/en/shirts</a> - List of all shirts - spanish</li>
</ul>
<p>and so on.</p><p>Inside the app you can always read the current language from <code>I18n.locale</code>.</p></section>
<section><a class='slide_break' href='internationalization.html#slide-4'>▻</a>
<h2 id="keys-and-translations"><a class="anchorlink" href="#keys-and-translations"><span>5</span> keys and translations</a></h2><p>The method <code>I18n.translate</code> or <code>I18n.t</code> can be used to look up translation.
In our example before the key was <code>hello</code>. In english <code>t 'hello'</code> will return "Hello World"
and in German it would be "Hallo Welt".</p><p>In the view we can use the even short form:</p><div class="interstitial code">
<pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">t</span> <span class="s1">'hello'</span> <span class="cp">%></span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="<%= t 'hello' %>
">Copy</button>
</div>
<p>If no translation can be found - because there is no yaml file or because
the key is missing, then a span with class <code>translation_missing</code> will be wrapped
around the text. The key itself will be used in place of a translation:</p><div class="interstitial code">
<pre><code class="highlight html"><span class="nt"><span</span> <span class="na">class=</span><span class="s">"translation_missing"</span> <span class="na">title=</span><span class="s">"translation missing: dk.hello"</span><span class="nt">></span>Hello<span class="nt"></span></span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="<span class="translation_missing" title="translation missing: dk.hello">Hello</span>
">Copy</button>
</div>
<p>Texts in views all need to be replaced by translation keys.</p></section>
<section><a class='slide_break' href='internationalization.html#slide-5'>▻</a>
<h2 id="locale-files-included-with-the-i18n-gem"><a class="anchorlink" href="#locale-files-included-with-the-i18n-gem"><span>6</span> locale files included with the I18n gem</a></h2><p>The I18n gem comes with translations for many languages in the <a href="https://github.com/svenfuchs/rails-i18n/tree/10141c451f03d7c6b78cfdcce808c389da6b9ddd/rails/locale">locale folder</a>.</p><p>You can copy the ones you need to your own apps <code>config/locale</code> folder
to use them.</p><p>If you are following along with the demo app, do this now for english, german, danish and spanish!</p></section>
<section><a class='slide_break' href='internationalization.html#slide-6'>▻</a>
<h2 id="numbers"><a class="anchorlink" href="#numbers"><span>7</span> Numbers</a></h2><p>There are different Rules for displaying numbers in different languages.
You may be familiar with the European Million being translated into an US Billion.
So different words are used for numbers.</p><p>But also the numbers themselves are formatted differently:</p><div class="interstitial code">
<pre><code class="highlight erb"><span class="nt"><p></span><span class="cp"><%=</span> <span class="n">number_with_delimiter</span><span class="p">(</span><span class="mi">100000000</span><span class="p">)</span> <span class="cp">%></span><span class="nt"></p></span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="<p><%= number_with_delimiter(100000000) %></p>
">Copy</button>
</div>
<p>In many languages this will be displayed as <code>100.000.000</code> or <code>100,000,000</code>.
Some examples of languages with different number formats are:</p>
<ul>
<li><a href="https://en.wikipedia.org/wiki/Indian_numbering_system">Indian Numbering System:</a> 10,00,00,000 or 10 lakh.</li>
<li><a href="https://en.wikipedia.org/wiki/Japanese_numerals#Powers_of_10">Japanese numerals</a> 1 0000 0000 or 1億.</li>
</ul>
<p>Rails provides several helper methods for constructing numbers:</p>
<ul>
<li><code>number_with_delimiter</code> - for large numbers like <code>100.000.000</code></li>
<li><code>number_to_currency</code> - includes the currency like <code>100.000.000,00 €</code></li>
<li><code>number_with_precision</code> - like <code>100000000,00</code></li>
<li><code>number_to_percentage</code> - like <code>90,00 %</code></li>
<li><code>number_to_human_size</code> - for bytes, like <code>95,4 MB</code></li>
</ul>
</section>
<section><a class='slide_break' href='internationalization.html#slide-7'>▻</a>
<h2 id="dates"><a class="anchorlink" href="#dates"><span>8</span> Dates</a></h2><p>Dates are a special case for translation: there are complex and different rules
for displaying dates in different languages.</p><p>The page <code>/order_items</code> gives a list of all orders, with a timestamp when each order was placed:</p><div class="interstitial code">
<pre><code class="highlight erb"> <span class="nt"><p></span>
<span class="nt"><strong></span>Order Date:<span class="nt"></strong></span>
<span class="cp"><%=</span> <span class="n">order_item</span><span class="p">.</span><span class="nf">created_at</span> <span class="cp">%></span>
<span class="nt"></p></span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=" <p>
<strong>Order Date:</strong>
<%= order_item.created_at %>
</p>
">Copy</button>
</div>
<p>We can use the method <code>I18n.localize</code> or <code>I18n.l</code> in the view to
display this in a language appropriate format:</p><div class="interstitial code">
<pre><code class="highlight erb"> <span class="nt"><p></span>
<span class="nt"><strong></span>Order Date:<span class="nt"></strong></span>
<span class="cp"><%=</span><span class="n">l</span> <span class="n">order_item</span><span class="p">.</span><span class="nf">created_at</span> <span class="cp">%></span>
<span class="nt"></p></span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=" <p>
<strong>Order Date:</strong>
<%=l order_item.created_at %>
</p>
">Copy</button>
</div>
<p>In German this will be displayed as:</p><div class="interstitial code">
<pre><code class="highlight html"> <span class="nt"><p></span>
<span class="nt"><strong></span>Order Date:<span class="nt"></strong></span>
Montag, 01. April 2024, 21:25 Uhr
<span class="nt"></p></span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=" <p>
<strong>Order Date:</strong>
Montag, 01. April 2024, 21:25 Uhr
</p>
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='internationalization.html#slide-8'>▻</a>
<h3 id="format-definitions"><a class="anchorlink" href="#format-definitions"><span>8.1</span> Format definitions</a></h3><p>You can find the definition for this date in <code>de.yml</code> under <code>de.time.formats.default</code>:</p><div class="interstitial code">
<pre><code class="highlight yml"> <span class="na">time</span><span class="pi">:</span>
<span class="na">formats</span><span class="pi">:</span>
<span class="na">default</span><span class="pi">:</span> <span class="s2">"</span><span class="s">%A,</span><span class="nv"> </span><span class="s">%d.</span><span class="nv"> </span><span class="s">%B</span><span class="nv"> </span><span class="s">%Y,</span><span class="nv"> </span><span class="s">%H:%M</span><span class="nv"> </span><span class="s">Uhr"</span>
<span class="na">long</span><span class="pi">:</span> <span class="s2">"</span><span class="s">%A,</span><span class="nv"> </span><span class="s">%d.</span><span class="nv"> </span><span class="s">%B</span><span class="nv"> </span><span class="s">%Y,</span><span class="nv"> </span><span class="s">%H:%M</span><span class="nv"> </span><span class="s">Uhr"</span>
<span class="na">short</span><span class="pi">:</span> <span class="s2">"</span><span class="s">%d.</span><span class="nv"> </span><span class="s">%b,</span><span class="nv"> </span><span class="s">%H:%M</span><span class="nv"> </span><span class="s">Uhr"</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=" time:
formats:
default: "%A, %d. %B %Y, %H:%M Uhr"
long: "%A, %d. %B %Y, %H:%M Uhr"
short: "%d. %b, %H:%M Uhr"
">Copy</button>
</div>
<p>To choose another format, you can add call <code>l</code> with the format option:</p><div class="interstitial code">
<pre><code class="highlight erb"> <span class="nt"><p></span>
<span class="nt"><strong></span>Order Date:<span class="nt"></strong></span>
<span class="cp"><%=</span><span class="n">l</span> <span class="n">order_item</span><span class="p">.</span><span class="nf">created_at</span><span class="p">,</span> <span class="ss">format: :short</span> <span class="cp">%></span>
<span class="nt"></p></span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=" <p>
<strong>Order Date:</strong>
<%=l order_item.created_at, format: :short %>
</p>
">Copy</button>
</div>
<p>Different languages will have different format, here a comparison of <code>:short</code>:</p><div class="interstitial code">
<pre><code class="highlight yml"><span class="na">en</span><span class="pi">:</span>
<span class="na">short</span><span class="pi">:</span> <span class="s2">"</span><span class="s">%d</span><span class="nv"> </span><span class="s">%b</span><span class="nv"> </span><span class="s">%H:%M"</span>
<span class="na">es</span><span class="pi">:</span>
<span class="na">short</span><span class="pi">:</span> <span class="s2">"</span><span class="s">%-d</span><span class="nv"> </span><span class="s">de</span><span class="nv"> </span><span class="s">%b</span><span class="nv"> </span><span class="s">%H:%M"</span>
<span class="na">da</span><span class="pi">:</span>
<span class="na">short</span><span class="pi">:</span> <span class="s2">"</span><span class="s">%e.</span><span class="nv"> </span><span class="s">%b</span><span class="nv"> </span><span class="s">%Y,</span><span class="nv"> </span><span class="s">%H.%M"</span>
<span class="na">de</span><span class="pi">:</span>
<span class="na">short</span><span class="pi">:</span> <span class="s2">"</span><span class="s">%d.</span><span class="nv"> </span><span class="s">%b,</span><span class="nv"> </span><span class="s">%H:%M</span><span class="nv"> </span><span class="s">Uhr"</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="en:
short: "%d %b %H:%M"
es:
short: "%-d de %b %H:%M"
da:
short: "%e. %b %Y, %H.%M"
de:
short: "%d. %b, %H:%M Uhr"
">Copy</button>
</div>
<p>As you can see, it is not only the names of weekdays and months that are
translated, but also the whole format changes from language to language.</p><p>You could add other formats than ':long' and ':short' to your translations
by adding to the <code>.yml</code> files. Just make sure to keep the keys consistent through all languages!</p></section>
<section><a class='slide_break' href='internationalization.html#slide-9'>▻</a>
<h3 id="relative-dates"><a class="anchorlink" href="#relative-dates"><span>8.2</span> Relative Dates</a></h3><p>The helper <code>time_ago_in_words</code> is already localized, you
can use it directly:</p><div class="interstitial code">
<pre><code class="highlight erb"> <span class="nt"><p></span>
<span class="nt"><strong></span>Order Date:<span class="nt"></strong></span>
<span class="cp"><%=</span> <span class="n">time_ago_in_words</span> <span class="n">order_item</span><span class="p">.</span><span class="nf">created_at</span> <span class="cp">%></span>
<span class="nt"></p></span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=" <p>
<strong>Order Date:</strong>
<%= time_ago_in_words order_item.created_at %>
</p>
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='internationalization.html#slide-10'>▻</a>
<h2 id="pluralization"><a class="anchorlink" href="#pluralization"><span>9</span> Pluralization</a></h2><p>The page <code>/order_items</code> gives a list of all orders, and also the number of orders.
Before internationalization this looked like this:</p><div class="interstitial code">
<pre><code class="highlight erb"><span class="nt"><p></span>There are <span class="cp"><%=</span> <span class="vi">@order_items</span><span class="p">.</span><span class="nf">length</span> <span class="cp">%></span> orders in the system.<span class="nt"></p></span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="<p>There are <%= @order_items.length %> orders in the system.</p>
">Copy</button>
</div>
<p>When translating this we also have to think about pluralization rules.
English is simple: just add an 's' to the end of the noun for more then one order.
Other languages follow more complex rules.</p><p>You can see how the example from polish in the video above is handled in the
standard translation for dates:</p>
<ul>
<li>Rails Pluralisation Rules for Polish <a href="https://github.com/svenfuchs/rails-i18n/blob/master/rails/pluralization/pl.rb">pluralization/pl.rb</a></li>
<li>Use in <a href="https://github.com/svenfuchs/rails-i18n/blob/10141c451f03d7c6b78cfdcce808c389da6b9ddd/rails/locale/pl.yml#L63">locale/pl.rb</a></li>
</ul>
<p>To build our own pluralisation rules we can add translations with several cases.
How many cases there are, and what they are called, differs from language
to language. Make sure to look it up in <a href="https://github.com/svenfuchs/rails-i18n/blob/master/rails/pluralization/">pluralization/*</a></p><div class="interstitial code">
<pre><code class="highlight yml"> <span class="na">orders_in_the_system</span><span class="pi">:</span>
<span class="na">zero</span><span class="pi">:</span> <span class="s">Es gibt keine Bestellungen im System</span>
<span class="na">one</span><span class="pi">:</span> <span class="s">Es gibt eine Bestellung im System</span>
<span class="na">other</span><span class="pi">:</span> <span class="s">Es gibt %{count} Bestellungen im System</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=" orders_in_the_system:
zero: Es gibt keine Bestellungen im System
one: Es gibt eine Bestellung im System
other: Es gibt %{count} Bestellungen im System
">Copy</button>
</div>
<p>In the view you use it like this:</p><div class="interstitial code">
<pre><code class="highlight erb"><span class="nt"><p></span><span class="cp"><%=</span><span class="n">t</span><span class="p">(</span><span class="s1">'orders_in_the_system'</span><span class="p">,</span> <span class="ss">count: </span><span class="vi">@order_items</span><span class="p">.</span><span class="nf">length</span><span class="p">)</span> <span class="cp">%></span><span class="nt"></p></span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="<p><%=t('orders_in_the_system', count: @order_items.length) %></p>
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='internationalization.html#slide-11'>▻</a>
<h2 id="translating-model-names-and-attributes"><a class="anchorlink" href="#translating-model-names-and-attributes"><span>10</span> Translating Model Names and Attributes</a></h2><p>The names of models and their attributes will be used
in many places in the app: from labels for form fields
to error messages in validation.</p><p>So it makes sense to store the names of models and their attributes
just once and then reuse them.</p><p>In the demo app there are three models: Shirt, Statement and OrderItem.
This is how to specify translations for shirts:</p><div class="interstitial code">
<pre><code class="highlight yml"> <span class="na">activerecord</span><span class="pi">:</span>
<span class="na">models</span><span class="pi">:</span>
<span class="na">shirt</span><span class="pi">:</span>
<span class="na">one</span><span class="pi">:</span> <span class="s">Hemd</span>
<span class="na">other</span><span class="pi">:</span> <span class="s">Hemden</span>
<span class="na">attributes</span><span class="pi">:</span>
<span class="na">shirt</span><span class="pi">:</span>
<span class="na">sizes</span><span class="pi">:</span>
<span class="na">one</span><span class="pi">:</span> <span class="s">Größe</span>
<span class="na">other</span><span class="pi">:</span> <span class="s">Größen</span>
<span class="na">colors</span><span class="pi">:</span>
<span class="na">one</span><span class="pi">:</span> <span class="s">Farbe</span>
<span class="na">other</span><span class="pi">:</span> <span class="s">Farben</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=" activerecord:
models:
shirt:
one: Hemd
other: Hemden
attributes:
shirt:
sizes:
one: Größe
other: Größen
colors:
one: Farbe
other: Farben
">Copy</button>
</div>
<p>To refer to a model use <code>.model_name.human</code>, for example <code>Shirt.model_name.human</code>.
To refer to an attribute use <code>human_attribute_name</code>, for example <code>Shirt.human_attribute_name("colors").
Both methods can take an attribute</code>count` for pluralization.</p></section>
<section><a class='slide_break' href='internationalization.html#slide-12'>▻</a>
<h3 id="automatic-texts-for-models"><a class="anchorlink" href="#automatic-texts-for-models"><span>10.1</span> Automatic texts for models</a></h3><p>The combination of model names and prepared translations from the <a href="https://github.com/svenfuchs/rails-i18n/tree/10141c451f03d7c6b78cfdcce808c389da6b9ddd/rails/locale">locale folder of the I18n gem</a> is quite powerful.</p><p>For example the helper for building labels automatically uses human_attribute_names:</p><div class="interstitial code">
<pre><code class="highlight erb"> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">label</span> <span class="ss">:sizes</span> <span class="cp">%></span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=" <%= form.label :sizes %>
">Copy</button>
</div>
<p>When using a the submit button in a form builder:</p><div class="interstitial code">
<pre><code class="highlight erb"><span class="cp"><%=</span> <span class="n">form_for</span> <span class="vi">@shirt</span> <span class="k">do</span> <span class="o">|</span><span class="n">f</span><span class="o">|</span> <span class="cp">%></span>
<span class="cp"><%=</span> <span class="n">f</span><span class="p">.</span><span class="nf">submit</span> <span class="cp">%></span>
<span class="cp"><%</span> <span class="k">end</span> <span class="cp">%></span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="<%= form_for @shirt do |f| %>
<%= f.submit %>
<% end %>
">Copy</button>
</div>
<p>The label will automatically be generated from these keys:</p><div class="interstitial code">
<pre><code class="highlight yml"><span class="na">en</span><span class="pi">:</span>
<span class="na">helpers</span><span class="pi">:</span>
<span class="na">submit</span><span class="pi">:</span>
<span class="na">create</span><span class="pi">:</span> <span class="s2">"</span><span class="s">Create</span><span class="nv"> </span><span class="s">a</span><span class="nv"> </span><span class="s">%{model}"</span>
<span class="na">update</span><span class="pi">:</span> <span class="s2">"</span><span class="s">Confirm</span><span class="nv"> </span><span class="s">changes</span><span class="nv"> </span><span class="s">to</span><span class="nv"> </span><span class="s">%{model}"</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="en:
helpers:
submit:
create: "Create a %{model}"
update: "Confirm changes to %{model}"
">Copy</button>
</div>
<p>so</p><div class="interstitial code">
<pre><code class="highlight erb"> <span class="cp"><%=</span> <span class="n">f</span><span class="p">.</span><span class="nf">submit</span> <span class="cp">%></span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=" <%= f.submit %>
">Copy</button>
</div>
<p>is a short version for</p><div class="interstitial code">
<pre><code class="highlight erb"> <span class="cp"><%=</span> <span class="n">form</span><span class="p">.</span><span class="nf">submit</span> <span class="n">t</span><span class="p">(</span><span class="s1">'helpers.submit.create'</span><span class="p">,</span> <span class="ss">model: </span><span class="no">Shirt</span><span class="p">.</span><span class="nf">model_name</span><span class="p">.</span><span class="nf">human</span> <span class="p">)</span> <span class="cp">%></span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=" <%= form.submit t('helpers.submit.create', model: Shirt.model_name.human ) %>
">Copy</button>
</div>
<p>Source: <a href="https://api.rubyonrails.org/v7.1.3.2/classes/ActionView/Helpers/FormBuilder.html#method-i-submit">Ruby on Rails Documentation, Action View Form Builder</a></p></section>
<section><a class='slide_break' href='internationalization.html#slide-13'>▻</a>
<h3 id="automatic-texts-for-validations-and-error-messages"><a class="anchorlink" href="#automatic-texts-for-validations-and-error-messages"><span>10.2</span> Automatic texts for validations and error messages</a></h3><p>Setting the name for models and attributes is enough to translate validations
and their error messages:</p><div class="interstitial code">
<pre><code class="highlight rb"><span class="k">class</span> <span class="nc">Shirt</span>
<span class="n">validates</span> <span class="ss">:name</span><span class="p">,</span> <span class="ss">presence: </span><span class="kp">true</span><span class="p">,</span> <span class="ss">length: </span><span class="p">{</span> <span class="ss">minimum: </span><span class="mi">3</span> <span class="p">}</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="class Shirt
validates :name, presence: true, length: { minimum: 3 }
">Copy</button>
</div>
<p>Will automatically generate error messages like:</p>
<blockquote>
<p>Name muss ausgefüllt werden
Name ist zu kurz (weniger als 3 Zeichen)</p></blockquote>
</section>
<section><a class='slide_break' href='internationalization.html#slide-14'>▻</a>
<h2 id="text-stored-in-tables"><a class="anchorlink" href="#text-stored-in-tables"><span>11</span> Text stored in tables</a></h2><p>The demo app is a shop for shirts with slogans printed on them.
In the first implementation all the slogans are in english.</p><p>When we think about selling to different markets we might want
to translate text stored and tables for different purposes:</p>
<ul>
<li>translate the shirts names, for example "generic t-shirt" and "fine t-shirt"</li>
<li>keep the english slogans, but provide translation into several languages to make searching easier
<ul>
<li>for example when you search for "freie software" in the app, you should find the slogans related to "free software"</li>
</ul></li>
<li>offer slogans in different languages
<ul>
<li>should these be different products with different ids? or should there be several columns for one product?</li>
</ul></li>
</ul>
</section>
<section><a class='slide_break' href='internationalization.html#slide-15'>▻</a>
<h3 id="the-mobility-gem"><a class="anchorlink" href="#the-mobility-gem"><span>11.1</span> the Mobility gem</a></h3><p>The gem Mobility adds translations for table data.
In the demo app Mobility is already installed and configured.</p><p>To make the name of a shirt translatable we add the following lines to the model:</p><div class="interstitial code">
<pre><code class="highlight rb"> <span class="kp">extend</span> <span class="no">Mobility</span>
<span class="n">translates</span> <span class="ss">:name</span><span class="p">,</span> <span class="ss">type: :string</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text=" extend Mobility
translates :name, type: :string
">Copy</button>
</div>
<p>From now on the gem will enable us to store different names.
For example in the seed file we could do this:</p><div class="interstitial code">
<pre><code class="highlight rb"><span class="no">I18n</span><span class="p">.</span><span class="nf">locale</span> <span class="o">=</span> <span class="ss">:en</span> <span class="c1"># switch to english</span>
<span class="n">s1</span> <span class="o">=</span> <span class="no">Shirt</span><span class="p">.</span><span class="nf">create!</span><span class="p">(</span>
<span class="ss">name: </span><span class="s1">'Polo Shirt'</span><span class="p">,</span>
<span class="ss">outline: </span><span class="no">File</span><span class="p">.</span><span class="nf">read</span><span class="p">(</span><span class="no">Rails</span><span class="p">.</span><span class="nf">root</span><span class="p">.</span><span class="nf">join</span><span class="p">(</span><span class="s1">'db/seeds/Shirt-type_Polo-omg.svg'</span><span class="p">)),</span>
<span class="ss">colors: </span><span class="s1">'#e0ffcd #fdffcd #ffebbb #ffcab0 white'</span><span class="p">,</span>
<span class="ss">sizes: </span><span class="s1">'S M L XL XXL'</span>
<span class="p">)</span>
<span class="no">I18n</span><span class="p">.</span><span class="nf">locale</span> <span class="o">=</span> <span class="ss">:de</span> <span class="c1"># switch to german</span>
<span class="n">s1</span><span class="p">.</span><span class="nf">update</span><span class="p">(</span><span class="ss">name: </span><span class="s1">'Polohemd'</span><span class="p">)</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="I18n.locale = :en # switch to english
s1 = Shirt.create!(
name: 'Polo Shirt',
outline: File.read(Rails.root.join('db/seeds/Shirt-type_Polo-omg.svg')),
colors: '#e0ffcd #fdffcd #ffebbb #ffcab0 white',
sizes: 'S M L XL XXL'
)
I18n.locale = :de # switch to german
s1.update(name: 'Polohemd')
">Copy</button>
</div>
<p>Now every time the object is read from the database and
the name is retrieved, the locale is used. For example in the rails console
this could look like this:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="n">irb</span><span class="p">(</span><span class="n">main</span><span class="p">):</span><span class="mo">001</span><span class="o">></span> <span class="n">s1</span> <span class="o">=</span> <span class="no">Shirt</span><span class="p">.</span><span class="nf">first</span>
<span class="no">Shirt</span> <span class="no">Load</span> <span class="p">(</span><span class="mf">1.5</span><span class="n">ms</span><span class="p">)</span> <span class="no">SELECT</span> <span class="s2">"shirts"</span><span class="p">.</span><span class="nf">*</span> <span class="no">FROM</span> <span class="s2">"shirts"</span> <span class="no">ORDER</span> <span class="no">BY</span> <span class="s2">"shirts"</span><span class="o">.</span><span class="s2">"id"</span> <span class="no">ASC</span> <span class="no">LIMIT</span> <span class="vg">$1</span> <span class="p">[[</span><span class="s2">"LIMIT"</span><span class="p">,</span> <span class="mi">1</span><span class="p">]]</span>
<span class="o">=></span>
<span class="c1">#<Shirt:0x00000001271fc570</span>
<span class="o">...</span>
<span class="n">irb</span><span class="p">(</span><span class="n">main</span><span class="p">):</span><span class="mo">002</span><span class="o">></span> <span class="n">s1</span><span class="p">.</span><span class="nf">name</span>
<span class="no">Mobility</span><span class="o">::</span><span class="no">Backends</span><span class="o">::</span><span class="no">ActiveRecord</span><span class="o">::</span><span class="no">KeyValue</span><span class="o">::</span><span class="no">StringTranslation</span> <span class="no">Load</span> <span class="p">(</span><span class="mf">1.3</span><span class="n">ms</span><span class="p">)</span> <span class="no">SELECT</span> <span class="s2">"mobility_string_translations"</span><span class="p">.</span><span class="nf">*</span> <span class="no">FROM</span> <span class="s2">"mobility_string_translations"</span> <span class="no">WHERE</span> <span class="s2">"mobility_string_translations"</span><span class="o">.</span><span class="s2">"translatable_id"</span> <span class="o">=</span> <span class="vg">$1</span> <span class="no">AND</span> <span class="s2">"mobility_string_translations"</span><span class="o">.</span><span class="s2">"translatable_type"</span> <span class="o">=</span> <span class="vg">$2</span> <span class="no">AND</span> <span class="s2">"mobility_string_translations"</span><span class="o">.</span><span class="s2">"key"</span> <span class="o">=</span> <span class="vg">$3</span> <span class="p">[[</span><span class="s2">"translatable_id"</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="s2">"translatable_type"</span><span class="p">,</span> <span class="s2">"Shirt"</span><span class="p">],</span> <span class="p">[</span><span class="s2">"key"</span><span class="p">,</span> <span class="s2">"name"</span><span class="p">]]</span>
<span class="o">=></span> <span class="s2">"Polo Shirt"</span>
<span class="n">irb</span><span class="p">(</span><span class="n">main</span><span class="p">):</span><span class="mo">003</span><span class="o">></span> <span class="no">I18n</span><span class="p">.</span><span class="nf">locale</span> <span class="o">=</span> <span class="ss">:de</span>
<span class="o">=></span> <span class="ss">:de</span>
<span class="n">irb</span><span class="p">(</span><span class="n">main</span><span class="p">):</span><span class="mo">004</span><span class="o">></span> <span class="n">s1</span><span class="p">.</span><span class="nf">name</span>
<span class="o">=></span> <span class="s2">"Polohemd"</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="irb(main):001> s1 = Shirt.first
Shirt Load (1.5ms) SELECT "shirts".* FROM "shirts" ORDER BY "shirts"."id" ASC LIMIT $1 [["LIMIT", 1]]
=>
#<Shirt:0x00000001271fc570
...
irb(main):002> s1.name
Mobility::Backends::ActiveRecord::KeyValue::StringTranslation Load (1.3ms) SELECT "mobility_string_translations".* FROM "mobility_string_translations" WHERE "mobility_string_translations"."translatable_id" = $1 AND "mobility_string_translations"."translatable_type" = $2 AND "mobility_string_translations"."key" = $3 [["translatable_id", 1], ["translatable_type", "Shirt"], ["key", "name"]]
=> "Polo Shirt"
irb(main):003> I18n.locale = :de
=> :de
irb(main):004> s1.name
=> "Polohemd"
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='internationalization.html#slide-16'>▻</a>
<h2 id="testing"><a class="anchorlink" href="#testing"><span>12</span> Testing</a></h2></section>
<section><a class='slide_break' href='internationalization.html#slide-17'>▻</a>
<h3 id="url-and-path-helpers"><a class="anchorlink" href="#url-and-path-helpers"><span>12.1</span> url and path helpers</a></h3><p>A first hurdle when running your test might be generating the right
urls and paths. This happens in both controller tests and system tests.
The errors might look something like this:</p><div class="interstitial code">
<pre><code class="highlight plaintext">Error:
StatementsTest#test_should_destroy_Statement:
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"statements", :locale=>#<Statement ...>}, missing required keys: [:id]
test/system/statements_test.rb:42:in `block in <class:StatementsTest>'
bin/rails test test/system/statements_test.rb:41
</code></pre>
<button class="clipboard-button" data-clipboard-text="Error:
StatementsTest#test_should_destroy_Statement:
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"statements", :locale=>#<Statement ...>}, missing required keys: [:id]
test/system/statements_test.rb:42:in `block in <class:StatementsTest>'
bin/rails test test/system/statements_test.rb:41
">Copy</button>
</div>
</section>
<section><a class='slide_break' href='internationalization.html#slide-18'>▻</a>
<h2 id="see-also"><a class="anchorlink" href="#see-also"><span>13</span> See Also</a></h2>
<ul>
<li><a href="https://thoughtbot.com/blog/better-tests-through-internationalization">Better Tests Through Internationalization </a></li>
<li><a href="https://guides.rubyonrails.org/i18n.html">Rails Guide: Internationalization</a></li>
<li><a href="https://metacpan.org/pod/distribution/Locale-Maketext/lib/Locale/Maketext/TPJ13.pod#A-Localization-Horror-Story:-It-Could-Happen-To-You">A Localization Horror Story: It Could Happen To You</a></li>
</ul>
</div></section>
</div>
</div>
<!-- End slides. -->
<!-- Required JS files. -->
<script src="javascripts/reveal.js"></script>
<script src="javascripts/search.js"></script>
<script src="javascripts/markdown.js"></script>
<script>
// Also available as an ES module, see:
// https://revealjs.com/initialization/
Reveal.initialize({
controls: false,
progress: true,
center: false,
hash: true,
// The "normal" size of the presentation, aspect ratio will
// be preserved when the presentation is scaled to fit different
// resolutions. Can be specified using percentage units.
width: 1000,
height: 600,
disableLayout: false,
// Factor of the display size that should remain empty around
// the content
margin: 0.05,
// Bounds for smallest/largest possible scale to apply to content
minScale: 0.2,
maxScale: 10.0,
keyboard: {
27: () => {
// do something custom when ESC is pressed
var new_url = window.location.pathname.replace('slides_', '') + window.location.hash.replace('/','slide-');
window.location = new_url;
},
191: 'toggleHelp',
13: 'next', // go to the next slide when the ENTER key is pressed
},
// Learn about plugins: https://revealjs.com/plugins/
plugins: [ RevealSearch, RevealMarkdown ]
});
</script>
</body>
</html>