I want to translate my app from English into other languages. I have a data frame that has both the English and the translations. Rather than make 20 output variables to display text differently around the app, I wanted to make a vector of outputs then for each spot that I need text, to call the correct element from that vector to display. Here is a toy example of what I am talking about.
## data frame with translations
translation_df <- data.frame(English = c("Hello", "Goodbye"),
Spanish = c("Hola", "Adios"))
server <- function(input, output, session) {
output$distPlot <- renderPlot({
hist(rnorm(input$obs), col = 'darkgray', border = 'white')
})
## if English is selected return vector of English phrases, ditto for Spanish
output$txt <- renderUI({
if(input$language == "english")
return(translation_df[,1])
if(input$language == "spanish")
return(translation_df[,2])
})
}
ui <- fluidPage(
## I want it to say "Hello" here in the correct language
titlePanel(uiOutput("txt")[1]),
sidebarLayout(
sidebarPanel(
sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
## input to choose language
selectInput("language", "Language", choices = c("English" = "english", "Spanish" = "spanish")),
## I want it to say "Goodbye" here in the correct language
helpText(uiOutput("txt")[2])
),
mainPanel(plotOutput("distPlot"))
)
)
shinyApp(ui = ui, server = server)
What I have found is that I can return the entire vector or nothing. I have experimented with different outputs (ui, text, dataframe, etc) and with the placement of the [1] or [[1]] (within quotes, outside of quotes but within parentheses, and outside of parentheses), but nothing has worked so far. I'm pretty sure I need some HTML code to call from the vector, but I am not familiar with how to do so.