Use of gettext in a Shiny application

216 Views Asked by At

I noticed a strange behaviour of gettext in a Shiny application running on an Ubuntu 14.04 machine.

From ?gettext, we learn that:

If domain is NULL or "", and gettext or ngettext is called from a function in the namespace of package pkg the domain is set to "R-pkg".

But when I develop a package using gettext to perform the internationalization in an associated Shiny app, messages are not always translated.

I have developed a small reproducible example that you can find at Github :

devtools::install_github("tutuchan/gettext")

It is a very simple package with three functions:

  • hello() calls gettext("Hello, world!"), implicitly specifying the domain as R-gettext,
  • hello2() calls gettext("Hello, world!", domain = "R-gettext"), explicitly specifying the domain as R-gettext,
  • app() is a wrapper around shiny::runApp() to launch the app directly

The package includes the .po file for a French translation (which requires you to have a "fr_FR.UTF8" locale on your system to try it out).

If I call app(), both strings are translated:

app

But if I run the app directly, either by opening the file and clicking the Run App button in RStudio or by calling shiny::runApp("inst/app"), only the message with the domain called explicitly is translated:

enter image description here

I may have misunderstood the help from gettext but I'd be glad if anyone could shed some light on this.

Does gettext consider the top-level calling function when trying to match the domain ? Because in that case, it would make sense that when calling app(), all messages are translated while when calling shiny::runApp(), only those with explicit domains are translated (because for implicit domains, gettext would look for .po files in the namespace of the shiny package). But I figured it would look for the domain of the function actually calling gettext.

1

There are 1 best solutions below

0
On BEST ANSWER

It looks like gettext looks indeed in the namespace of the toplevel function for translation files when the domain is not specified.

I created another package that contains translation files and calls the app from my gettext package. When gettextpo::app() is called (which calls gettext::app() internally, the translation when the domain is implicit is found in the namespace of the gettextpo package, and not gettext.

enter image description here