Temporarily change locale settings.

with_locale(new, code)

local_locale(.new = list(), ..., .local_envir = parent.frame())

Arguments

new, .new

[named character]
New locale settings

code

[any]
Code to execute in the temporary environment

...

Additional arguments with locale settings.

.local_envir

[environment]
The environment to use for scoping.

Value

[any]
The results of the evaluation of the code argument.

Details

Setting the LC_ALL category is currently not implemented.

See also

withr for examples

Sys.setlocale()

Examples

## Change locale for time: df <- data.frame( stringsAsFactors = FALSE, date = as.Date(c("2019-01-01", "2019-02-01")), value = c(1, 2) ) with_locale(new = c("LC_TIME" = "es_ES"), code = plot(df$date, df$value))
## Compare with: # plot(df$date, df$value) ## Month names: with_locale(new = c("LC_TIME" = "en_GB"), format(ISOdate(2000, 1:12, 1), "%B"))
#> [1] "January" "February" "March" "April" "May" "June" #> [7] "July" "August" "September" "October" "November" "December"
with_locale(new = c("LC_TIME" = "es_ES"), format(ISOdate(2000, 1:12, 1), "%B"))
#> [1] "enero" "febrero" "marzo" "abril" "mayo" #> [6] "junio" "julio" "agosto" "septiembre" "octubre" #> [11] "noviembre" "diciembre"
## Change locale for currencies: with_locale(new = c("LC_MONETARY" = "it_IT"), Sys.localeconv())
#> decimal_point thousands_sep grouping int_curr_symbol #> "." "" "" "EUR " #> currency_symbol mon_decimal_point mon_thousands_sep mon_grouping #> "Eu" "," "." "\003\003" #> positive_sign negative_sign int_frac_digits frac_digits #> "" "-" "2" "2" #> p_cs_precedes p_sep_by_space n_cs_precedes n_sep_by_space #> "1" "1" "1" "1" #> p_sign_posn n_sign_posn #> "1" "1"
with_locale(new = c("LC_MONETARY" = "en_US"), Sys.localeconv())
#> decimal_point thousands_sep grouping int_curr_symbol #> "." "" "" "USD " #> currency_symbol mon_decimal_point mon_thousands_sep mon_grouping #> "$" "." "," "\003\003" #> positive_sign negative_sign int_frac_digits frac_digits #> "" "-" "2" "2" #> p_cs_precedes p_sep_by_space n_cs_precedes n_sep_by_space #> "1" "0" "1" "0" #> p_sign_posn n_sign_posn #> "1" "1"
## Ordering: x <- c("bernard", "bérénice", "béatrice", "boris") with_locale(c(LC_COLLATE = "fr_FR"), sort(x))
#> [1] "béatrice" "bérénice" "bernard" "boris"
with_locale(c(LC_COLLATE = "C"), sort(x))
#> [1] "bernard" "boris" "béatrice" "bérénice"