Appsilon/shiny.i18n

Example of Update_lang for Automatic Translation

Opened this issue · 4 comments

hdbt commented

Hi there,

I was trying to get the automatic translation to work with the update_lang live example.
Although it is possible to initate the translation, it wont display in labels. working with "i18n$t()" works perfectly fine.

Isit maybe not supported (yet)?

EDIT: oh sorry, I misread your message, you meant for Automatic Translation. Indeed this is not something that we tested thoroughly yet. If could post some minimal example with the error, or unexpected behaviour, we could help to debug further. Thanks!

In general, in-browser translations with update_lang add span tag to each character verctor, thus some UI elements might not accept that. In that case we recommend regenerate the whole object with the server-side translation:
https://github.com/Appsilon/shiny.i18n/blob/master/examples/live_language_change/server_app.R

hdbt commented

I am sorry for the vague terminology and description of the problem. Thank you for the clarification; I thought I might have missed something obvious.

Following your request, I took shiny.i18n/examples/live_language_change/browser_app.R and replaced the functions i18n$t() with i18n$at().

Automatic Translation works perfectly if you set up the language key beforehand, but it wont change in a live_language_change scenario.


#' This script demonstrates how to use shiny.i18n Translator object
#' for live language change on the UI side. Two key steps are:
#' (a) add `usei18n(i18n)` to UI
#' (b) use `update_lang` function to change the language in session

library(shiny)
library(shiny.i18n)

# File with translations
i18n <- Translator$new(automatic = T)
i18n$set_translation_language("en") # here you select the default translation to display

ui <- fluidPage(
    #
    div(style = "float: right;",
        selectInput('selected_language',
                    i18n$at("Change language"),
                    choices = c("de","en"),
                    selected = i18n$get_key_translation())
    ),
    titlePanel(i18n$at("Hello Shiny!"), windowTitle = NULL),
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        i18n$at("Number of bins:"), # you use i18n object as always
                        min = 1,
                        max = 50,
                        value = 30)
        ),
        mainPanel(
            plotOutput("distPlot"),
            p(i18n$at("This is description of the plot."))
        )
    )
)

server <- function(input, output, session) {
    
    observeEvent(input$selected_language, {
        # This print is just for demonstration
        print(paste("Language change!", input$selected_language))
        # Here is where we update language in session
        shiny.i18n::update_lang(session, input$selected_language)
    })
    
    output$distPlot <- renderPlot({
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        hist(x, breaks = bins,
             col = "darkgray", border = "white",
             main = i18n$at("Histogram of x"), ylab = i18n$at("Frequency"))
    })
}

shinyApp(ui, server)

Thank you @Hamidburg for reporting that. That's definitely a bug, we will have to investigate, hopefully, will come back with some solution soon.

The root cause of this bug is that browser side translation uses dictionaries that are passed to the browser during initialization. Automatic translation doesn't have dictionaries upfront so the translation doesn't work. API usage should be implemented on browser side.