r-Shiny.i18n不翻译模块内呈现的模态对话



我想在一个模块化的闪亮应用程序中翻译部分UI。当我总结我的简化代码时,在第一个模块中,我对i18n没有问题,因为它以参数i18n进入模块1,并且翻译在registerUI中运行良好(基于此处的推荐(。但我的问题是这个功能的模块2(M2UI(的UI其本身在模块1(寄存器(的服务器内被调用以返回模态对话。但是i18n没有被检测到,并且翻译不能在显示的新模态上工作。为什么会发生这种情况,有什么建议吗?提前感谢。。。

我编辑了我的例子,现在它完全可以复制了。此处提供翻译csv文件。只需将它们复制到";翻译";福尔德。并且,模块应该被复制到";模块";文件夹

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

})


}

您尝试过最新的开发版本吗?您必须通过开发包对其进行更新。我很确定它是在那里修复的。该问题视为通过闪亮会话丢失回调。

你的例子适用于我的设置。

最新更新