1
# locale ---------------------------------------------------------------------
2

3
set_locale <- function(cats) {
4 1
  cats <- as_character(cats)
5 1
  stopifnot(is.named(cats))
6

7 1
  if ("LC_ALL" %in% names(cats)) {
8 1
    stop("Setting LC_ALL category not implemented.", call. = FALSE)
9
  }
10

11 1
  old <- vapply(names(cats), Sys.getlocale, character(1))
12

13 1
  mapply(Sys.setlocale, names(cats), cats)
14 1
  invisible(old)
15
}
16

17
#' Locale settings
18
#'
19
#' Temporarily change locale settings.
20
#'
21
#' Setting the `LC_ALL` category is currently not implemented.
22
#'
23
#' @template with
24
#' @param new,.new `[named character]`\cr New locale settings
25
#' @param ... Additional arguments with locale settings.
26
#' @inheritParams with_collate
27
#' @seealso [Sys.setlocale()]
28
#' @examples
29
#'
30
#' ## Change locale for time:
31
#' df <- data.frame(
32
#'   stringsAsFactors = FALSE,
33
#'   date = as.Date(c("2019-01-01", "2019-02-01")),
34
#'   value = c(1, 2)
35
#' )
36
#' with_locale(new = c("LC_TIME" = "es_ES"), code = plot(df$date, df$value))
37
#' ## Compare with:
38
#' #  plot(df$date, df$value)
39
#'
40
#' ## Month names:
41
#' with_locale(new = c("LC_TIME" = "en_GB"), format(ISOdate(2000, 1:12, 1), "%B"))
42
#' with_locale(new = c("LC_TIME" = "es_ES"), format(ISOdate(2000, 1:12, 1), "%B"))
43
#'
44
#' ## Change locale for currencies:
45
#' with_locale(new = c("LC_MONETARY" = "it_IT"), Sys.localeconv())
46
#' with_locale(new = c("LC_MONETARY" = "en_US"), Sys.localeconv())
47
#'
48
#' ## Ordering:
49
#' x <- c("bernard", "bérénice", "béatrice", "boris")
50
#' with_locale(c(LC_COLLATE = "fr_FR"), sort(x))
51
#' with_locale(c(LC_COLLATE = "C"), sort(x))
52
#'
53
#' @export
54
with_locale <- with_(set_locale)
55

56
#' @rdname with_locale
57
#' @export
58
local_locale <- local_(set_locale, dots = TRUE)

Read our documentation on viewing source code .

Loading