rjsonshinytranslationshiny.i18n

Translate shiny app using shiny.i18n in modules?


Problem: I want to translate parts of a modularised shiny application either to German or to English. The package I want to use is shiny.i18n which seems to work in a non-modularized app and is seemingly easy to handle. However, in the below modularised shiny toy example the translation does not work. Any suggestions why this happens?

Server/UI:

library(shiny)
library(shinydashboard)
library(DT)
library(data.table)
library(shiny.i18n)

i18n <- Translator$new(translation_json_path = "translation.json")
i18n$set_translation_language("en")

source("displayTable_module.R")

ui <- fluidPage(
  table_UI("display_table")
)

server <- function(input, output) {

  callModule(table_server,
             "display_table"
             )
}

shinyApp(ui = ui, server = server)

displayTable_module Module:

## displayTable_module.R
table_UI <- function(id){
  ns <- NS(id)

  shinydashboard::box(i18n$t("Daten Visualisieren"),
    # "Test header",
  DTOutput(ns("table"))
  )
}

table_server <- function(input, output, session){

  output$table <- renderDT(datatable(mtcars))

}

JSON-Translation Mapping file (translation.json):

{
  "languages": ["en", "ger"],
  "translation": [
    {
      "en": "Daten Visualisieren",
      "ger": "Visualize your data"
    }
    ]
}

Solution

  • You can pass the i18n object over from the ui / server function as additional argument:

    displayTable_module.R

    table_UI <- function(id, i18n){
        ns <- NS(id)
    
        shinydashboard::box(i18n$t("Daten Visualisieren"),
                            # "Test header",
                            DTOutput(ns("table"))
        )
    }
    

    app.r

    ui <- fluidPage(
        table_UI("display_table", i18n = i18n)
    )