Translate shiny app using shiny.i18n in modules?

房东的猫 提交于 2021-01-27 17:11:49

问题


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"
    }
    ]
}

回答1:


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)
)


来源:https://stackoverflow.com/questions/60756339/translate-shiny-app-using-shiny-i18n-in-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!