Another take on building a multi-lingual shiny app

November 8, 2014
By

(This article was first published on Christophe Ladroue » R, and kindly contributed to R-bloggers)

I was reading this interesting post about how to build a multi-lingual Shiny app. I’m also building a multi-lingual Shiny app and came up with slightly different take on it.

First, I don’t use a function for finding the translation, but a 2D list. This way I can directly get to the translation with a simple access to the list.

Select All Code:

1
2
3
4
5
6
7
translation <- list(
  "youhaveselected" = list("en" = "You have selected:", "fr"="Vous avez sélectionné:"),
  "greetings" = list("en" = "Hello", "fr"="Bonjour")
  )
# then:
translation[['greetings']][['en']] # Hello
translation[['greetings']][['fr']]  # Bonjour

Second, I don’t use observe, as I didn’t find it necessary. I simply have a radio button for switching between languages, and a function tr() to translate a phrase or a list of phrases. Like in the original post, the UI is built from server.R using renderUI().

Select All Code:

1
2
3
  tr <- function(text){ # translates text into current language
    sapply(text,function(s) translation[[s]][[input$language]], USE.NAMES=FALSE)
  }
Select All Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
  # UI
  output$uiObs <- renderUI({
    sliderInput("obs", tr("numberOfObservations"),  
                  min = 1, max = 100, value = 50)
  })
 
  output$uiWeekdays <- renderUI({
    # Using a named list in order to pass the same value regardless of the label (which changes with the language)
    daysValue <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
    days <- structure(daysValue, .Names = tr(daysValue))
 
    selectInput(inputId   = "weekdays",
                label     = tr("Selection:"),
                choices   = days,
                multiple  = TRUE)
  })

To make things easier for the translators, the dictionary is stored as a csv file, which is easy to edit. A small R script turns the csv into the expected 2D list, and saves it in a binary file, to avoid re-processing the file every time the user decides to switch language.

Select All Code:

1
2
3
4
5
6
7
8
9
# update the processed translation file translation.bin
# run this every time dictionary.csv is updated 
# it reads the look-up table in dictionary.csv and turns it into a 2D list
 
library(plyr)
translationContent <- read.delim("dictionary.csv", header = TRUE, sep = "t", as.is = TRUE) 
translation <- dlply(translationContent ,.(key), function(s) key = as.list(s))
 
save(translation, file = "translation.bin")

You can consult the whole code on the github repository and run it directly from R using:

Select All Code:

1
shiny::runGitHub("multilingualShinyApp","chrislad")

To leave a comment for the author, please follow the link and comment on their blog: Christophe Ladroue » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)