Appsilon / shiny.i18n

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

Compatibility with selectInput #27

Closed emilmahler closed 4 years ago

emilmahler commented 4 years ago

Does shiny.i18n have compatibility with selectInput? Here I have an example where the label choices and values are different:

selectInput(ns('label_id'), label=i18n$t('My label'), choices=c(i18n$t('Transformers') = 'Transformere', i18n$t('Cable closets') = 'Kabelskabe'), selected = c('Transformers','Cable closets'), multiple = T, selectize = TRUE)

It throws a poorly defined error at the equal sign in the first choice/value equal sign in i18n$t('Transformers') = 'Transformere'. Is there a work around for this?

dokato commented 4 years ago

Good example, let me have a closer look at that and I'll come back shortly.

krystian8207 commented 4 years ago

@emilmahler try this:

c('Transformere', 'Kabelskabe') %>% stats::setNames(c(i18n$t('Transformers'), i18n$t('Cable closets')))
emilmahler commented 4 years ago

@krystian8207 this works, thanks!

mcsiple commented 3 years ago

Has anyone tried this since last December? I just started revisiting the issue of live-translating selectInput chocies, and have not gotten this example to work so far. Here is the code I am using. The translation key is below the script. Hopefully this is a small problem that I have just missed.

When I try to run the app, I get the error: Error in names(object) <- nm : 'names' attribute [6] must be the same length as the vector [2]

library(tidyverse)
library(shiny)
library(shiny.i18n)

i18n <- Translator$new(translation_json_path = "translation.json")
i18n$set_translation_language("en")

# UI
ui <- 
   fluidPage(
      shiny.i18n::usei18n(i18n),
      titlePanel("Incremental plotting", windowTitle = NULL),
      sidebarLayout(
         sidebarPanel(
            radioButtons(
               inputId = "selected_language",
               label = "Language",
               choiceNames = c("English", "Deutsch"),
               choiceValues = i18n$get_languages(),
               selected = i18n$get_key_translation(),
               inline = FALSE
            ),
            selectInput('label_id',
                        label=i18n$t("My label"),
                        choices=c('Transformers', 'Cable closets') %>% stats::setNames(nm = c(i18n$t('Transformers'), i18n$t('Cable closets'))),
                        selected = c('Transformers','Cable closets'), 
                        multiple = T,
                        selectize = TRUE)),
         mainPanel(
            plotOutput("testPlot")
         )

      ) # /sidbarLayout
   ) #/fluidpage

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

   observeEvent(input$selected_language, {
      print(paste("Language change:", input$selected_language))
      update_lang(session, input$selected_language)
   })

   output$testPlot <- renderPlot({
      plot(1:20, 1:20)
   })
} #/server

shinyApp(ui = ui, server = server)

Here are the contents of the file translation.json :

{
  "cultural_date_format": "%d-%m-%Y",
  "languages": ["en", "de"],
  "translation": [
      {
    "en": "Transformers",
    "de": "Transformere"
      },
      {
      "en": "My label",
      "de": "Mein label"
      },
      {
      "en": "Cable closets",
      "de": "Kabelskabe"
      }
    ]
}
clemens-11 commented 2 years ago

Hello, have you already found a solution to this problem? Thank you very much!

KHwong12 commented 2 years ago

I run into the same error (i.e. Error in names(object) <- nm : 'names' attribute [3*X] must be the same length as the vector [X]) also.

I find following the guideline in #60 (comment) solves my problem. Basically, you put the widget in the server function and then warp it inside renderUI({}).