locales {base} | R Documentation |
Query or Set Aspects of the Locale
Description
Get details of or set aspects of the locale for the R process.
Usage
Sys.getlocale (category = "LC_ALL")
Sys.setlocale (category = "LC_ALL", locale = "")
.LC.categories
Arguments
category |
character string. The following categories should
always be supported: |
locale |
character string. A valid locale name on the system in
use. Normally |
Details
The locale describes aspects of the internationalization of a program.
Initially most aspects of the locale of R are set to "C"
(which is the default for the C language and reflects North-American
usage – also known as "POSIX"
). R sets "LC_CTYPE"
and
"LC_COLLATE"
, which allow the use of a different character set
and alphabetic comparisons in that character set (including the use of
sort
), "LC_MONETARY"
(for use by
Sys.localeconv
) and "LC_TIME"
may affect the
behaviour of as.POSIXlt
and strptime
and
functions which use them (but not date
).
The first seven categories described here are those specified by
POSIX. "LC_MESSAGES"
will be "C"
on systems that do not
support message translation, and is not supported on Windows, where
you must use the LANGUAGE environment variable for
message translation, see below and the Sys.setLanguage()
utility. Trying to use an unsupported category is an error for
Sys.setlocale
.
Note that setting category "LC_ALL"
sets only categories
"LC_COLLATE"
, "LC_CTYPE"
, "LC_MONETARY"
and
"LC_TIME"
.
Attempts to set an invalid locale are ignored. There may or may not be a warning, depending on the OS.
Attempts to change the character set (by
Sys.setlocale("LC_CTYPE", )
, if that implies a different
character set) during a session may not work and are likely to lead to
some confusion.
Note that the LANGUAGE environment variable has precedence over
"LC_MESSAGES"
in selecting the language for message translation
on most R platforms.
On platforms where ICU is used for collation the locale used for
collation can be reset by icuSetCollate
. Except on
Windows, the initial setting is taken from the "LC_COLLATE"
category, and it is reset when this is changed by a call to
Sys.setlocale
.
Value
A character string of length one describing the locale in use (after
setting for Sys.setlocale
), or an empty character string if the
current locale settings are invalid or NULL
if locale
information is unavailable.
For category = "LC_ALL"
the details of the string are
system-specific: it might be a single locale name or a set of locale
names separated by "/"
(macOS) or ";"
(Windows, Linux). For portability, it is best to query categories
individually: it is not necessarily the case that the result of
foo <- Sys.getlocale()
can be used in
Sys.setlocale("LC_ALL", locale = foo)
.
Available locales
On most Unix-alikes the POSIX shell command locale -a
will
list the ‘available public’ locales. What that means is
platform-dependent. On recent Linuxen this may mean ‘available
to be installed’ as on some RPM-based systems the locale data is in
separate RPMs. On Debian/Ubuntu the set of available locales is
managed by OS-specific facilities such as locale-gen
and
locale -a
lists those currently enabled.
For Windows, Microsoft moves its documentation frequently so a Web
search is the best way to find current information. From R 4.2, UCRT
locale names should be used. The character set should match the
system/ANSI codepage (l10n_info()$codepage
be the same as
l10n_info()$system.codepage
). Setting it to any other value
results in a warning and may cause encoding problems. As from R 4.2
on recent Windows the system codepage is 65001 and one should always
use locale names ending with ".UTF-8"
(except for "C"
and ""
), otherwise Windows may add a different character set.
Warning
Setting "LC_NUMERIC"
to any value other than "C"
may
cause R to function anomalously, so gives a warning. Input
conversions in R itself are unaffected, but the reading and writing
of ASCII save
files will be, as may packages which do
their own input/output.
Setting it temporarily on a Unix-alike to produce graphical or text
output may work well enough, but options(OutDec)
is
often preferable.
Almost all the output routines used by R itself under Windows ignore
the setting of "LC_NUMERIC"
since they make use of the Trio
library which is not internationalized.
Note
Changing the values of locale categories whilst R is running ought to be noticed by the OS services, and usually is but exceptions have been seen (usually in collation services).
Do not use the value of Sys.getlocale("LC_CTYPE")
to attempt to
find the character set – for example UTF-8 locales can have suffix
‘.UTF-8’ or ‘.utf8’ (more common on Linux than ‘UTF-8’)
or none (as on macOS) and Latin-9 locales can have suffix
‘ISO8859-15’, ‘iso885915’, ‘iso885915@euro’ or
‘ISO8859-15@euro’. Use l10n_info
instead.
See Also
strptime
for uses of category = "LC_TIME"
.
Sys.localeconv
for details of numerical and monetary
representations.
l10n_info
gives some summary facts about the locale and
its encoding (including if it is UTF-8).
The ‘R Installation and Administration’ manual for background on locales and how to find out locale names on your system.
Examples
Sys.getlocale()
## Date-time related :
Sys.getlocale("LC_TIME") -> olcT
then <- as.POSIXlt("2001-01-01 01:01:01", tz = "UTC")
## Not run:
c(m = months(then), wd = weekdays(then)) # locale specific
Sys.setlocale("LC_TIME", "de") # Solaris: details are OS-dependent
Sys.setlocale("LC_TIME", "de_DE") # Many Unix-alikes
Sys.setlocale("LC_TIME", "de_DE.UTF-8") # Linux, macOS, other Unix-alikes
Sys.setlocale("LC_TIME", "de_DE.utf8") # some Linux versions
Sys.setlocale("LC_TIME", "German.UTF-8") # Windows
Sys.getlocale("LC_TIME") # the last one successfully set above
c(m = months(then), wd = weekdays(then)) # in C_TIME locale 'cT' ; typically German
## End(Not run)
Sys.setlocale("LC_TIME", "C")
c(m = months(then), wd = weekdays(then)) # "standard" (still platform specific ?)
Sys.setlocale("LC_TIME", olcT) # reset to previous
## Other locales
Sys.getlocale("LC_PAPER") # may or may not be set
.LC.categories # of length 9 on all platforms
## Not run: Sys.setlocale("LC_COLLATE", "C") # turn off locale-specific sorting,
# usually (but not on all platforms)
Sys.setenv("LANGUAGE" = "es") # set the language for error/warning messages
## End(Not run)
## some nice formatting; should work on most platforms,
## macOS does not name the entries.
sep <- switch(Sys.info()[["sysname"]],
"Darwin"=, "SunOS" = "/",
"Linux" =, "Windows" = ";")
##' named vector from a "full" Sys.getlocale() :
asNvec <- function(loc) {
sl <- strsplit(strsplit(loc, sep)[[1L]], "=")
if(all(lengths(sl) == 2L))
setNames(sapply(sl, `[[`, 2L), sapply(sl, `[[`, 1L))
else
setNames(as.character(sl), .LC.categories[1+seq_along(sl)])
}
print.Dlist(lloc <- asNvec(Sys.getlocale()))
## R-supported ones (but LC_ALL):
lloc[.LC.categories[-1]]