【问题标题】:Shiny.i18n not translate the modal dialoge rendered within moduleShiny.i18n 不翻译模块内呈现的模态对话框
【发布时间】:2020-12-25 21:47:06
【问题描述】:

我想在模块化闪亮应用中翻译部分 UI。当我总结我的简化代码时, 在第一个模块中,我对 i18n 没有任何问题,因为它使用参数 i18n 进入模块 1 和 翻译在 registerUI 中效果很好(基于推荐 here)。但我的问题是这个功能的模块 2 (M2UI) 的 UI 本身在模块 1(注册)的服务器中调用以返回模态对话。但未检测到 i18n 并且翻译不适用于显示的新模式。有什么建议为什么会发生这种情况?提前谢谢...

我编辑了我的示例,现在它完全可以重现了。翻译 csv 文件可用here。只需将它们复制到“翻译”文件夹中即可。并且,模块应该被复制到“模块”文件夹中。

## CSV translation files are available at : https://github.com/Appsilon/shiny.i18n/tree/master/examples/data

# Copy "translation_it.csv" and "translation_pl.csv" files to "translations" folder

###### make modules and copy them into folder "modules"
source("modules/register.R")
source("modules/M2.R")
#####

library(shiny)
library(shiny.i18n)
library(shinydashboard)
 
i18n <- Translator$new(translation_csvs_path = "translations")
i18n$set_translation_language("en")
shiny.i18n::usei18n(i18n) 

############################ UI

header <- dashboardHeader(title = i18n$t('Hello Shiny!'), titleWidth = 400 ,
                          
                          tags$li( fluidRow( 
                            shiny.i18n::usei18n(i18n),
                            div(style="display: inline-block;vertical-align:top; font-size: 10px; height=30px;width: 150px;",selectInput(
                              inputId='selected_language',
                              label=i18n$t('Change language'),
                              choices = i18n$get_languages(),
                              selected = i18n$get_key_translation()
                            )) 
                            
                         
                             
                          ),
                          class = "dropdown") 
                           
                          
                          
)


# Sidebar Menu ------------------------------------------------------------
sidebar <- dashboardSidebar(width = 220,
                            
                            sidebarMenu(
                              menuItem( i18n$t("Hello Shiny!"), tabName = "diary", icon = icon("align-justify")),
                              menuItem("Help", tabName = "help", icon = icon("table")),
                              #menuItem("Data analysis", tabName = "descriptive", icon = icon("chart-bar")),
                              menuItem("About", tabName = "about", icon = icon("info-circle"))
                              
                            ) 
                            
) 



body <- dashboardBody(
  
  
  tabItems(
    tabItem("diary", 
            # includeMarkdown("Introduction.Rmd"),
            # includeMarkdown("Contact.Rmd")
            titlePanel(i18n$t("Hello Shiny!")),
            sidebarLayout(
              sidebarPanel(
                sliderInput("bins",
                            i18n$t("Number of bins:"),
                            min = 1,
                            max = 50,
                            value = 30)
              ),
              mainPanel(
                plotOutput("distPlot"),
                actionButton("test","test"),
                p(i18n$t("This is description of the plot."))
              )
            ),
            tags$style(type = "text/css", ".recalculating {opacity: 1.0;}"),   # Prevents gray screen during Sys.sleep()
            
    ), 
    
    
    
    tabItem("help", 
            
    ), 
    
    tabItem("about",
            
    )
  )
  
)




ui <- dashboardPage(title = 'Coronavirus', header, sidebar, body, skin='blue')


#################################### SERVER


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

  
  
  observeEvent(input$selected_language, {
    update_lang(session, input$selected_language)
  })
  
  shiny::observeEvent(input$test, {
    
       registerUI(id = "REG",reg_title=i18n$t("Hello Shiny!"),i18n=i18n )  #This ID should be mached with ID in server
      
    
  })
  
   callModule(register,id = "REG", title= i18n$t("Hello Shiny!"), i18n=i18n )
   
  
  
  output$distPlot <- renderPlot({
    x    <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    hist(x, breaks = bins,
         col = "darkgray", border = "white",
         main = i18n$t("Histogram of x"), ylab = i18n$t("Frequency"))
  })
}

shinyApp(ui = ui, server = server)

registerUI <- function(id, reg_title=NULL  ,i18n) {
  ns <- shiny::NS(id)

  shiny.i18n::usei18n(i18n)  
  
  showModal(tags$div( modalDialog(title = "" ,size="s",
                                             
                                             shiny::div(id =ns("regpanel"),   
                                                        shiny::wellPanel(
                                                          shiny::tags$h2(reg_title, class = "text-center", style = "padding-top: 0;"),
                                                          
                                                          shinyjs::disabled(shiny::textInput(ns("user_name1"), value= "", shiny::tagList(shiny::icon("user"), "suggested user name"))) ,# 
                                                              shiny::actionButton(ns("regSubmit"),  i18n$t("Submit") , class = "btn-primary", style = "color: white;")
                                                            
                                                          ) 
                                                          
                                                          
                                                        )
                                             ),
                                             
                                             
                                             easyClose = TRUE, footer = NULL )) 
}


###### Module 1

register <- function(input, output, session ,title,i18n) {
  ns <- session$ns
  
   shiny::observeEvent(input$regSubmit, {
     shiny.i18n::usei18n(i18n)     
     removeModal() 
     M2UI(id =   ns("M2") ,reg_title=i18n$t("Hello Shiny!" ),i18n=i18n ) 
     
     
  })
   callModule(M2,id = "M2" , title= i18n$t("Hello Shiny!"),i18n=i18n)

}

###### Module 2
M2UI <- function(id, reg_title=NULL,i18n ) {
  ns <- shiny::NS(id)
  shiny.i18n::usei18n(i18n) 
  
    showModal(modalDialog(title = reg_title ,size="s",
                          
                        shiny::wellPanel(
                          
                          shiny::actionButton(ns("Finish"),  i18n$t("Hello Shiny!" ) )
                          
                        ) 
                        
                        , easyClose = TRUE, footer = NULL ) )
 
}


M2 <- function(input, output, session ,title,i18n) {
  ns <- session$ns
  
  shiny::observeEvent(input$Finish, {
    
    removeModal()
    
  })
  
  
}

【问题讨论】:

  • 试试registerUI(id = ns("REG"), i18n=i18n )
  • 您好 YBS,您的建议无效。我试图为自己简化情况。为澄清起见,假设一个按钮,单击它后模块返回一个模态对话,在这个模态翻译中是可以的。但是,如果就在 observeEvent() 中的 Module_UI() 行之后,您使用 callModule() 调用 Module_server 可以删除以前的模态并打开一个新模态,现在在这个新的模态转换中不起作用!因此我认为问题与 callModule() 函数有关。但我还没有找到任何解决方案。
  • 我很惊讶它完全有效。代码中至少有两个语法错误:1) 在body 之前缺少sidebar 的右括号。 2) registerUI 模块 2 中的 showModal 中缺少逗号。 - 我不确定我是否能提供帮助,但我的下一个尝试是检查反应域(i18n$t 默认使用 shiny::getDefaultReactiveDomain())。
  • 不幸的是我上面的代码是不可重现的。我将对其进行编辑并替换可重现的代码以澄清问题。
  • 现在我编辑了示例代码,它现在可以重现了。问题是 i18n 没有翻译模态对话 2 中的文本。

标签: r json shiny internationalization translation


【解决方案1】:

您是否尝试过最新的开发版本?您必须通过开发包更新它。我很确定它在那里修好了。该问题涉及通过闪亮会话丢失的回调。

您的示例适用于我的设置。

【讨论】:

  • 亲爱的 Hamidburg,感谢您的回答。单击第一个模态中的提交按钮后,您确定它在第二个模态对话中有效吗?我安装了 Shiny.i18n 的开发版本,但问题没有解决!第二个模态对话没有翻译!请您是否可以再次检查并告诉我。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多