Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try using bindtextdomain() for cache clearing #183

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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