Bug 17298 - tools::xgettext() doesn't encode; incorrect when newlines in message
Summary: tools::xgettext() doesn't encode; incorrect when newlines in message
Status: CLOSED FIXED
Alias: None
Product: R
Classification: Unclassified
Component: Translations (show other bugs)
Version: R-devel (trunk)
Hardware: All All
: P5 minor
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2017-06-26 03:04 UTC by Richard Cotton
Modified: 2017-08-08 19:10 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Richard Cotton 2017-06-26 03:04:24 UTC
If a package has error/warning messages that contain a newline, tools::xgettext() incorrectly splits them at the newlines, and treats them as separate messages.

To reproduce:

pkg_dir <- tempfile("pkg")
devtools::create(pkg_dir)
writeLines(
  'f <- function() {
  stop("This\nerror message\ncontains\nnewlines.")
}',
  file.path(pkg_dir, "R/f.R")
)
tools::xgettext(pkg_dir)

I see

$`/some folders/RtmplBC24C/pkg1872728e2e99/R/f.R`
This
error message
contains
newlines.

It seems that there ought to be a call to encodeString() somewhere, so that \n and \t are preserved.
Comment 1 Richard Cotton 2017-06-26 12:08:48 UTC
On further reflection, it seems that the object is correct; it's just the print method that is the problem.

unclass(x[[1]])
## [1] "This\nerror message\ncontains\nnewlines."

I suggest changing print.xgettext() to 

tools:::print.xgettext <- function (x, ...) 
{
    cat(encodeString(x), sep = "\n")
    invisible(x)
}
Comment 2 Martin Maechler 2017-08-03 18:32:58 UTC
I've looked at it,  and I agree that changing the print() method as indicated improves it, exactly for those case where the strings contain \n or \t.

The same applies to  print.ngettext()  by the way.

OTOH, I have not been a user of these functions myself.
Applying it to the base package shows that in a few cases it makes a difference, 
and in my view the difference is  "uniformly positive".

If I don't hear other opinions,
I plan to commit the change  to R-devel.
Comment 3 Martin Maechler 2017-08-08 19:10:04 UTC
Committed to R-devel in svn rev 73068