Can't get month names to appear in Spanish in shinyapps.io

Hi all,

I'm trying to render html text on a shinyapp which takes date inputs both from Sys.Date() and from a shiny dateInput(), and pastes it with input text onto a htmlOutput(). The idea is to then print all of this into a pdf.

Example code looks like this:

output_text<- function(input){
  paste(
    div(
      br(),
      p("Today is,", format(Sys.Date(), "%d de %B de %Y"), "and I am X"),
      br())}

ui <- navbarPage(
                 tabPanel(
                        fluidRow(
                                    htmlOutput("text"))))

server <- function(input, output, session) {
  output$text <- renderUI({HTML(output_text(input))})
  }

(I haven't run this example, as it's a short demo just to transmit the gist of the explicit problem)

I've looked into this, this, this, and gone through the rsconnect docs.

I've tried several approaches (separately and jointly):

  • Changed my system locale on my Windows 10 machine to spanish.
  • Changed locale in R with: Sys.setlocale("LC_TIME", "spanish")
  • Ran on console and set in script (separately): options(shinyapps.locale.cache = FALSE)
  • Ran on console before Sys.setlocale() and deployApp(): options(shinyapps.locale.cache = FALSE)

When I run the app locally, the html text prints the month name properly in spanish (e.g. "08 de Noviembre de 2019"), but when I push it to shinyapps.io (having tried different combinations of the above mentioned attempts, including changing my Windows locale to spanish), I still get the month name in English when running the app on shinyapps.io (e.g. "08 de November de 2019").

Can you please help me to solve this?

Thanks you.
Francis

2 Likes

Shinyapps.io runs on linux server, so changing the locale is a little different, this example works

library(shiny)
Sys.setlocale("LC_TIME", "es_ES")

output_text <- function(input){
    paste(
        div(
            br(),
            p("Today is,", format(Sys.Date(), "%d de %B de %Y"), "and I am X"),
            br()))
    }

ui <- htmlOutput("text")

server <- function(input, output, session) {
    output$text <- renderUI({HTML(output_text(input))})
}

shinyApp(ui = ui, server = server)

image

2 Likes

Thanks Andrés,

Great catch! I'd forgotten about that.

However, I'm still getting the months' names in English. I've tried the following:

  • Reset my Windows Administrator System Language and Region settings to spanish.
  • Before deploying, ran:
    options(rsconnect.locale.cache = FALSE)
  • At another attempt, before deploying ran:
    Sys.setenv("LANGUAGE" = "Spanish")

This is the output of rsconnect:::systemInfo()' locale relevant fields:

System.Locale Input.Locale Time.Zone Total.
es-co;Spanish (Colombia) es-mx;Spanish (Mexico) (UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna

This is the output of sessionInfo():

R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=Spanish_Colombia.1252 LC_CTYPE=Spanish_Colombia.1252 LC_MONETARY=Spanish_Colombia.1252 LC_NUMERIC=C LC_TIME=Spanish_Colombia.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached):
[1] compiler_3.6.1 rsconnect_0.8.15 tools_3.6.1 packrat_0.5.0

In my app's script I've also modified the following for congruence:
Sys.setlocale("LC_TIME", "es_CO")

Please help, I've been stuck on this for almost a week and can't get users to test the app and move the project forward.

Thanks again!

Sorry I can't reproduce your issue, the only thing it comes to my mind is to use a more common Spanish variant like es_ES or es_US, just in case the server doesn't have your specific variant installed.

Hi Andrés,

Modified my systems locale to "Spanish(United States)" and set R locale to:
Sys.setlocale("LC_TIME", "es_US")

But still getting month names in English.

As I need to keep advancing, I'll just handle the problem internally by scripting a simple translating function for month description from english to spanish.

For future reference on this topic, how would we go about for creating multi-lingual dashboards on shinyapp if locale has to be set at a system level (OS and/or R)?

Thanks for all,

Francis

Hi,

This is the solution I'm using for the time being, in case anyone runs into the same problem or wants to enable multi-lingual usage without having to modify system variables:

library(stringr)

english_months <- c("january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december")
spanish_months <- c("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre")

to_spanish_dict <- spanish_months
names(to_spanish_dict) <- english_months

translate_date <- function(date, output_lang = "es"){
  if(output_lang == "es"){
    str_replace_all(tolower(date), to_spanish_dict)
  }
}

(str_replace_all might sound like overkill for this example, but could come in handy for more complex cases).

To test it:
First set/verify that your locale is set to English. Then:

test <- format(Sys.Date(), "%d de %B de %Y")

[1] "12 de November de 2019"

translate_date(test)

[1] "12 de Noviembre de 2019"

I've included the parameter "output_lang" and set its default value to "es" (which maps to our spanish dictionary), but you can add as many dictionaries from English to any other language your users might need, add them inside if statements, and pass a different value to the parameter "output_lang".

Hope this helps.

Best,
Francis

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.