Internationalization of shiny apps has never been easier!
This article is originally published at https://appsilon.com
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 which makes a live easier when it comes to mulitlingual.
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 the package (0.1.0) will be released to CRAN (edit: it’s being currently reviewed, should be live at 14th of September, for now please use GitHub repository), so you can simply get it like this:
To install the development version always check the GitHub project first.
In 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 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 pl Polish, it Italian and – as the language code is completely arbitrary – kl for Klingon language.
Let’s have a look at how typical CSV translation file should look like:
This time we need to remember that all CSV files from one dictionary must share common reference language in the left column (which is en English in the above case).
Creating the app
To integrate our translations with Shiny we start from loading packages and an example JSON file.
Having that, we can check in RStudio console all languages stored in i18n object.
Now within 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, 再见!
Please visit source website for post related comments.