Skip to content

Commit

Permalink
do not warn but message, when Sys.setLanguage("en") is deficient in C…
Browse files Browse the repository at this point in the history
… locale

git-svn-id: https://svn.r-project.org/R/trunk@87051 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
maechler committed Aug 26, 2024
1 parent e5f99af commit 0f99146
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
3 changes: 2 additions & 1 deletion doc/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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{"<missing>"} even when \R's language setting is not English.
Expand Down
15 changes: 9 additions & 6 deletions src/library/base/R/stop.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.))
Expand Down
9 changes: 6 additions & 3 deletions src/library/base/man/gettext.Rd
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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.}
Expand All @@ -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}
Expand All @@ -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
Expand Down

0 comments on commit 0f99146

Please sign in to comment.