Appsilon / shiny.i18n

Shiny applications internationalization made easy
https://appsilon.github.io/shiny.i18n/
Other
168 stars 38 forks source link

UTF encoding and special characters during golem deployment #66

Closed mcsiple closed 3 years ago

mcsiple commented 3 years ago

Hi Appsilon developers! I built an app using golem because I hope to publish it as an R package.

I am using live translation in the app via radioButtons. The .json file with my translations contains special characters. When I deploy the app from the R package to shinyapps.io , the version that publishes to the server does not display these special characters at all (notice how the í is missing from "vaya aquí"): Screen Shot 2021-08-25 at 6 18 15 PM

Special characters included in the app_ui.R file render as UTF on the server: Screen Shot 2021-08-25 at 6 17 26 PM

I am not sure if this is a golem issue or a shiny.i18n issue, but I am posting here because I am guessing other shiny.i18n users have similar issues. I have to keep both types (the .json contents and the in-app special characters) in order to allow translation for the radioButton options and the plots.

Here is the repo for the R package (everything works perfectly when the app is deployed from the package): https://github.com/mcsiple/mmrefpoints Here is the server version that deploys automatically when I use golem::add_shinyappsio_file(); rsconnect::deployApp(): https://msiple.shinyapps.io/mmrefpoints/

If I should post this on the golem github page instead, just let me know. And thank you in advance for your help.

mcsiple commented 3 years ago

I just noticed that this problem is specific to areas where I have chunks of raw HTML and where I have special characters in plot labels, so I don't think this is a shinyi18.n issue. I will leave it up for now just in case others have run into the same issue but am fairly sure the fix will not require any changes on your part.

mcsiple commented 3 years ago

I fixed this with an update where I saved all the .R files usign UTF-8 encoding (that was the system default, but I did it again anyway) and changed the raw HTML translations (accessed with switch() for use in the splash page) I converted UTF-8 characters to HTML, e.g., exploración --> exploración. Thank you, and hopefully someone else will benefit from this little code adventure.

owen-cho commented 2 years ago

Thanks @mcsiple , it was indeed helpful. So you used switch function inside your server instead of shiny.i18n translator. I managed to get it working this way, but wonder if you've done the same. Thanks!

mcsiple commented 2 years ago

Hi @owen-cho indeed, this is what I used. Here's an example code chunk for what those HTML pieces look like in my code:

In the constants file that loads before the app runs:

splash_es <- "<div class='jumbotron'>
  <h2>Bienvenido a la herramienta de exploraci&oacute;n de impactos de la captura incidental de mam&iacute;feros marinos</h2>
</div>"

In the server code:

 output$splash <- renderUI({
    htmltools::HTML(
      switch(input$selected_language,
        "en" = splash_en,
        "es" = splash_es,
        "fr" = splash_fr
      )
    )
  })

I hope this helps!

owen-cho commented 2 years ago

Thank you @mcsiple I did pretty much the same:

library(shiny)

ui <- fluidPage(
  actionButton("btn", "Welcome"),
  selectInput("lang", NULL, c("en", "kr"))
)

server <- function(input, output, session) {
  observeEvent(input$lang, {
    label <- switch(
      input$lang,
      en = "Welcome",
      kr = HTML("&#xd658;&#xc601;&#xd569;&#xb2c8;&#xb2e4;")
    )
    updateActionButton(inputId = "btn", label = label)
  })
}

shinyApp(ui = ui, server = server)