Closed MichaelChirico closed 5 years ago
Proposal -- replace is_english()
with
tryCatch(get(x, envir = new.env()), error = identity)$message == "object 'x' not found"
Can you please provide the results of Sys.getenv("LANGUAGE")
? I'd rather figure out why you're getting translations.
It's ""
.
Here's Sys.getenv()
:
Apple_PubSub_Socket_Render
/private/tmp/com.apple.launchd.UyX2pzrQyD/Render
COLUMNS 286
DISPLAY /private/tmp/com.apple.launchd.nVoqtK5YL2/org.macosforge.xquartz:0
DYLD_FALLBACK_LIBRARY_PATH
/Library/Frameworks/R.framework/Resources/lib:/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/lib/server
EDITOR vi
HOME /Users/michael.chirico
LC_CTYPE UTF-8
LINES 54
LN_S ln -s
LOGNAME michael.chirico
MAKE make
PAGER /usr/bin/less
PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
PWD /Users/michael.chirico/github/data.table
R_ARCH
R_BROWSER /usr/bin/open
R_BZIPCMD /usr/bin/bzip2
R_DOC_DIR /Library/Frameworks/R.framework/Resources/doc
R_GZIPCMD /usr/bin/gzip
R_HOME /Library/Frameworks/R.framework/Resources
R_INCLUDE_DIR /Library/Frameworks/R.framework/Resources/include
R_LIBS_SITE
R_LIBS_USER ~/Library/R/3.5/library
R_PAPERSIZE a4
R_PDFVIEWER /usr/bin/open
R_PLATFORM x86_64-apple-darwin15.6.0
R_PRINTCMD lpr
R_QPDF /Library/Frameworks/R.framework/Resources/bin/qpdf
R_RD4PDF times,inconsolata,hyper
R_SESSION_TMPDIR /var/folders/mc/zjbjjvv979zfyqzb5gsg8zt40000gn/T//Rtmp7I3fmg
R_SHARE_DIR /Library/Frameworks/R.framework/Resources/share
R_SYSTEM_ABI osx,gcc,gxx,gfortran,?
R_TEXI2DVICMD /usr/local/bin/texi2dvi
R_UNZIPCMD /usr/bin/unzip
R_ZIPCMD /usr/bin/zip
SED /usr/bin/sed
SHELL /bin/bash
SHLVL 1
SSH_AUTH_SOCK ###
TAR /usr/bin/tar
TERM xterm-256color
TERM_PROGRAM Apple_Terminal
TERM_PROGRAM_VERSION 404.1
TERM_SESSION_ID 52A3D43C-0591-40EA-B1E9-9E6F477656ED
TMPDIR /var/folders/mc/zjbjjvv979zfyqzb5gsg8zt40000gn/T/
USER michael.chirico
XPC_FLAGS 0x0
XPC_SERVICE_NAME 0
__CF_USER_TEXT_ENCODING
0x1F5:0x0:0x64
Screencap of Language & Region
:
The preferred language for messages is by default taken from the locale. This can be overridden first by the setting of the environment variable LANGUAGE and then by the environment variables LC_ALL, LC_MESSAGES and LANG. (The last three are normally used to set the locale and so should not be needed, but the first is only used to select the language for messages.) The code tries hard to map locales to languages, but on some systems (notably Windows) the locale names needed for the environment variable LC_ALL do not all correspond to XPG language names and so LANGUAGE may need to be set. (One example is ‘LC_ALL=es’ on Windows which sets the locale to Estonian and the language to Spanish.) --- https://cran.r-project.org/doc/manuals/r-release/R-admin.html#Localization-of-messages
But you don't have LANG
or LANGUAGE
set. Can you please try Sys.getlocale("LC_MESSAGES")
? Otherwise you might need to ask on R-devel
The source itself (I think), is at https://github.com/wch/r-source/blob/5a156a0865362bb8381dcd69ac335f5174a4f60c/src/extra/intl/dcigettext.c#L1508-L1520
Sys.getlocale("LC_MESSAGES")
# [1] "C"
I also poked around the gettext
manual for some help, mainly around "Setting the POSIX Locale", to no avail:
https://www.gnu.org/software/gettext/manual/gettext.html#Setting-the-POSIX-Locale
After reading the source, I think what happens is that if LANG
etc are unset, then R uses the locale provided by the operating system, but provides no way to access that value. I poked around a bit, and I think implementing our own equivalent is going to be a lot of work.
The problem with using your tryCatch()
idea is that that specific error message might not be translated, but other messages still could be (for example, the built-in Danish translation translates the error for not_present()
but not "a" / 1
).
This makes me think that skip_if_translated()
is fundamentally ill-founded as translation is not a property of the session, but of individual messages. I think this suggests that skip_if_translated()
should be deprecated (which seems like it shouldn't cause widespread problems because no CRAN package currently uses it: https://github.com/search?q=org%3Acran+skip_if_translated&type=Code)
But what should we suggest instead? I think the context is usually checking that your code throws the expected base R error (e.g. #565, https://github.com/Rdatatable/data.table/issues/3039), so I think the best thing is to probably use gettext()
to translate the error message by hand:
msg <- sprintf(gettext("'%s' not found", domain = "R"), "is_english")
expect_error(is_english(), msg)
I don't see any obvious way to automate this, so I think all we can do is document.
I wonder if R-core might be persuaded to add a new field to base conditions that would contain the untranslated error message? Alternatively, a class hierarchy for conditions could also solve the same problem in a different way (since you'd be able to test for the class rather than the error message text).
Actually, maybe skip_if_translated()
just needs an argument:
skip_if_translated <- function(message = "'%s' not found") {
if (gettext(message, domain = "R") == message) {
return(invisible(TRUE))
}
skip(paste0("'", message, "' is translated"))
}
We wound up doing the following in data.table
. It's not a fix, instead we opted to just surface to those running tests that some tests might not return the correct result:
https://github.com/Rdatatable/data.table/blob/master/R/test.data.table.R#L65-L72
txt = eval(parse(text="tryCatch(mean(not__exist__), error = function(e) e$message)"), envir=.GlobalEnv)
foreign = txt != "object 'not__exist__' not found"
if (foreign) {
# nocov start
cat("\n**** This R session's language is not English. Each test will still check that the correct number of errors and/or\n",
"**** warnings are produced. However, to test the text of each error/warning too, please restart R with LANGUAGE=en\n\n", sep="")
# nocov end
}
And unfortunately, my machine that led to this issue was recently decommissioned so I'm afraid I won't be of help to confirm any fix :\
With info:
Now for the
testthat
part:i.e., since
is_english()
will evaluate toTRUE
inskip_if_translated()
, it won't be possible to skip thegettext
-produced error found here.