Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translation for radioButton() choices and selectInput() #54

Closed
mcsiple opened this issue Dec 23, 2020 · 5 comments
Closed

Translation for radioButton() choices and selectInput() #54

mcsiple opened this issue Dec 23, 2020 · 5 comments

Comments

@mcsiple
Copy link

mcsiple commented Dec 23, 2020

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.

@mehdihadi
Copy link

mehdihadi commented Dec 27, 2020

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",""

@dokato
Copy link
Contributor

dokato commented Dec 28, 2020

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.

@mcsiple
Copy link
Author

mcsiple commented Dec 29, 2020

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

@mcsiple mcsiple closed this as completed Dec 29, 2020
@ainhoavega
Copy link

ainhoavega commented Oct 8, 2021

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!

@mickeykawai
Copy link

mickeykawai commented Jan 2, 2023

@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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants