Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is_english not catching non-English machine #879

Closed
MichaelChirico opened this issue May 9, 2019 · 9 comments
Closed

is_english not catching non-English machine #879

MichaelChirico opened this issue May 9, 2019 · 9 comments
Labels
bug an unexpected problem or unintended behavior skip

Comments

@MichaelChirico
Copy link
Contributor

R
R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)

R は、自由なソフトウェアであり、「完全に無保証」です。 
一定の条件に従えば、自由にこれを再配布することができます。 
配布条件の詳細に関しては、'license()' あるいは 'licence()' と入力してください。 

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

'demo()' と入力すればデモをみることができます。 
'help()' とすればオンラインヘルプが出ます。 
'help.start()' で HTML ブラウザによるヘルプがみられます。 
'q()' と入力すれば R を終了します。 

With info:

R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] C/UTF-8/C/C/C/C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.5.2

Now for the testthat part:

library(testthat)
testthat:::is_english()
# [1] TRUE
expect_error(is_english(), 'could not find function')
#  エラー: `is_english()` threw an error with unexpected message.
# Expected match: "could not find function"
# Actual message: " 関数 \"is_english\" を見つけることができませんでした "

i.e., since is_english() will evaluate to TRUE in skip_if_translated(), it won't be possible to skip the gettext-produced error found here.

@MichaelChirico
Copy link
Contributor Author

Proposal -- replace is_english() with

tryCatch(get(x, envir = new.env()), error = identity)$message == "object 'x' not found"

@hadley
Copy link
Member

hadley commented May 27, 2019

Can you please provide the results of Sys.getenv("LANGUAGE")? I'd rather figure out why you're getting translations.

@MichaelChirico
Copy link
Contributor Author

MichaelChirico commented May 28, 2019

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:

Screen Shot 2019-05-28 at 10 35 38 AM

@hadley
Copy link
Member

hadley commented May 28, 2019

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

@hadley
Copy link
Member

hadley commented May 28, 2019

@MichaelChirico
Copy link
Contributor Author

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

@hadley hadley added bug an unexpected problem or unintended behavior skip labels Jul 15, 2019
@hadley
Copy link
Member

hadley commented Jul 18, 2019

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:/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, Rdatatable/data.table#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).

@hadley
Copy link
Member

hadley commented Jul 18, 2019

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"))
}

@MichaelChirico
Copy link
Contributor Author

MichaelChirico commented Jul 18, 2019

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:/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 :\

@hadley hadley closed this as completed in 8bcc8a7 Jul 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior skip
Projects
None yet
Development

No branches or pull requests

2 participants