Skip to content

Commit

Permalink
Use dngettext() for some plural messages in C (#6475)
Browse files Browse the repository at this point in the history
* Use ngettext() for some plural messages

* indent

* fix too many args to DTWARN

* Found another

* Use dngettext, with macro

* Tricked myself: switch order back

* Fix in tests
  • Loading branch information
MichaelChirico authored Sep 6, 2024
1 parent 79b3840 commit c616cb9
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 21 deletions.
12 changes: 6 additions & 6 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -12427,22 +12427,22 @@ test(1867.02, fread("A,B,C,D,E\n1,3,5,7\n2,4,6,8\n"), data.table(A=1:2, B=3:4, C
warning="Detected 5.*but.*4.*Filling rows automatically.")
if (test_R.utils) {
test(1867.03, fread(testDir("fillheader.csv.bz2"))[c(1,.N), c(1,29,30)], data.table("V1"="Ashburton District", EASTING=c(5154177L,5144032L), NORTHING=NA),
warning="Detected 29.*but.*30.*Added 1 extra default column name.*guessed to be row names or an index.*valid file")
warning="Detected 29.*but.*30.*Added an extra default column name.*guessed to be row names or an index.*valid file")
# in this unusual case, every data row has a trailing comma but the column names do not. So the guess is wrong; a deliberate choice currently.
}
test(1867.04, fread("A,B\nCol1,Col2,Col3\n1,3,5\n2,4,6\n"), data.table(Col1=1:2, Col2=3:4, Col3=5:6))
test(1867.05, fread("A\nCol1,Col2\n1,3,5\n2,4,6\n"), data.table(V1=1:2, Col1=3:4, Col2=5:6), warning="Added 1 extra default column name.*guessed to be row names or an index")
test(1867.05, fread("A\nCol1,Col2\n1,3,5\n2,4,6\n"), data.table(V1=1:2, Col1=3:4, Col2=5:6), warning="Added an extra default column name.*guessed to be row names or an index")
test(1867.06, fread("Some header\ninfo\nCol1,Col2,Col3\n1,3,5\n2,4,6\n"), data.table(Col1=1:2, Col2=3:4, Col3=5:6))
test(1867.07, fread("Some header\ninfo\n\nCol1,Col2\n1,3,5\n2,4,6\n"), data.table(V1=1:2, Col1=3:4, Col2=5:6), warning="Added 1 extra")
test(1867.07, fread("Some header\ninfo\n\nCol1,Col2\n1,3,5\n2,4,6\n"), data.table(V1=1:2, Col1=3:4, Col2=5:6), warning="Added an extra")
test(1867.08, fread("A,B,C,D,E\n1,3,5\n2,4,6\n"), data.table(A=1:2, B=3:4, C=5:6, D=NA, E=NA), warning="Detected 5.*but.*3.*Filling rows automatically")
test(1867.09, fread("Heading text\nA,B,C,D,E\n1,3,5\n2,4,6\n"), data.table(A=1:2, B=3:4, C=5:6, D=NA, E=NA), warning="Detected 5.*but.*3.*Filling rows automatically")
test(1867.10, fread("Heading text\n1,3,5\n2,4,6\n"), data.table("Heading text"=1:2, V2=3:4, V3=5:6), warning="Added 2 extra default column names at the end")
test(1867.11, fread("A,B\n\n1,3,5\n2,4,6\n"), data.table(V1=1:2, V2=3:4, V3=5:6))
test(1867.12, fread("A\n1,3\n2,4\n"), data.table(V1=1:2, A=3:4), warning="Added 1 extra default column name")
test(1867.12, fread("A\n1,3\n2,4\n"), data.table(V1=1:2, A=3:4), warning="Added an extra default column name")
# test from #763, covers #1818 too
DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1L,3L,6L), v=10:18)
write.table(DT, file = (f<-tempfile()), sep = "\t")
test(1867.13, fread(f), data.table(V1=1:9, x=DT$x, y=DT$y, v=DT$v), warning="Added 1 extra default column name")
test(1867.13, fread(f), data.table(V1=1:9, x=DT$x, y=DT$y, v=DT$v), warning="Added an extra default column name")
unlink(f)
# test(1867.14, fread(testDir("iterations.txt.bz2")))

Expand Down Expand Up @@ -12880,7 +12880,7 @@ test(1908, observed, expected)
if (test_R.utils) {
test(1909.1, names(ans<-fread(testDir("genotypes_genome.txt.bz2"), skip="Samples:", sep=" ", colClasses="character")),
c("V1","Samples:"),
warning="Detected 1 column name.*but the data has 2 columns.*Added 1 extra default column name for the first column")
warning="Detected 1 column name.*but the data has 2 columns.*Added an extra default column name for the first column")
test(1909.2, ans$V1, c("POP1:","POP1:","POP1:"))
test(1909.3, nchar(ans[["Samples:"]]), INT(3287,3287,3287))
test(1909.4, names(ans<-fread(testDir("genotypes_genome.txt.bz2"), skip="POP1:", sep=" ", colClasses="character", header=FALSE)),
Expand Down
6 changes: 4 additions & 2 deletions src/assign.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,10 @@ SEXP assign(SEXP dt, SEXP rows, SEXP cols, SEXP newcolnames, SEXP values)
UNPROTECT(protecti);
return(dt); // all items of rows either 0 or NA. !length(newcolnames) for #759
}
if (verbose) Rprintf(_("Added %d new column%s initialized with all-NA\n"),
length(newcolnames), (length(newcolnames)>1)?"s":"");
if (verbose) Rprintf(Pl_(length(newcolnames),
"Added %d new column initialized with all-NA\n",
"Added %d new columns initialized with all-NA\n"),
length(newcolnames));
}
}
if (!length(cols)) {
Expand Down
19 changes: 14 additions & 5 deletions src/fread.c
Original file line number Diff line number Diff line change
Expand Up @@ -1594,8 +1594,13 @@ int freadMain(freadMainArgs _args) {
row1line++;
}
}
if (ch > sof && verbose) DTPRINT(_(" Skipped to line %"PRIu64" in the file"), (uint64_t)row1line);
if (ch>=eof) STOP(_("skip=%"PRIu64" but the input only has %"PRIu64" line%s"), (uint64_t)args.skipNrow, (uint64_t)row1line, row1line>1?"s":"");
if (ch > sof && verbose)
DTPRINT(_(" Skipped to line %"PRIu64" in the file"), (uint64_t)row1line);
if (ch>=eof)
STOP(Pl_(row1line,
"skip=%"PRIu64" but the input only has %"PRIu64" line",
"skip=%"PRIu64" but the input only has %"PRIu64" lines"),
(uint64_t)args.skipNrow, (uint64_t)row1line);
pos = ch;
}

Expand Down Expand Up @@ -1977,9 +1982,13 @@ int freadMain(freadMainArgs _args) {
if (ch!=pos) INTERNAL_STOP("ch!=pos after counting fields in the line before the first data row"); // # nocov
if (verbose) DTPRINT(_("Types in 1st data row match types in 2nd data row but previous row has %d fields. Taking previous row as column names."), tt);
if (tt<ncol) {
autoFirstColName = (tt==ncol-1);
DTWARN(_("Detected %d column names but the data has %d columns (i.e. invalid file). Added %d extra default column name%s\n"), tt, ncol, ncol-tt,
autoFirstColName ? _(" for the first column which is guessed to be row names or an index. Use setnames() afterwards if this guess is not correct, or fix the file write command that created the file to create a valid file.") : _("s at the end."));
autoFirstColName = (ncol-tt==1);
if (autoFirstColName) {
DTWARN(_("Detected %d column names but the data has %d columns (i.e. invalid file). Added an extra default column name for the first column which is guessed to be row names or an index. Use setnames() afterwards if this guess is not correct, or fix the file write command that created the file to create a valid file.\n"),
tt, ncol);
} else {
DTWARN(_("Detected %d column names but the data has %d columns (i.e. invalid file). Added %d extra default column names at the end.\n"), tt, ncol, ncol-tt);
}
} else if (tt>ncol) {
if (fill) INTERNAL_STOP("fill=true but there is a previous row which should already have been filled"); // # nocov
DTWARN(_("Detected %d column names but the data has %d columns. Filling rows automatically. Set fill=TRUE explicitly to avoid this warning.\n"), tt, ncol);
Expand Down
8 changes: 4 additions & 4 deletions src/fwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -960,10 +960,10 @@ void fwriteMain(fwriteMainArgs args)
int ETA = (int)((args.nrow-end)*((now-startTime)/end));
if (hasPrinted || ETA >= 2) {
if (verbose && !hasPrinted) DTPRINT("\n");
DTPRINT("\rWritten %.1f%% of %"PRId64" rows in %d secs using %d thread%s. "
"maxBuffUsed=%d%%. ETA %d secs. ",
(100.0*end)/args.nrow, args.nrow, (int)(now-startTime), nth, nth==1?"":"s",
maxBuffUsedPC, ETA);
DTPRINT(Pl_(nth,
"\rWritten %.1f%% of %"PRId64" rows in %d secs using %d thread. maxBuffUsed=%d%%. ETA %d secs. ",
"\rWritten %.1f%% of %"PRId64" rows in %d secs using %d threads. maxBuffUsed=%d%%. ETA %d secs. "),
(100.0*end)/args.nrow, args.nrow, (int)(now-startTime), nth, maxBuffUsedPC, ETA);
// TODO: use progress() as in fread
nextTime = now+1;
hasPrinted = true;
Expand Down
5 changes: 4 additions & 1 deletion src/nafill.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP nan_is_na_arg, SEXP inplace, S
ansMsg(vans, nx, verbose, __func__);

if (verbose)
Rprintf(_("%s: parallel processing of %d column(s) took %.3fs\n"), __func__, nx, omp_get_wtime()-tic);
Rprintf(Pl_(nx,
"%s: parallel processing of %d column took %.3fs\n",
"%s: parallel processing of %d columns took %.3fs\n"),
__func__, nx, omp_get_wtime()-tic);

UNPROTECT(protecti);
if (binplace) {
Expand Down
3 changes: 3 additions & 0 deletions src/po.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifdef ENABLE_NLS
#include <libintl.h>
#define _(String) dgettext("data.table", String)
// NB: flip argument order to match that of R's ngettext()
#define Pl_(n, String1, StringPlural) dngettext("data.table", String1, StringPlural, n)
#else
#define _(String) (String)
#define Pl_(n, String1, StringPlural) ((n) == 1 ? (String1) : (StringPlural))
#endif
6 changes: 4 additions & 2 deletions src/rbindlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ SEXP rbindlist(SEXP l, SEXP usenamesArg, SEXP fillArg, SEXP idcolArg, SEXP ignor
if (numZero) { // #1871
SEXP names = getAttrib(VECTOR_ELT(l, firstZeroItem), R_NamesSymbol);
const char *ch = names==R_NilValue ? "" : CHAR(STRING_ELT(names, firstZeroCol));
warning(_("Column %d ['%s'] of item %d is length 0. This (and %d other%s like it) has been filled with NA (NULL for list columns) to make each item uniform."),
firstZeroCol+1, ch, firstZeroItem+1, numZero-1, numZero==2?"":"s");
warning(Pl_(numZero-1,
"Column %d ['%s'] of item %d is length 0. This (and %d other like it) has been filled with NA (NULL for list columns) to make each item uniform.",
"Column %d ['%s'] of item %d is length 0. This (and %d others like it) has been filled with NA (NULL for list columns) to make each item uniform."),
firstZeroCol+1, ch, firstZeroItem+1, numZero-1);
}
if (nrow==0 && ncol==0) return(R_NilValue);
if (nrow>INT32_MAX) error(_("Total rows in the list is %"PRId64" which is larger than the maximum number of rows, currently %d"), (int64_t)nrow, INT32_MAX);
Expand Down
6 changes: 5 additions & 1 deletion src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,11 @@ void copySharedColumns(SEXP x) {
if (shared[i])
SET_VECTOR_ELT(x, i, copyAsPlain(xp[i]));
}
if (GetVerbose()) Rprintf(_("Found and copied %d column%s with a shared memory address\n"), nShared, nShared>1?"s":"");
if (GetVerbose())
Rprintf(Pl_(nShared,
"Found and copied %d column with a shared memory address\n",
"Found and copied %d columns with a shared memory address\n"),
nShared);
// GetVerbose() (slightly expensive call of all options) called here only when needed
}
}
Expand Down

0 comments on commit c616cb9

Please sign in to comment.