Skip to content

Commit

Permalink
Use bindtextdomain() for cache clearing (#183)
Browse files Browse the repository at this point in the history
Closes #213.
  • Loading branch information
hadley committed Jan 9, 2024
1 parent 214d46c commit a57401a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
17 changes: 9 additions & 8 deletions R/language.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ local_language <- function(lang, .local_envir = parent.frame()) {
}

local_envvar(LANGUAGE = lang, .local_envir = .local_envir)
if (Sys.info()[["sysname"]] != "Windows") {
# Reset cache to avoid gettext() retrieving cached value from a previous
# language. I think this works because Sys.setlocale() calls setlocale()
# which https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=931456 claims
# resets the cache. So if there's some OS/setup that this technique fails
# on, we might try bindtextdomain() instead or as well.
local_locale(c(LC_MESSAGES = ""), .local_envir = .local_envir)
}

# Reset cache to avoid gettext() retrieving cached value from a previous
# language (idea from https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=931456)
defer(
bindtextdomain("reset", local_tempdir()),
envir = .local_envir
)

invisible()
}

has_nls <- function() capabilities("NLS")[[1]]
3 changes: 3 additions & 0 deletions tests/testthat/test-language.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ test_that("can temporarily change language", {
# can use either _ or -
expect_error(with_lang("pt_BR", "pt_BR", mean[[1]]), "não possível dividir")
expect_error(with_lang("pt_BR", "pt-BR", mean[[1]]), "não possível dividir")

# Is correctly reset (#213)
expect_error(mean[[1]], "not subsettable")
})

test_that("warns if LANG=C", {
Expand Down

0 comments on commit a57401a

Please sign in to comment.