Nicola Rennie | Say Hello! to Multilingual Shiny Apps | Posit (2022)
Multilingual shiny apps are not straightforward to build. Translation affects almost every single aspect of an app. Although there are a few packages designed to automate the translation process, they tend to only work for the most widely spoken languages. Using a bilingual English-Welsh shiny app we developed to present public health data as a case study, this talk will discuss: • how we built a multilingual shiny app; • how translation affected design decisions; • how we overcame the main issues faced; • and most importantly, what we'd do differently next time. By the end of this talk, you will have a better understanding of how to translate your Shiny app to help you to share your app with a much wider audience. Talk materials are available at https://nrennie.rbind.io/talks/2022-july-rstudio-conf/ Session: Lightning Talks
image: thumbnail.jpg
Transcript#
This transcript was generated automatically and may contain errors.
Hi, I'm Nicola, and I want to talk to you about translating your Shiny app into multiple languages. Some of you might be disappointed because when I say multiple languages in Shiny apps, I'm talking about spoken languages, not programming languages today. Since I'm basically going to spend the next couple of minutes telling you how annoying it was to develop a Shiny app in multiple languages, I thought I'd start by telling you why it's actually a worthwhile thing to do.
The vast majority of websites and apps, not just Shiny apps, are developed in English. The problem with this is that the vast majority of people don't speak English. Only around 2 in 11 people actually speak English, and that's a large number of people that automatically exclude from interacting with your Shiny app. Language selectors are becoming more commonplace in traditional web development, and we want to make sure that Shiny apps don't get left behind.
Language selectors are becoming more commonplace in traditional web development, and we want to make sure that Shiny apps don't get left behind.
Challenges of multilingual Shiny apps
Having said that, translating Shiny apps into multiple languages is not that straightforward to do. What I want to talk about today is some of the small issues that cropped up when we were developing a bilingual Shiny app to present public data. Translation affects every single component of your Shiny app. Simple UI elements suddenly became reactive expressions that relied on a user input. There are a couple of packages out there that help you to automate the translation process, but what we found is that those packages typically only work for a few of the most widely spoken languages, and that their built-in dictionaries typically didn't include domain-specific terms. So when we were developing our bilingual app, we built the translation capabilities directly into the app itself.
And during the development process, there were a couple of seemingly small problems that suddenly became much bigger problems to face. And one of those problems was what we thought was easy, ordering a factor for a bar chart. Most of you have probably come across the fact that R, in the absence of other instructions, will automatically sort your character vectors and your factors alphabetically. However, this doesn't always make sense. So in this example here, we have age group encodings as character strings, and we might want to sort them from young to old because that's easier for a user to interpret. So for a single language, this is really easy, right? We just create a factor and we explicitly set the factor level. And we get a nicely ordered bar chart.
Solving factor ordering across languages
The problem is that when we start doing this with multiple languages, it's not quite so easy. First of all, we can't rely on this alphabetical ordering at all anymore. If we sort character vector in one language and we sort it in another, actually they're still in different orders now. So we can't rely on alphabetical ordering at all. We also don't want to have to manually specify factor levels for all languages. It's a little bit annoying if you have a few words in two languages, but if you have hundreds or thousands of words in multiple languages, it's just completely unmanageable.
So to solve this problem for our app, what we did was we ended up using a sort of default or primary language for everything related to data processing. So in our case, we did everything in English and we explicitly set these factor levels in English only. For our bar chart example, what that meant was essentially creating two different functions. One function to compute the break point for the x axis. Again, this is done in English only based on our dataset. We then created a second function to compute the labels based on the output from that breaks function and from the user's choice of language. Essentially, we wanted to translate as little as possible.
So I hope the last few minutes have convinced you that translating Shiny app, although it's not straightforward, it is a very useful and worthwhile thing to do. What we found our sort of biggest lesson was that when we were building this app, translation was always the first problem that we actually had to think about if we wanted to add something new. But to keep our app actually efficient, it was always the last one. So I hope it's been useful. Find me on Twitter if you want to ask questions.