rstudio / rsconnect

Publish Shiny Applications, RMarkdown Documents, Jupyter Notebooks, Plumber APIs, and more

Home Page:http://rstudio.github.io/rsconnect/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Need multiple locales when deployed to shinyapps.io

bjcarothers opened this issue · comments

I've built an app where users have the option to generate reports in English, Spanish, and Portuguese. The reports have a time stamp at the top that relies on Sys.setlocale and format to translate and display the date properly depending on the selected language. (E.g. "02 março, 2023" for Portuguese.) Works locally, but not when deployed.

Stripped down app.R:

library(shiny)
library(dplyr)
library(tools) # cap functions

ui <- fluidPage(
  selectInput(
    inputId="langSel",
    label="Select Lanugage",
    choices=c("English"="en_US",
              "Spanish"="es_MX",
              "Portuguese"="pt_BR"),
    selected="en_US"
  ),
  textOutput("repDateOut")
)

server <- function(input, output, session) {
  
  timeStamp <- "2023-03-02"
  
  # set locale depending on language selection to translate month
  observeEvent(input$langSel,{
    Sys.setlocale("LC_TIME", paste0(input$langSel,".UTF-8"))
  })
  
  repDate <- reactive({
    dateOrder <- if (input$langSel=="en_US"){
      '%B %d, %Y' # month before day for English
    } else {
      '%d %B, %Y' # day before month for everything else
    }
    fmDate <- format(as.Date(timeStamp), dateOrder) %>%
      {if (input$langSel=="en_US") toTitleCase(.) else .} # cap month for English only
    fmDate
  })
  
  # print timestamp
  output$repDateOut <- renderText({repDate()})
  
}

shinyApp(ui = ui, server = server)

Following the advice here about making non-local locales available in rsconnect before deploying, I've tried running a few things in the console to deploy the app:

Attempt 1:

options(rsconnect.locale.cache = FALSE, rsconnect.locale = "en_US.UTF-8")
options(rsconnect.locale.cache = FALSE, rsconnect.locale = "pt_BR.UTF-8")
options(rsconnect.locale.cache = FALSE, rsconnect.locale = "es_MX.UTF-8")
rsconnect::deployApp()

Attempt 2:

options(rsconnect.locale.cache = FALSE, rsconnect.locale = "en_US.UTF-8")
options(rsconnect.locale = "pt_BR.UTF-8")
options(rsconnect.locale = "es_MX.UTF-8")
rsconnect::deployApp()

Attempt 3:

options(rsconnect.locale.cache = FALSE, rsconnect.locale = "en_US.UTF-8", rsconnect.locale = "pt_BR.UTF-8", 
rsconnect.locale = "es_MX.UTF-8")
rsconnect::deployApp()

In each case, only the Spanish locale (last one specified) works, and I get the following errors in the log:

OS reports request to set locale to "pt_BR.UTF-8" cannot be honored

OS reports request to set locale to "en_US.UTF-8" cannot be honored

Is it possible to make all three available in shinyapps.io?

(Apologies for earlier crospostings here and here, but it didn't get any takers.)

While we might want to somehow fix this on shinyapps, there is a fix that's available now (and is also more reliable than relying system locales): use a date-time package that has different locales built in. For example, here's an example using the clock package:

library(clock)

date <- as.Date("2023-01-02")

date_format(date, format = "%d %B %Y", locale = clock_locale("en"))
#> [1] "02 January 2023"
date_format(date, format = "%d %B %Y", locale = clock_locale("es"))
#> [1] "02 enero 2023"
date_format(date, format = "%d %B %Y", locale = clock_locale("pt"))
#> [1] "02 janeiro 2023"

Created on 2024-01-16 with reprex v2.0.2.9000

(I don't know if the country specifiers are important for your use case, although a little research suggests that at least Spain and Latin America use the same month names, you can also use clock_labels() to provide your own names.)

Yep, that did it. We don't need the country specifiers yet, but good to know about clock_labels() if we need it in the future. Thanks!