From 0f99146fb196d70e90c959d57c1c092431427d6a Mon Sep 17 00:00:00 2001 From: maechler Date: Mon, 26 Aug 2024 14:50:33 +0000 Subject: [PATCH] do not warn but message, when Sys.setLanguage("en") is deficient in C locale git-svn-id: https://svn.r-project.org/R/trunk@87051 00db46b3-68df-0310-9c12-caf00c1e9a41 --- doc/NEWS.Rd | 3 ++- src/library/base/R/stop.R | 15 +++++++++------ src/library/base/man/gettext.Rd | 9 ++++++--- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/doc/NEWS.Rd b/doc/NEWS.Rd index 1e04c7445b7..4d63d8b253b 100644 --- a/doc/NEWS.Rd +++ b/doc/NEWS.Rd @@ -255,7 +255,8 @@ \PR{15027}. \item \code{Sys.setLanguage()} now works in an \command{LC_ALL=C R} session - on some platforms. + on some platforms, warns about \emph{some} failures to change the + language and gets an option related to these warning messages. \item Printing \code{ls.str()} now tries harder to show \code{""} even when \R's language setting is not English. diff --git a/src/library/base/R/stop.R b/src/library/base/R/stop.R index 3ca39985c65..b7583c45473 100644 --- a/src/library/base/R/stop.R +++ b/src/library/base/R/stop.R @@ -126,27 +126,30 @@ gettextf <- function(fmt, ..., domain = NULL, trim = TRUE) ## Could think of using *several* domains, i.e. domain = vector; but seems complicated; ## the default domain="R" seems to work for all of base R: {"R", "R-base", "RGui"} -Sys.setLanguage <- function(lang, unset = "en") +Sys.setLanguage <- function(lang, unset = "en", allowC4en = TRUE) { if (!capabilities("NLS") || is.na(.popath)) { warning(gettextf("no natural language support or missing translations"), domain=NA) - return(invisible("")) + return(invisible(structure("", ok = FALSE))) } stopifnot(is.character(lang), length(lang) == 1L, # e.g., "es" , "fr_CA" lang == "C" || grepl("^[a-z][a-z]", lang)) curLang <- Sys.getenv("LANGUAGE", unset = NA) # so it can be reset if(is.na(curLang) || !nzchar(curLang)) curLang <- unset # "factory" default - if(isC <- identical("C", Sys.getlocale())) { ## e.g. LC_ALL=C R on Linux + Warning <- if(allowC4en && startsWith(lang, "en")) message else warning + if(identical("C", Sys.getlocale()) && lang != "C") { ## e.g. LC_ALL=C R on Linux lcSet <- if(.Platform[["OS.type"]] == "unix") Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") - ## TODOs: 1) does en_US.UTF-8 always exist? 2) need to deal w/ Windows ? + ## TODOs: 1) does en_US.UTF-8 always exist? + ## 2) How to deal w/ Windows ? {can set things but with *no* effect} ok.lc <- !is.null(lcSet) && nzchar(lcSet) # NULL or "" are not ok - if(!ok.lc) warning(gettextf("In a bare C locale, could not change language"), domain=NA) + if(!ok.lc) + Warning(gettextf("In a bare C locale, could not change language"), domain=NA) } else ok.lc <- TRUE ok <- Sys.setenv(LANGUAGE=lang) if(!ok) - warning(gettextf('Sys.setenv(LANGUAGE="%s") may have failed', lang), domain=NA) + Warning(gettextf('Sys.setenv(LANGUAGE="%s") may have failed', lang), domain=NA) ok. <- capabilities("NLS") && isTRUE(bindtextdomain(NULL)) # only flush the cache (of already translated strings) invisible(structure(curLang, ok = ok && ok.lc && ok.)) diff --git a/src/library/base/man/gettext.Rd b/src/library/base/man/gettext.Rd index a0dcd6d65e9..79cfcfa17cf 100644 --- a/src/library/base/man/gettext.Rd +++ b/src/library/base/man/gettext.Rd @@ -1,6 +1,6 @@ % File src/library/base/man/gettext.Rd % Part of the R package, https://www.R-project.org -% Copyright 1995-2023 R Core Team +% Copyright 1995-2024 R Core Team % Distributed under GPL 2 or later \name{gettext} @@ -27,7 +27,7 @@ ngettext(n, msg1, msg2, domain = NULL) bindtextdomain(domain, dirname = NULL) -Sys.setLanguage(lang, unset = "en") +Sys.setLanguage(lang, unset = "en", allowC4en = TRUE) } \arguments{ \item{\dots}{one or more character vectors.} @@ -46,6 +46,9 @@ Sys.setLanguage(lang, unset = "en") \item{unset}{a string, specifying the default language assumed to be current in the case \code{\link{Sys.getenv}("LANGUAGE")} is unset or empty.} + \item{allowC4en}{logical indicating if a current (sometimes \dQuote{hard}) + setting of language \code{"C"} is acceptable for \code{lang = "en"}. In that + case, \code{\link{message}()} is called instead of \code{\link{warning}()}.} } \details{ If \code{domain} is \code{NULL} (the default) in \code{gettext} @@ -70,7 +73,7 @@ Sys.setLanguage(lang, unset = "en") The \emph{language} to be used for message translation is determined by your OS default and/or the locale setting at \R's startup, see - \code{\link{Sys.getlocale}()}, and notably the \env{LANGUAGE} environment + \code{\link{Sys.getlocale}()}, and notably the \env{LANGUAGE} environment variable, and also \code{Sys.setLanguage()} here. Conventionally the domain for \R warning/error messages in package