Appsilon/shiny.i18n

Translation for radioButton() choices and selectInput()

Closed this issue ยท 5 comments

Hi shiny.i18n team,

I know I have seen questions about this before but I can't find a solution -- I need to find a way to translate the choices in my selectInput() and radioButtons() inputs. If I try to wrap choice labels with i18n$t(), the app does not run. I don't know if this is a feature in the package yet, but if it isn't, would you mind offering some best practices for how people should get by in the meantime?

I am doing live translation and using uiOutput(page_content) in my UI to do automatic translation when the user selects a radio button with the language on it. I don't need to update the language selection radio buttons themselves, but I need it for other radioButton inputs and a dropdown SelectInput() with common names of species.

Thank you in advance for your help.

This was just my issue, I resolved this using following function. It works for me. Hope you find it useful too.

trF<-function(x){
a<- i18n$t(x)
a<-gsub( """ , "'", a, fixed = TRUE)
a<-noquote(a)
a<-gsub("'", '"', a, fixed = T)
a<-unlist(strsplit(a, split=","))
gsub(""", "", a, fixed = T)
}

use single quotation mark (') around x argument.
in your csv file the keywords should be like this:
"Adult/himself", "Teenager/older Child/himself","Parent on behalf of child",""

hey @mcsiple , because both radio button as select input are dynamic elements, they need to be updated with respective update functions from shiny on the server-side (for instance updateSelectInput). Thus, in this example, I'd suggest using server-side translation. For example:

library(shiny)
library(shiny.i18n)

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

ui <- fluidPage(
  usei18n(i18n),
  h1(i18n$t("Hello")),
  actionButton("change", i18n$t("Change language")),
  radioButtons("radio", i18n$t("Radio"), c("one", "two")),
  selectInput("select", i18n$t("Choose"), c("one", "two", "three"))
)

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

  i18n_r <- reactive({
    i18n
  })

  observeEvent(input$change, {
    lang <- ifelse(as.numeric(input$change) %% 2, "pl", "en")
    shiny.i18n::update_lang(session, lang)
    i18n_r()$set_translation_language(lang)
  })

  observe({
    updateRadioButtons(session, "radio", label = i18n_r()$t("Radio"),
                       choices = i18n_r()$t(c("one", "two")))
    updateSelectInput(session, "select", label = i18n_r()$t("Choose"),
                      choices = i18n_r()$t(c("one", "two", "three")))
  })
}
shinyApp(ui, server)

Note that I create a reactive variable i18n_r() from our translation object. LMK if this example is not clear.

Success! It worked. Thank you @dokato , this is great.

Hi! This works fine for unnamed choices, but what about named ones? I want to translate the names of the choices but not the actual choice itself, e.g. my choices are c("INFORMATION" = "info", "EVOLUTION" = "evol") and I want to translate INFORMATION and EVOLUTION but not info and evol. The above code does not work in that case and I can't manage to make it work. Any suggestions? Thanks!

@ainhoavega :

I solved as follows.

library(shiny)
library(shiny.i18n)

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

choices_select <- c("one", "two", "three")

ui <- fluidPage(
  usei18n(i18n),
  h1(i18n$t("Hello")),
  actionButton("change", i18n$t("Change language")),
  radioButtons("radio", i18n$t("Radio"), c("one", "two")),
  selectInput("select", i18n$t("Choose"), c("one", "two", "three")),
  verbatimTextOutput("o_select")
)

server <- function(input, output, session) {
  
  i18n_r <- reactive({
    i18n
  })
  
  observeEvent(input$change, {
    lang <- ifelse(as.numeric(input$change) %% 2, "pl", "en")
    shiny.i18n::update_lang(session, lang)
    i18n_r()$set_translation_language(lang)
  })
  
  
  update_choices_select <- reactive({
    names(choices_select) <- i18n_r()$t(choices_select)
    # print(choices_select)
    choices_select
  })
  
  observe({
    updateRadioButtons(session, "radio", label = i18n_r()$t("Radio"),
                       choices = i18n_r()$t(c("one", "two")))
    updateSelectInput(session, "select", label = i18n_r()$t("Choose"),
                      choices = update_choices_select())
                      #choices = i18n_r()$t(update_choices_select()))
                      #choices = i18n_r()$t(c("one", "two", "three")))
  })
  
  output$o_select <- renderText({ 
    input$select
  })
}
shinyApp(ui, server)