My organization has a few shiny apps running that are available in three languages, Icelandic, English and Polish. However, our dates are always formatted in our system default of is_IS.UTF-8 on shinyapps, as you can't change the system locale once your app has been deployed to shinyapps.
Is there a way to set localization within the format function; or basically any other way than changing the system locale?
Details:
On shinyapps.io you can't change the system locale once the app has been deployed (see here), but we can execute the following code locally and on unix servers:
#Icelandic
Sys.setlocale("LC_ALL", "is_IS.UTF-8")
#> [1] "is_IS.UTF-8/is_IS.UTF-8/is_IS.UTF-8/C/is_IS.UTF-8/is_IS.UTF-8"
format(as.Date("2020-09-09"), "%e. %B, %Y")
#> [1] " 9. september, 2020"
# Polish
Sys.setlocale("LC_ALL", "pl_PL.UTF-8")
#> [1] "pl_PL.UTF-8/pl_PL.UTF-8/pl_PL.UTF-8/C/pl_PL.UTF-8/is_IS.UTF-8"
format(as.Date("2020-09-09"), "%e. %B, %Y")
#> [1] " 9. września, 2020"
#English
Sys.setlocale("LC_ALL", "en_US.UTF-8")
#> [1] "en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/is_IS.UTF-8"
format(as.Date("2020-09-09"), "%e. %B, %Y")
#> [1] " 9. September, 2020"
Created on 2020-10-01 by the reprex package (v0.3.0)
Here's a reprex for an app that will work locally (assuming you have permissions to change the system locale - but this app will not work once deployed to shinyapps.
library(shiny)
ui <- fluidPage(
radioButtons("hnappur", "Veldu tungumál",
choices = c("Íslenska", "Pólska", "Enska"), selected = "Íslenska"),
textOutput("texti")
)
server <- function(input, output, session) {
observeEvent(input$hnappur, {
if (input$hnappur == "Íslenska") {
Sys.setlocale("LC_ALL", "is_IS.UTF-8")
output$texti <- renderText(format(as.Date("2020-09-09"), "%e. %B, %Y"))
} else if (input$hnappur == "Pólska") {
Sys.setlocale("LC_ALL", "pl_PL.UTF-8")
output$texti <- renderText(format(as.Date("2020-09-09"), "%e. %B, %Y"))
} else if (input$hnappur == "Enska") {
Sys.setlocale("LC_ALL", "en_US.UTF-8")
output$texti <- renderText(format(as.Date("2020-09-09"), "%e. %B, %Y"))
}
})
}
shinyApp(ui, server)