[Rd] translation domain is not inferred correctly from a package's print methods -- intended behavior?

Michael Chirico m|ch@e|ch|r|co4 @end|ng |rom gm@||@com
Mon Jul 12 23:21:14 CEST 2021


Here is a reprex:

# initialize reprex package
cd /tmp
mkdir myPkg && cd myPkg
echo "Package: myPkg" > DESCRIPTION
echo "Version: 0.0.1" >> DESCRIPTION
mkdir R
echo "print.my_class = function(x, ...) { cat(gettext(\"'%s' is
deprecated.\"), '\n', gettext(\"'%s' is deprecated.\",
domain='R-myPkg'), '\n') }" > R/foo.R
echo "S3method(print, my_class)" > NAMESPACE
# extract string for translation
Rscript -e "tools::update_pkg_po('.')"
# add dummy translation
msginit -i po/R-myPkg.pot -o po/R-ja.po -l ja --no-translator
head -n -1 po/R-ja.po > tmp && mv tmp po/R-ja.po
echo 'msgstr "%s successfully translated"' >> po/R-ja.po
# install .mo translations
Rscript -e "tools::update_pkg_po('.')"
# install package & test
R CMD INSTALL .
LANGUAGE=ja Rscript -e "library(myPkg); print(structure(1, class = 'my_class'))"
#  '%s' は廃止予定です
#  %s successfully translated

Note that the first gettext() call, which doesn't supply domain=,
returns the corresponding translation from base R (i.e., the output is
the same as gettext("'%s' is deprecated.", domain="R-base")).

The second gettext() call, where domain= is supplied, returns our
dummy translation, which is what I would have expected from the first
execution.

Here is what's in ?gettext:

> 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". Otherwise there is no default domain.

Does that mean the S3 print method is not "in the namespace of myPkg"?
Or is there a bug here?

If the former, is the edge case of concern here just S3 methods where
the "top level" S3 method is defined in another package? Can we refine
the manual text wording here to be more clear about when we should
expect we need to supply domain= vs have it set automatically?



More information about the R-devel mailing list