Shiny.i18n not translate the modal dialoge rendered within module

北城以北 提交于 2021-01-29 08:12:12

问题


I want to translate parts of my UI in a modularized shiny app. As I summarized my simplified code, in the first module I have no problem with i18n as it is enters to the module 1 with argument i18n and translation works well in registerUI(based on recommendation here). But my problem is with the UI of module 2 (M2UI) which this function itself called within the server of module 1 (register) to return a modal dialogue. But i18n not detected and translation not works on displayed new modal. Any suggestions why this happens? thanks in advance...

I edited my example and it is now completely reproducible. Translation csv files are available here. Just copy them in "translations" forlder. And, the modules should be copied to "modules" folder.

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

回答1:


Did you try the latest dev version? You have to update it through the dev package. I am pretty sure it was fixed there. The problem regarded a missing callback through shiny session.

Your exampe works on my setup.



来源:https://stackoverflow.com/questions/65452049/shiny-i18n-not-translate-the-modal-dialoge-rendered-within-module

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