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

Use bindtextdomain() hack to reset cache #175

Merged
merged 1 commit into from
Nov 29, 2021
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
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)
})