Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of Update_lang for Automatic Translation #53

Open
hdbt opened this issue Dec 23, 2020 · 4 comments
Open

Example of Update_lang for Automatic Translation #53

hdbt opened this issue Dec 23, 2020 · 4 comments
Labels

Comments

@hdbt
Copy link

hdbt commented Dec 23, 2020

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)?

@dokato
Copy link
Contributor

dokato commented Dec 23, 2020

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
Copy link
Author

hdbt commented Dec 23, 2020

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)

@dokato
Copy link
Contributor

dokato commented Dec 23, 2020

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

@dokato dokato added the bug label Feb 9, 2021
@jakubsob jakubsob assigned jakubsob and unassigned jakubsob Apr 6, 2021
@jakubsob
Copy link

jakubsob commented Apr 9, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants