Skip to content

Commit d43d355

Browse files
Use catf() over messagef() where appropriate
1 parent 86f67b5 commit d43d355

18 files changed

Lines changed: 218 additions & 183 deletions

R/custom-translators.R

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1+
catf = function(fmt, ..., sep=" ", domain="R-potools") {
2+
cat(gettextf(fmt, ..., domain=domain), sep=sep)
3+
}
4+
15
# tends to make some really repetitive messages with call.=TRUE, so always turn it off
2-
stopf = function(fmt, ..., domain = NULL) {
3-
stop(gettextf(fmt, ..., domain = NULL), domain = NA, call. = FALSE)
6+
stopf = function(fmt, ..., domain="R-potools") {
7+
stop(gettextf(fmt, ..., domain=domain), domain = NA, call. = FALSE)
48
}
59

610
# uncovered for now, used in msgmerge.R
7-
warningf = function(fmt, ..., immediate. = FALSE, noBreaks. = FALSE) {
8-
warning(gettextf(fmt, ..., domain = NULL), domain = NA, call. = FALSE, immediate. = immediate., noBreaks. = noBreaks.)
11+
warningf = function(fmt, ..., immediate.=FALSE, noBreaks.=FALSE, domain="R-potools") {
12+
warning(gettextf(fmt, ..., domain=domain), domain = NA, call. = FALSE, immediate. = immediate., noBreaks. = noBreaks.)
913
}
1014

11-
messagef = function(fmt, ..., appendLF = TRUE) {
12-
message(gettextf(fmt, ..., domain = NULL), domain = NA, appendLF = appendLF)
15+
messagef = function(fmt, ..., appendLF=TRUE, domain="R-potools") {
16+
message(gettextf(fmt, ..., domain=domain), domain = NA, appendLF = appendLF)
1317
}
1418

1519
# not actually used yet in package src, so commenting out for test coverage, but leaving in for illustration
1620
# packageStartupMessagef = function(fmt, ..., appendLF = TRUE) {
17-
# packageStartupMessage(gettextf(fmt, ..., domain = NULL), domain = NA, appendLF = appendLF)
21+
# packageStartupMessage(gettextf(fmt, ..., domain = domain), domain = NA, appendLF = appendLF)
1822
# }

R/explain_plurals.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ po_explain_plurals <- function(language, index) {
2424

2525
plural_metadata = .potools$PLURAL_RANGE_STRINGS[language_metadata, .SD, on = "plural"]
2626
if (missing(index)) {
27-
language_metadata[, message(domain = NA, sprintf(
27+
language_metadata[, messagef(
2828
ngettext(nplurals, "%s (%s) has %d plural form.", "%s (%s) has %d plural forms."),
29-
full_name_eng, full_name_native, nplurals
30-
))]
29+
full_name_eng, full_name_native, nplurals,
30+
domain = NA
31+
)]
3132
plural_metadata[, message(domain = NA, paste(
3233
gettextf(" - plural_index = %d applies %s", plural_index, range_translation = gettext(range)),
3334
collapse = "\n"

R/find_fuzzy_messages.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@ find_fuzzy_messages <- function(message_data, lang_file) {
22
old_message_data = get_po_messages(lang_file)
33

44
if (any(idx <- old_message_data$fuzzy == 2L)) {
5-
messagef('Found %d translations marked as deprecated in %s.', sum(idx), lang_file)
6-
message('Typically, this means the corresponding error messages have been refactored.')
7-
message('Reproducing these messages here for your reference since they might still provide some utility.')
5+
messagef(
6+
paste(
7+
'Found %d translations marked as deprecated in %s.',
8+
'Typically, this means the corresponding error messages have been refactored.',
9+
'Reproducing these messages here for your reference since they might still provide some utility.',
10+
sep = '\n'
11+
),
12+
sum(idx), lang_file
13+
)
814

915
dashes = strrep('-', 0.9*getOption('width'))
1016
old_message_data[idx & type == 'singular', {
1117
if (.N > 0L) {
12-
message(' ** SINGULAR MESSAGES **')
18+
cat(' ** SINGULAR MESSAGES **\n')
1319
cat(rbind(dashes, msgid, msgstr), sep='\n')
1420
}
1521
}]
1622
old_message_data[idx & type == 'plural', {
1723
if (.N > 0L) {
18-
message(' ** PLURAL MESSAGES **')
24+
cat(' ** PLURAL MESSAGES **\n')
1925
cat(do.call(rbind, c(list(dashes), msgid_plural, msgstr_plural)), sep='\n')
2026
}
2127
}]

R/get_message_data.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ get_message_data = function(
9292
}
9393
}
9494

95-
if (verbose && dir.exists(file.path(dir, "R"))) message('Getting R-level messages...')
95+
if (verbose && dir.exists(file.path(dir, "R"))) cat('Getting R-level messages...\n')
9696
r_message_data = get_r_messages(
9797
dir,
9898
# nolint next: backport_linter. False positive on 'R'.
@@ -101,7 +101,7 @@ get_message_data = function(
101101
is_base = is_base
102102
)
103103

104-
if (verbose && dir.exists(file.path(dir, "src"))) message('Getting src-level messages...')
104+
if (verbose && dir.exists(file.path(dir, "src"))) cat('Getting src-level messages...\n')
105105
src_message_data = get_src_messages(
106106
dir,
107107
custom_translation_functions = custom_translation_functions$src,

R/msgmerge.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ run_msgmerge <- function(po_file, pot_file, previous = FALSE, verbose = TRUE) {
1212
)
1313

1414
if (verbose) {
15-
message("Running system command msgmerge ", paste(msgmerge_args, collapse = " "), "...")
15+
catf("Running system command msgmerge %s ...\n", paste(msgmerge_args, collapse = " "))
1616
}
1717
val <- system2("msgmerge", msgmerge_args, stdout = TRUE, stderr = TRUE)
1818
if (!identical(attr(val, "status", exact = TRUE), NULL)) {
1919
# nocov these warnings? i don't know how to trigger them as of this writing.
2020
warningf("Running msgmerge on './po/%s' failed:\n %s", basename(po_file), paste(val, collapse = "\n"))
2121
} else if (verbose) {
22-
messagef(paste(val, collapse = "\n"))
22+
writeLines(val)
2323
}
2424

2525
res <- tools::checkPoFile(po_file, strictPlural = TRUE)
@@ -45,7 +45,7 @@ run_msgfmt = function(po_file, mo_file, verbose) {
4545
cmd = glue("msgfmt -o {shQuote(mo_file)} {shQuote(po_file)}") # nocov
4646
}
4747
if (verbose) {
48-
message("Running system command ", cmd, "...")
48+
catf("Running system command %s ...\n", cmd)
4949
}
5050
if (system(cmd) != 0L) {
5151
warningf(
@@ -88,13 +88,13 @@ run_msginit <- function(po_path, pot_path, locale, width = 80L, verbose = TRUE)
8888
"--no-translator" # don't consult user-email etc
8989
)
9090
if (verbose) {
91-
message("Running system command msginit ", paste(msginit_args, collapse = " "), "...")
91+
catf("Running system command msginit %s...\n", paste(msginit_args, collapse = " "))
9292
}
9393
val <- system2("msginit", msginit_args, stdout = TRUE, stderr = TRUE)
9494
if (!identical(attr(val, "status", exact = TRUE), NULL)) {
9595
stopf("Running msginit on '%s' failed", pot_path)
9696
} else if (verbose) {
97-
messagef(paste(val, collapse = "\n"))
97+
writeLines(val)
9898
}
9999
invisible()
100100
}

R/po_compile.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ po_compile = function(dir = ".", package = NULL, lazy = TRUE, verbose = TRUE) {
2424
to_delete <- mo_dirs[!basename(mo_dirs) %in% c(po_metadata$language, "en@quot")]
2525

2626
for (dir in to_delete) {
27-
if (verbose) messagef(
28-
"Found a compiled translation for %s at %s, but no corresponding .po; deleting",
27+
if (verbose) catf(
28+
"Found a compiled translation for %s at %s, but no corresponding .po; deleting\n",
2929
basename(dir), dirname(dir)
3030
)
3131
unlink(dir, recursive = TRUE)
@@ -34,7 +34,7 @@ po_compile = function(dir = ".", package = NULL, lazy = TRUE, verbose = TRUE) {
3434

3535
for (ii in seq_len(nrow(po_metadata))) {
3636
row_ii <- po_metadata[ii]
37-
if (verbose) messagef("Recompiling '%s' %s translation", row_ii$language, row_ii$type)
37+
if (verbose) catf("Recompiling '%s' %s translation\n", row_ii$language, row_ii$type)
3838
run_msgfmt(row_ii$po, row_ii$mo, verbose = verbose)
3939
}
4040

R/po_create.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ po_create <- function(languages, dir = ".", verbose = !is_testing()) {
2222
for (ii in seq_len(nrow(po_files))) {
2323
row_ii <- po_files[ii]
2424
if (file.exists(row_ii$po_path)) {
25-
if (verbose) messagef("Updating '%s' %s translation", row_ii$language, row_ii$type)
25+
if (verbose) catf("Updating '%s' %s translation\n", row_ii$language, row_ii$type)
2626
run_msgmerge(row_ii$po_path, row_ii$pot_path, previous = TRUE, verbose = verbose)
2727
} else {
28-
if (verbose) messagef("Creating '%s' %s translation", row_ii$language, row_ii$type)
28+
if (verbose) catf("Creating '%s' %s translation\n", row_ii$language, row_ii$type)
2929
run_msginit(row_ii$po_path, row_ii$pot_path, locale = row_ii$language, verbose = verbose)
3030
}
3131
}

R/po_extract.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,10 @@ po_extract <- function(
2727

2828
n <- nrow(message_data)
2929
if (!n) {
30-
if (verbose) message('No messages to translate')
30+
if (verbose) catf('No messages to translate\n')
3131
return(invisible())
3232
}
33-
# TODO: messagef() is double-translating the ngettext() result...
34-
# it needs an escape valve
35-
if (verbose) messagef(ngettext(n, "Found %i message", "Found %i messages"), n)
33+
if (verbose) catf(ngettext(n, "Found %i message", "Found %i messages"), n, domain=NA)
3634

3735
po_dir <- file.path(dir, 'po')
3836
dir.create(po_dir, showWarnings = FALSE)

R/po_update.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ po_update <- function(dir = ".", lazy = TRUE, verbose = !is_testing()) {
3737

3838
for (ii in seq_len(nrow(meta))) {
3939
row_ii <- meta[ii]
40-
if (verbose) messagef("Updating '%s' %s translation", row_ii$language, row_ii$type)
40+
if (verbose) catf("Updating '%s' %s translation\n", row_ii$language, row_ii$type)
4141
run_msgmerge(row_ii$po, row_ii$pot, previous = TRUE, verbose = verbose)
4242
}
4343

R/translate_package.R

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -344,21 +344,21 @@ translate_package = function(
344344
if (tr_update) {
345345
# is it worthwhile to try and distinguish the creation time of the
346346
# R pot file and the src pot file? probably not...
347-
messagef(
348-
"Updating translation template for package '%s' (last updated %s)",
347+
catf(
348+
"Updating translation template for package '%s' (last updated %s)\n",
349349
package,
350350
format(file.info(r_potfile)$atime)
351351
)
352352
} else {
353-
messagef("Starting translations for package '%s'", package)
353+
catf("Starting translations for package '%s'\n", package)
354354
}
355355
}
356356
if (!tr_update) dir.create(po_dir, showWarnings = FALSE)
357357

358358
message_data = get_message_data(dir, custom_translation_functions, verbose=verbose)
359359

360360
if (!nrow(message_data)) {
361-
if (verbose) message('No messages to translate; finishing')
361+
if (verbose) cat('No messages to translate; finishing\n')
362362
return(invisible())
363363
}
364364

@@ -369,7 +369,7 @@ translate_package = function(
369369
set_prompt_conn()
370370
on.exit(unset_prompt_conn())
371371

372-
if (verbose) message('Running message diagnostics...')
372+
if (verbose) cat('Running message diagnostics...\n')
373373

374374
for (diagnostic in diagnostics) {
375375
diagnostic <- match.fun(diagnostic)
@@ -387,13 +387,13 @@ translate_package = function(
387387
if (l10n_info()[["UTF-8"]]) {
388388
# on UTF-8 machines we install the en@quot messages too
389389
# TODO: streamline this -- en_quote is definitely doing some redundant stuff
390-
message('Generating en@quot translations')
390+
cat('Generating en@quot translations\n')
391391
update_en_quot_mo_files(dir, verbose)
392392
}
393393

394394

395395
if (is.null(languages)) {
396-
if (verbose) message('No languages provided; finishing')
396+
if (verbose) cat('No languages provided; finishing\n')
397397
return(invisible())
398398
}
399399

@@ -411,8 +411,8 @@ translate_package = function(
411411
lang_file <- file.path(po_dir, glue("R-{language}.po"))
412412
if (tr_update && file.exists(lang_file)) {
413413
if (verbose) {
414-
messagef(
415-
'Found existing R translations for %s (%s/%s) in %s. Running msgmerge...',
414+
catf(
415+
'Found existing R translations for %s (%s/%s) in %s. Running msgmerge...\n',
416416
language, metadata$full_name_eng, metadata$full_name_native, lang_file
417417
)
418418
}
@@ -426,8 +426,8 @@ translate_package = function(
426426
lang_file <- file.path(po_dir, glue("{language}.po"))
427427
if (tr_update && file.exists(lang_file)) {
428428
if (verbose) {
429-
messagef(
430-
'Found existing src translations for %s (%s/%s) in %s. Running msgmerge...',
429+
catf(
430+
'Found existing src translations for %s (%s/%s) in %s. Running msgmerge...\n',
431431
language, metadata$full_name_eng, metadata$full_name_native, lang_file
432432
)
433433
}
@@ -448,15 +448,15 @@ translate_package = function(
448448
]
449449

450450
if (!length(new_idx)) {
451-
if (verbose) messagef('Translations for %s are up to date! Skipping.', language)
451+
if (verbose) catf('Translations for %s are up to date! Skipping.\n', language)
452452
next
453453
}
454454
if (verbose) {
455-
messagef(
456-
'Beginning new translations for %s (%s/%s); found %d untranslated messages',
455+
catf(
456+
'Beginning new translations for %s (%s/%s); found %d untranslated messages\n',
457457
language, metadata$full_name_eng, metadata$full_name_native, length(new_idx)
458458
)
459-
message("(To quit translating, press 'Esc'; progress will be saved)")
459+
cat("(To quit translating, press 'Esc'; progress will be saved)\n")
460460
}
461461

462462
po_params$author = prompt('Thanks! Who should be credited with these translations?')

0 commit comments

Comments
 (0)