Appsilon / shiny.i18n

Shiny applications internationalization made easy
https://appsilon.github.io/shiny.i18n/
Other
168 stars 38 forks source link

How to translate selectInput ? Getting error names vector error #60

Closed Miracle-90 closed 3 years ago

Miracle-90 commented 3 years ago

Hello! I wanna translate selectInput in my shiny app,but guess i18n$t('my text for translate') is returning list with 3 elements theregore i am getting error in 'choices' pls help. Tried setNames no result. Code example is below tabsetPanel(id = "tabs", type = "tabs", tabPanel(i18n$t("Population density and ethnic composition of the population"),value = 1, selectInput( inputId = "variant1", label = "", width = "500px", choices =setNames(c('pdregion'),i18n$t("Population density of the region")) )),

jakubsob commented 3 years ago

Hello @Miracle-90! Thanks for using the package and submitting an issue. The reason why you're getting 3 elements is that browser side translation wraps translated elements with a span tag with information on currently selected language. Out-of-the-box shiny::selectInput doesn't allow you to use html tags as choices. In this case you could either use server side translation or use shinyWidgets::pickerInput which allows you to do so. Please check out the following example with both approaches to see whether this solves your issue.

library(shiny)
library(shiny.i18n)
library(shinyWidgets)
i18n <- Translator$new(translation_csvs_path = ".")
# Set translation language of your choice
i18n$set_translation_language("pl")

ui <- fluidPage(
  usei18n(i18n),
  tabsetPanel(
    id = "tabs",
    type = "tabs",
    tabPanel(
      "Browser side translation",
      value = 1,
      pickerInput(
        inputId = "browser_translation",
        label = "", 
        choices = c("pdregion"),
        width = "500px",
        choicesOpt = list(
          content = c(as.character(i18n$t("Population density of the region")))
        )
      )
    ),
    tabPanel(
      "Server-side translation",
      value = 2,
      uiOutput("tab_content")
    )
  )
)

server <- function(input, output, session) {
  output$tab_content <- renderUI({
    selectInput(
      inputId = "server_translation",
      label = "",
      width = "500px",
      choices = setNames(c('pdregion'), i18n$t("Population density of the region"))
    )
  })
}

shinyApp(ui = ui, server = server)

With a translation dictionary being:

en,pl
Population density of the region,Y