Search code examples
rutf-8shinymultilingual

Strings containing an accent do not appear


I'm currently building a shiny application that needs to be translated in different languages. I have the whole structure but I'm struggling into getting values such as "Validació" that contain accents.

The structure I've followed is the following:

  • I have a dictionary that is simply a csv with the translation where there's a key and then each language. The structure of this dictionary is the following:
key, cat, en
"selecció", "selecció", "Selection"
"Diferències","Diferències", "Differences"
"Descarregar","Descarregar", "Download"
"Diagnòstics","Diagnòstics", "Diagnoses"

  • I have a script that once the dictionary.csv is modified, generates a .bin file that later will be loaded in the code.

  • In strings.R I have all the strings that will appear on the code and I use a function to translate the current language to the one I want. The function is the following:

Code:

tr <- function(text){    
  sapply(text, function(s) translation[[s]][["cat"]], USE.NAMES=F) 
}

When I translate something, since I am doing in another file, I assign it to another variable something like:

str_seleccio <- tr('Selecció) 

  • The problem I'm facing is that for example if we translate 'Selecció' would be according to this function, tr('Selecció') and provides a correct answer if I execute it in the RStudio terminal but when I do it in the Shiny application, appears to me as a NULL. If the word I translate has no accents such as "Hello", tr("Hello") provides me a correct answer in the Shiny application and I can see it throught the code.

So mainly tr(word) gets the correct value but when assigning it "loses the value" so I'm a bit lost how to do it.

I know that you can do something like Encoding(str_seleccio) <- "UTF-8" but in this case is not working. In case of plain words it used to do but since when I asssign it, gets NULL is not working.

Any idea? Any suggestion? What I would like is to add something to tr function

The main idea comes from this repository that if you can take a look is the simplest version you can do, but (s)he has problem with utf-8 also.

https://github.com/chrislad/multilingualShinyApp


Solution

  • As in http://shiny.rstudio.com/articles/unicode.html suggested (re)save all files with UTF-8 encoding.

    Additionaly change within updateTranslation.R:

    translationContent <- read.delim("dictionary.csv", header = TRUE, sep = "\t", as.is = TRUE)
    

    to:

    translationContent <- read.delim("dictionary.csv", header = TRUE, sep = "\t", as.is = TRUE, fileEncoding = "UTF-8").

    Warning, when you (re)save ui.R, your "c-cedilla" might get destroyed. Just re-insert it, in case it happens.

    Happy easter :)