-
Notifications
You must be signed in to change notification settings - Fork 38
/
rhino.Rmd
627 lines (525 loc) · 14.1 KB
/
rhino.Rmd
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
---
title: "shiny.i18n in Rhino applications"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{shiny.i18n in Rhino applications}
%\VignetteEngine{knitr::knitr}
%\VignetteEncoding{UTF-8}
---
# Introduction
This tutorial shows an easy way of implementing internationalization in a Shiny application
built using [Rhino framework](https://appsilon.github.io/rhino/).
The goal is to recreate a simple app that can be [found in the
examples](https://github.com/Appsilon/shiny.i18n/blob/master/examples/live_language_change/browser_app.R), but with the Rhino setup.
# Setup Rhino application
The first step is to create a Rhino application.
Before that, make sure that you have `rhino` installed:
```r
install.packages("rhino")
```
Now, initialize a new Rhino application. You can do it either by using RStudio Wizard
or a function `rhino::init("rhino_i18n")`.
More details on how to create a Rhino app can be found in the [Rhino tutorial](https://appsilon.github.io/rhino/articles/tutorial/create-your-first-rhino-app.html#create-an-initial-application).
After initializing the application, the next step is to install `shiny.i18n` and add it to the dependencies.
In the R console run:
```r
install.packages("shiny.i18n")
```
After that, modify `dependencies.R` file:
```r
# dependencies.R
library(rhino)
library(shiny.i18n)
```
And update `renv.lock` by running in the R console:
```r
renv::snapshot()
```
# Build a basic application
The next step is to fill out the application with the content.
We will use the example application that can be [found in the
examples](https://github.com/Appsilon/shiny.i18n/blob/master/examples/live_language_change/browser_app.R).
## Initial application
First, let's implement the basic version of the app (without internationalization).
Modify `app/main.R` in the following way:
```r
# app/main.R
box::use(
shiny,
graphics[hist],
)
#' @export
ui <- function(id) {
ns <- shiny$NS(id)
shiny$fluidPage(
shiny$div(
style = "float: right;",
shiny$selectInput(
ns("selected_language"),
"Change language",
choices = c("en", "pl", "it")
)
),
shiny$titlePanel("Hello Shiny!", windowTitle = NULL),
shiny$sidebarLayout(
shiny$sidebarPanel(
shiny$sliderInput(
ns("bins"),
"Number of bins:",
min = 1,
max = 50,
value = 30
)
),
shiny$mainPanel(
shiny$plotOutput(ns("distPlot")),
shiny$p("This is description of the plot.")
)
)
)
}
#' @export
server <- function(id) {
shiny$moduleServer(id, function(input, output, session) {
output$distPlot <- shiny$renderPlot({
x <- datasets::faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(
x,
breaks = bins,
col = "darkgray",
border = "white",
main = "Histogram of x",
ylab = "Frequency"
)
})
})
}
```
If you run it with `shiny::runApp()` you will see a slightly modified version
of the "Old Faithful Geyser Data" app.
<div class="app-preview">
![](images/i18n_init.png)
</div>
## Add translator
The next step is to add an instance of `shiny.i18n::Translator` class and provide the translations.
In `shiny.i18n`, translations can be added as CSV or JSON files - we will use the latter.
Create `app/translations` directory, and inside add `translations.json` file:
```json
{
"languages": ["en", "pl", "it"],
"translation": [
{
"en": "Hello Shiny!",
"pl": "Witaj Shiny!",
"it": "Ciao Shiny!"
},
{
"en": "Number of bins:",
"pl": "Liczba podziałek:",
"it": "Numero di intervalli"
},
{
"en": "This is description of the plot.",
"pl": "To jest opis obrazka.",
"it": "Questa è la descrizione della grafico."
},
{
"en": "Histogram of x",
"pl": "Histogram x",
"it": "Istogramma di x"
},
{
"en": "Frequency",
"pl": "Częstotliwość",
"it": "Frequenza"
},
{
"en": "Change language",
"pl": "Wybierz język",
"it": "Cambia lingua"
}
]
}
```
Now, we need to create a Translator and pass the path to the translation file.
In the `app/main.R` file add:
```r
# app/main.R
box::use(
shiny,
graphics[hist],
shiny.i18n[Translator],
)
i18n <- Translator$new(translation_json_path = "app/translations/translations.json")
#' @export
ui <- function(id) {
...
```
The next step is to choose the default translation language:
```r
# app/main.R
box::use(
shiny,
graphics[hist],
shiny.i18n[Translator],
)
i18n <- Translator$new(translation_json_path = "app/translations/translations.json")
i18n$set_translation_language("en")
#' @export
ui <- function(id) {
...
```
## Translate UI elements
Now we are ready to start plugging the translator into the application.
First, the language selection input should use languages available in the translator
and start with the default language selected.
```r
# app/main.R
box::use(
shiny,
graphics[hist],
shiny.i18n[Translator],
)
i18n <- Translator$new(translation_json_path = "app/translations/translations.json")
i18n$set_translation_language("en")
#' @export
ui <- function(id) {
ns <- shiny$NS(id)
shiny$fluidPage(
shiny$div(
style = "float: right;",
shiny$selectInput(
ns("selected_language"),
"Change language",
choices = i18n$get_languages(),
selected = i18n$get_key_translation()
)
),
shiny$titlePanel("Hello Shiny!", windowTitle = NULL),
...
```
We also need to add `shiny.i18n` dependencies and add the translator to the UI
by calling `usei18n()`:
```r
# app/main.R
box::use(
shiny,
graphics[hist],
shiny.i18n[Translator, usei18n],
)
i18n <- Translator$new(translation_json_path = "app/translations/translations.json")
i18n$set_translation_language("en")
#' @export
ui <- function(id) {
ns <- shiny$NS(id)
shiny$fluidPage(
usei18n(i18n),
shiny$div(
style = "float: right;",
shiny$selectInput(
ns("selected_language"),
"Change language",
choices = i18n$get_languages(),
selected = i18n$get_key_translation()
)
),
...
```
We still miss the connection between the select input and the translation language change.
To achieve that, we will use an `observeEvent` with `shiny.i18n::update_lang` function:
```r
# app/main.R
box::use(
shiny,
graphics[hist],
shiny.i18n[Translator, usei18n, update_lang],
)
i18n <- Translator$new(translation_json_path = "app/translations/translations.json")
i18n$set_translation_language("en")
#' @export
ui <- function(id) {
ns <- shiny$NS(id)
shiny$fluidPage(
usei18n(i18n),
shiny$div(
style = "float: right;",
shiny$selectInput(
ns("selected_language"),
"Change language",
choices = i18n$get_languages(),
selected = i18n$get_key_translation()
)
),
shiny$titlePanel("Hello Shiny!", windowTitle = NULL),
shiny$sidebarLayout(
shiny$sidebarPanel(
shiny$sliderInput(
ns("bins"),
"Number of bins:",
min = 1,
max = 50,
value = 30
)
),
shiny$mainPanel(
shiny$plotOutput(ns("distPlot")),
shiny$p("This is description of the plot.")
)
)
)
}
#' @export
server <- function(id) {
shiny$moduleServer(id, function(input, output, session) {
shiny$observeEvent(input$selected_language, {
update_lang(input$selected_language)
})
output$distPlot <- shiny$renderPlot({
x <- datasets::faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(
x,
breaks = bins,
col = "darkgray",
border = "white",
main = "Histogram of x",
ylab = "Frequency"
)
})
})
}
```
The next step is to translate text in the UI part of the application.
To do that, simply wrap text in `i18n$translate()` method:
```r
# app/main.R
box::use(
shiny,
graphics[hist],
shiny.i18n[Translator, usei18n, update_lang],
)
i18n <- Translator$new(translation_json_path = "app/translations/translations.json")
i18n$set_translation_language("en")
#' @export
ui <- function(id) {
ns <- shiny$NS(id)
shiny$fluidPage(
usei18n(i18n),
shiny$div(
style = "float: right;",
shiny$selectInput(
ns("selected_language"),
i18n$translate("Change language"),
choices = i18n$get_languages(),
selected = i18n$get_key_translation()
)
),
shiny$titlePanel(i18n$translate("Hello Shiny!"), windowTitle = NULL),
shiny$sidebarLayout(
shiny$sidebarPanel(
shiny$sliderInput(
ns("bins"),
i18n$translate("Number of bins:"),
min = 1,
max = 50,
value = 30
)
),
shiny$mainPanel(
shiny$plotOutput(ns("distPlot")),
shiny$p(i18n$translate("This is description of the plot."))
)
)
)
}
...
```
If you now run the app, you will see that changing the language updates
application text with the proper translation:
<div class="app-preview">
![](images/i18n_browser_app.gif)
</div>
## Translate the server part
The only missing part is the plot - the title and y-axis label.
The text there is rendered in the server, so we need to add the translator there:
```r
# app/main.R
box::use(
shiny,
graphics[hist],
shiny.i18n[Translator, usei18n, update_lang],
)
i18n <- Translator$new(translation_json_path = "app/translations/translations.json")
i18n$set_translation_language("en")
#' @export
ui <- function(id) {
ns <- shiny$NS(id)
shiny$fluidPage(
usei18n(i18n),
shiny$div(
style = "float: right;",
shiny$selectInput(
ns("selected_language"),
i18n$translate("Change language"),
choices = i18n$get_languages(),
selected = i18n$get_key_translation()
)
),
shiny$titlePanel(i18n$translate("Hello Shiny!"), windowTitle = NULL),
shiny$sidebarLayout(
shiny$sidebarPanel(
shiny$sliderInput(
ns("bins"),
i18n$translate("Number of bins:"),
min = 1,
max = 50,
value = 30
)
),
shiny$mainPanel(
shiny$plotOutput(ns("distPlot")),
shiny$p(i18n$translate("This is description of the plot."))
)
)
)
}
#' @export
server <- function(id) {
shiny$moduleServer(id, function(input, output, session) {
shiny$observeEvent(input$selected_language, {
update_lang(input$selected_language)
})
output$distPlot <- shiny$renderPlot({
x <- datasets::faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(
x,
breaks = bins,
col = "darkgray",
border = "white",
main = i18n$translate("Histogram of x"),
ylab = i18n$translate("Frequency")
)
})
})
}
```
Now, if you run the application, everything should work:
<div class="app-preview">
![](images/i18n_single_module_app.gif)
</div>
# Add inner module
A Rhino application is usually built using multiple Shiny modules.
You can learn more about the Rhino structure from [this article](https://appsilon.github.io/rhino/articles/explanation/application-structure.html).
In this section, we are going to show how the translator can be used in such a setup,
by moving the main panel into a separate module.
First, create a new R file `app/view/main_panel.R`:
```r
# app/view/main_panel.R
box::use(
shiny,
)
#' @export
ui <- function(id) {
ns <- shiny$NS(id)
}
#' @export
server <- function(id) {
shiny$moduleServer(id, function(input, output, session) {
})
}
```
Now, let's move the main panel from `app/main.R` into `app/view/main_panel.R`:
```r
# app/view/main_panel.R
box::use(
shiny,
graphics[hist],
)
#' @export
ui <- function(id, i18n) {
ns <- shiny$NS(id)
shiny$mainPanel(
shiny$plotOutput(ns("distPlot")),
shiny$p(i18n$translate("This is description of the plot."))
)
}
#' @export
server <- function(id, bins, i18n) {
shiny$moduleServer(id, function(input, output, session) {
output$distPlot <- shiny$renderPlot({
x <- datasets::faithful[, 2]
bins <- seq(min(x), max(x), length.out = bins() + 1)
hist(
x,
breaks = bins,
col = "darkgray",
border = "white",
main = i18n$translate("Histogram of x"),
ylab = i18n$translate("Frequency")
)
})
})
}
```
The important part to notice here is that the Translator (`i18n` object) needs
to be passed as an argument to the inner module.
The next step is to call the main panel module in the `app/main.R` file:
```r
# app/main.R
box::use(
shiny,
shiny.i18n[Translator, usei18n, update_lang],
)
box::use(
app/view/main_panel
)
i18n <- Translator$new(translation_json_path = "app/translations/translations.json")
i18n$set_translation_language("en")
#' @export
ui <- function(id) {
ns <- shiny$NS(id)
shiny$fluidPage(
usei18n(i18n),
shiny$div(
style = "float: right;",
shiny$selectInput(
ns("selected_language"),
i18n$translate("Change language"),
choices = i18n$get_languages(),
selected = i18n$get_key_translation()
)
),
shiny$titlePanel(i18n$translate("Hello Shiny!"), windowTitle = NULL),
shiny$sidebarLayout(
shiny$sidebarPanel(
shiny$sliderInput(
ns("bins"),
i18n$translate("Number of bins:"),
min = 1,
max = 50,
value = 30
)
),
main_panel$ui(ns("main_panel"), i18n = i18n)
)
)
}
#' @export
server <- function(id) {
shiny$moduleServer(id, function(input, output, session) {
shiny$observeEvent(input$selected_language, {
update_lang(input$selected_language)
})
main_panel$server("main_panel", bins = shiny$reactive(input$bins), i18n = i18n)
})
}
```
If you now run the application, it should look and behave as previously.
The translation mechanism works, but the application code is now better structured.
<div class="app-preview">
![](images/i18n_finall_app.gif)
</div>
----
**Congratulations! You now have a fully armed and operational ~~battle station~~ multilingual Rhino application!**