Internationalization of shiny apps has never been easier!
[UPDATE – December 2020] – The shiny.i18n package was updated recently. We decided to make a new hands-on tutorial demonstrating its capabilities. Explore what’s new with the shiny.i18n update.
Have you ever created a multilingual Shiny app? It is very likely that the answer is no because Shiny just doesn’t have any good tools for that. In Appsilon we came across the internationalization problem many times, so we decided to make a tool that makes life easier when it comes to multilingual.
shiny.i18n is the new kid on the block and still under rapid development, but the
0.1.0 version is already ready to go.
NOTE:shiny.i18n usage is not limited to Shiny apps. You can use it as standalone R package for generating multilingual reports or visualizations. We decided on this name because Shiny is the most common and obvious use-case scenario.
The latest version of the package (0.1.0) is released on CRAN, so you can simply get it like this:
To install the development version always check the GitHub project first.
On the GitHub page, you can also find more examples, read the documentation, post some issues or your own contributions.
Now it’s time to learn more about how to use shiny.i18n. The package utilizes specific translation file data formats. Currently, two approaches are supported.
JSON translation format
Example of a JSON translation file for English and Polish language you can find below:
It consists of a single file translation.json with two mandatory fields:
- “languages” with a list of all language codes;
- “translation” with a list of dictionaries assigning translation to a language code.
Other fields (such as cultural_date_format) are optional and if missing will be read from the default config yaml file.
CSV translation format
Another approach is to use a CSV format. The main idea behind it is to support distributed translation tasks among many translators. Instead of having to concatenate results of work from many translators together, we can just store them in a common folder with the specific name of the file: translation_<LANGUAGE-CODE>.csv.
You can imagine a situation with the following folder structure:
which have translations for Polish (pl), Italian (it), and – as the language code is completely arbitrary – kl for the Klingon language.
Let’s have a look at how a typical CSV translation file should look like:
This time we need to remember that all CSV files from one dictionary must share a common reference language in the left column – which is English (en) in the above case.
Creating the app
To integrate our translations with Shiny we start by loading packages and an example JSON file.
Having that, we can check in the RStudio console all languages stored in the i18n object.
Now within the Shiny app, we need to surround all text elements within i18n$translate or in short i18n$t method. For instance:
to translate a message displayed by
sliderInput element, or:
to translate a titlePanel content.
If we decide to run an instance of the app with a specific language (let’s say Klingon) we should call:
right after defining
Below you can see the full example:
Above code should convince you about how easy it is to start using shiny.i18n. For more examples once again I refer you to the GitHub page. We hope that shiny.i18n will help you to forget about problems with internationalization.
Bye, cześć, ciao, tschüs, 再见!