Skip to content

Commit c581ffa

Browse files
Use ngettext() for some plural messages in R (#6476)
* ngettext() for R-side plural messages * Fix test references * Find and switch more ngettext candidates in R * missing ',' * Another round of tests fixes * Remove ':' from message too in test
1 parent 8cc2dd1 commit c581ffa

File tree

12 files changed

+86
-63
lines changed

12 files changed

+86
-63
lines changed

R/bmerge.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
173173
xo = forderv(nqx, c(ncol(nqx), xcols))
174174
if (verbose) {catf("done in %s\n",timetaken(last.started.at)); flush.console()}
175175
} else nqgrp = integer(0L)
176-
if (verbose) catf(" Found %d non-equi group(s) ...\n", nqmaxgrp)
176+
if (verbose)
177+
catf(ngettext(nqmaxgrp, " Found %d non-equi group ...\n", " Found %d non-equi groups ...\n"), nqmaxgrp, domain=NA)
177178
}
178179

179180
if (verbose) {last.started.at=proc.time();catf("Starting bmerge ...\n");flush.console()}

R/data.table.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,9 @@ replace_dot_alias = function(e) {
715715
if (is.factor(j)) j = as.character(j) # fix for FR: #358
716716
if (is.character(j)) {
717717
if (notj) {
718-
if (anyNA(idx <- chmatch(j, names_x))) warningf("column(s) not removed because not found: %s", brackify(j[is.na(idx)]))
718+
if (anyNA(idx <- chmatch(j, names_x)))
719+
warningf(ngettext(sum(is.na(idx)), "column not removed because not found: %s", "columns not removed because not found: %s"),
720+
brackify(j[is.na(idx)]), domain=NA)
719721
# all duplicates of the name in names(x) must be removed; e.g. data.table(x=1, y=2, x=3)[, !"x"] should just output 'y'.
720722
w = !names_x %chin% j
721723
ansvars = names_x[w]
@@ -729,7 +731,8 @@ replace_dot_alias = function(e) {
729731
if (!length(ansvals)) return(null.data.table())
730732
if (!length(leftcols)) {
731733
if (!anyNA(ansvals)) return(.Call(CsubsetDT, x, irows, ansvals))
732-
else stopf("column(s) not found: %s", brackify(ansvars[is.na(ansvals)]))
734+
else stopf(ngettext(sum(is.na(ansvals)), "column not found: %s", "columns not found: %s"),
735+
brackify(ansvars[is.na(ansvals)]), domain=NA)
733736
}
734737
# else the NA in ansvals are for join inherited scope (test 1973), and NA could be in irows from join and data in i should be returned (test 1977)
735738
# in both cases leave to the R-level subsetting of i and x together further below
@@ -895,7 +898,10 @@ replace_dot_alias = function(e) {
895898
}
896899
}
897900
tt = lengths(byval)
898-
if (any(tt!=xnrow)) stopf("The items in the 'by' or 'keyby' list are length(s) %s. Each must be length %d; the same length as there are rows in x (after subsetting if i is provided).", brackify(tt), xnrow)
901+
if (any(tt!=xnrow)) {
902+
plural_part <- sprintf(ngettext(length(tt), "The item in the 'by' or 'keyby' list is length %s.", "The items in the 'by' or 'keyby' list have lengths %s."), brackify(tt))
903+
stopf("%s Each must be length %d; the same length as there are rows in x (after subsetting if i is provided).", plural_part, xnrow)
904+
}
899905
if (is.null(bynames)) bynames = rep.int("",length(byval))
900906
if (length(idx <- which(!nzchar(bynames))) && !bynull) {
901907
# TODO: improve this and unify auto-naming of jsub and bysub
@@ -2731,8 +2737,8 @@ setnames = function(x,old,new,skip_absent=FALSE) {
27312737

27322738
setcolorder = function(x, neworder=key(x), before=NULL, after=NULL) # before/after #4358
27332739
{
2734-
if (is.character(neworder) && anyDuplicated(names(x)))
2735-
stopf("x has some duplicated column name(s): %s. Please remove or rename the duplicate(s) and try again.", brackify(unique(names(x)[duplicated(names(x))])))
2740+
if (is.character(neworder))
2741+
check_duplicate_names(x)
27362742
if (!is.null(before) && !is.null(after))
27372743
stopf("Provide either before= or after= but not both")
27382744
if (length(before)>1L || length(after)>1L)

R/fcast.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ dcast.data.table = function(data, formula, fun.aggregate = NULL, sep = "_", ...,
192192
fun.call = aggregate_funs(fun.call, lvals, sep, ...)
193193
maybe_err = function(list.of.columns) {
194194
if (!all(lengths(list.of.columns) == 1L)) {
195-
msg <- gettext("Aggregating function(s) should take a vector as input and return a single value (length=1), but they do not, so the result is undefined. Please fix by modifying your function so that a single value is always returned.")
195+
msg <- gettext("Aggregating functions should take a vector as input and return a single value (length=1), but they do not, so the result is undefined. Please fix by modifying your function so that a single value is always returned.")
196196
if (is.null(fill)) { # TODO change to always stopf #6329
197197
stop(msg, domain=NA, call. = FALSE)
198198
} else {

R/fmelt.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ patterns = function(..., cols=character(0L), ignore.case=FALSE, perl=FALSE, fixe
2828
stopf("Input patterns must be of type character.")
2929
matched = lapply(p, grep, cols, ignore.case=ignore.case, perl=perl, fixed=fixed, useBytes=useBytes)
3030
if (length(idx <- which(lengths(matched) == 0L)))
31-
stopf('Pattern(s) not found: [%s]', brackify(p[idx]))
31+
stopf(ngettext(length(idx), 'Pattern not found: [%s]', 'Patterns not found: [%s]'), brackify(p[idx]), domain=NA)
3232
if (length(matched) == 1L) return(matched[[1L]])
3333
matched
3434
}

R/foverlaps.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ foverlaps = function(x, y, by.x=if (!is.null(key(x))) key(x) else key(y), by.y=k
4444
stopf("Duplicate columns are not allowed in overlap joins. This may change in the future.")
4545
if (length(by.x) != length(by.y))
4646
stopf("length(by.x) != length(by.y). Columns specified in by.x should correspond to columns specified in by.y and should be of same lengths.")
47-
if (any(dup.x<-duplicated(names(x)))) #1730 - handling join possible but would require workarounds on setcolorder further, it is really better just to rename dup column
48-
stopf("%s has some duplicated column name(s): %s. Please remove or rename the duplicate(s) and try again.", "x", brackify(unique(names(x)[dup.x])))
49-
if (any(dup.y<-duplicated(names(y))))
50-
stopf("%s has some duplicated column name(s): %s. Please remove or rename the duplicate(s) and try again.", "y", brackify(unique(names(y)[dup.y])))
47+
48+
#1730 - handling join possible but would require workarounds on setcolorder further, it is really better just to rename dup column
49+
check_duplicate_names(x)
50+
check_duplicate_names(y)
51+
5152
xnames = by.x; xintervals = tail(xnames, 2L)
5253
ynames = by.y; yintervals = tail(ynames, 2L)
5354
xval1 = x[[xintervals[1L]]]; xval2 = x[[xintervals[2L]]]

R/fread.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,9 @@ yaml=FALSE, autostart=NA, tmpdir=tempdir(), tz="UTC")
326326
} else {
327327
cols_to_factor = which(vapply_1b(ans, is.character))
328328
}
329-
if (verbose) catf("stringsAsFactors=%s converted %d column(s): %s\n", stringsAsFactors, length(cols_to_factor), brackify(names(ans)[cols_to_factor]))
329+
if (verbose)
330+
catf(ngettext(length(cols_to_factor), "stringsAsFactors=%s converted %d column: %s\n", "stringsAsFactors=%s converted %d columns: %s\n"),
331+
stringsAsFactors, length(cols_to_factor), brackify(names(ans)[cols_to_factor]), domain=NA)
330332
for (j in cols_to_factor) set(ans, j=j, value=as_factor(.subset2(ans, j)))
331333
}
332334

R/merge.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ merge.data.table = function(x, y, by = NULL, by.x = NULL, by.y = NULL, all = FAL
2121
else
2222
warningf("Input data.table '%s' has no columns.", "y")
2323
}
24+
check_duplicate_names(x)
25+
check_duplicate_names(y)
26+
2427
nm_x = names(x)
2528
nm_y = names(y)
26-
if (anyDuplicated(nm_x)) stopf("%s has some duplicated column name(s): %s. Please remove or rename the duplicate(s) and try again.", "x", brackify(nm_x[duplicated(nm_x)]))
27-
if (anyDuplicated(nm_y)) stopf("%s has some duplicated column name(s): %s. Please remove or rename the duplicate(s) and try again.", "y", brackify(nm_y[duplicated(nm_y)]))
2829

2930
## set up 'by'/'by.x'/'by.y'
3031
if ( (!is.null(by.x) || !is.null(by.y)) && length(by.x)!=length(by.y) )

R/print.data.table.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ trunc_cols_message = function(not_printed, abbs, class, col.names){
283283
n = length(not_printed)
284284
if (class && col.names != "none") classes = paste0(" ", tail(abbs, n)) else classes = ""
285285
catf(
286-
"%d variable(s) not shown: %s\n",
287-
n, brackify(paste0(not_printed, classes))
286+
ngettext(n, "%d variable not shown: %s\n", "%d variables not shown: %s\n"),
287+
n, brackify(paste0(not_printed, classes)),
288+
domain=NA
288289
)
289290
}

R/setops.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ funique = function(x) {
4242
if (!identical(names(x), names(y))) stopf("x and y must have the same column order")
4343
bad_types = c("raw", "complex", if (block_list) "list")
4444
found = bad_types %chin% c(vapply_1c(x, typeof), vapply_1c(y, typeof))
45-
if (any(found)) stopf("unsupported column type(s) found in x or y: %s", brackify(bad_types[found]))
45+
if (any(found))
46+
stopf(ngettext(sum(found), "unsupported column type found in x or y: %s", "unsupported column types found in x or y: %s"),
47+
brackify(bad_types[found]), domain=NA)
4648
super = function(x) {
4749
# allow character->factor and integer->numeric because from v1.12.4 i's type is retained by joins, #3820
4850
ans = class(x)[1L]

R/test.data.table.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ test.data.table = function(script="tests.Rraw", verbose=FALSE, pkg=".", silent=F
252252
if (nfail > 0L) {
253253
# nocov start
254254
stopf(
255-
"%d error(s) out of %d. Search %s for test number(s) %s. Duration: %s.",
256-
nfail, ntest, names(fn), toString(env$whichfail), timetaken(env$started.at)
255+
ngettext(nfail, "%d error out of %d. Search %s for test number %s. Duration: %s.", "%d errors out of %d. Search %s for test numbers %s. Duration: %s."),
256+
nfail, ntest, names(fn), toString(env$whichfail), timetaken(env$started.at), domain=NA
257257
)
258258
# important to stopf() here, so that 'R CMD check' fails
259259
# nocov end

0 commit comments

Comments
 (0)