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