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

default to UTF-8 when writing .pot files #300

Merged
merged 6 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Suggests:
crayon,
knitr,
rmarkdown,
testthat (>= 3.0.0),
testthat (>= 3.1.5),
withr
VignetteBuilder:
knitr
Expand Down
8 changes: 5 additions & 3 deletions R/write_po_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ write_po_files <- function(message_data, po_dir, params, template = FALSE, use_b
if (is_base_package) {
params$package <- "R"
params$bugs <- "bugs.r-project.org"
# #299: msg* commands work best with charset=UTF-8,
# but the 'base' .pot files leave it unsepecified (CHARSET).
if (template) params$charset <- "CHARSET"
}

if (template) {
Expand Down Expand Up @@ -248,7 +251,7 @@ write_po_file <- function(
po_data[ , 'msgid_plural' := strsplit(msgid_plural, "|||", fixed = TRUE)]

# tools::xgettext2pot() tries to make the entries' whitespace align, which xgettext doesn't do
if (use_base_rules & po_data$message_source[1L] == "R") {
if (use_base_rules && po_data$message_source[1L] == "R") {
plural_fmt <- '\n%s%smsgid "%s"\nmsgid_plural "%s"\n%s'
msgstr_fmt <- 'msgstr[%d] "%s"'
} else {
Expand Down Expand Up @@ -403,7 +406,7 @@ po_metadata = function(package='', version='', language='', author='', email='',
is.null(copyright) || is.character(copyright) || is.list(copyright)
)
pm = c(as.list(environment()), list(...))
pm$charset <- "UTF-8"
if (is.null(pm$charset)) pm$charset <- "UTF-8"
if (is.null(pm$pot_timestamp)) pm$pot_timestamp <- Sys.time()
if (is.null(pm$po_timestamp)) pm$po_timestamp <- pm$pot_timestamp
if (is.null(pm$language_team)) pm$language_team <- pm$language
Expand All @@ -420,7 +423,6 @@ format.po_metadata = function(x, template = FALSE, use_plurals = FALSE, ...) {
x$email = "EMAIL@ADDRESS"
x$language = ''
x$language_team = "LANGUAGE <[email protected]>"
x$charset = 'CHARSET'
}
if (is.character(x$copyright)) {
x$copyright = list(years = format(x$pot_timestamp, "%Y"), holder = x$copyright)
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-po_compile.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,18 @@ test_that("get_po_metadata() returns 0 rows if no .po fles", {
meta <- get_po_metadata(temp)
expect_equal(nrow(meta), 0)
})

test_that("po_compile() can handle UTF-8 msgstr", { temp <-
local_test_package(
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
`R/foo.R` = "foo <- function() message('Hello!')"
)

po_extract(temp) # R/* -> .pot
po_create("es", temp) # .pot -> R-es.po
r_es_po <- file.path(temp, "po", "R-es.po")
l <- readLines(r_es_po)
l[grep('msgstr ""', l)[2L]] <- 'msgstr "\U00A1Hello!"'
cat(l, file = r_es_po, sep = "\n")

expect_no_error(po_compile(temp, verbose=FALSE))
})
Loading