Skip to content

Commit

Permalink
Use bindtextdomain() hack to reset cache
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored and jimhester committed Nov 29, 2021
1 parent e43073c commit 19fadb8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 21 deletions.
23 changes: 5 additions & 18 deletions R/po.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,12 @@ load_po <- function(package, path) {
return()
}

# Clean up previous copies
unlink(temp_po_dirs(package), recursive = TRUE, force = TRUE)
# Reset cache to avoid gettext() retrieving cached value
# See <https://bugs.r-project.org/show_bug.cgi?id=18055> for details
bindtextdomain("reset", withr::local_tempdir())

# Create new copy of translations in temp dir
tmp <- tempfile(temp_po_prefix(package))
dir.create(tmp, showWarnings = FALSE)
tmp_po <- file.path(tmp, "po")
file.copy(po_path, tmp, recursive = TRUE)

bindtextdomain(paste0("R-", package), tmp_po) # R level messages
bindtextdomain(package, tmp_po) # C level messages
bindtextdomain(paste0("R-", package), po_path) # R level messages
bindtextdomain(package, po_path) # C level messages

invisible()
}

temp_po_prefix <- function(package) {
paste0("pkgload-po-", package, "-")
}

temp_po_dirs <- function(package) {
dir(tempdir(), paste0("^", temp_po_prefix(package)), full.names = TRUE)
}
3 changes: 0 additions & 3 deletions tests/testthat/test-po.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@ test_that("translation domain correctly loaded", {

withr::local_envvar(LANGUAGE = "fr")
expect_equal(hello(), "Bonjour")

load_all(test_path("testTranslations"))
expect_equal(length(temp_po_dirs("testTranslations")), 1)
})

0 comments on commit 19fadb8

Please sign in to comment.