在模块中使用shiny.i18n 翻译闪亮的应用程序? [英] Translate shiny app using shiny.i18n in modules?

查看:25
本文介绍了在模块中使用shiny.i18n 翻译闪亮的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我想将模块化闪亮应用程序的一部分翻译成德语或英语.我想使用的包是 shiny.i18n,它似乎可以在非模块化应用程序中运行,并且看起来很容易处理.但是,在下面的模块化闪亮玩具示例中,翻译不起作用.有什么建议为什么会发生这种情况吗?

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?

服务器/用户界面:

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 模块:

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.json):

JSON-Translation Mapping file (translation.json):

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

推荐答案

您可以将 i18n 对象作为附加参数从 ui/server 函数传递过来:

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

这篇关于在模块中使用shiny.i18n 翻译闪亮的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆