Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 11 additions & 7 deletions R/custom-translators.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
catf = function(fmt, ..., sep=" ", domain="R-potools") {
cat(gettextf(fmt, ..., domain=domain), sep=sep)
}

# tends to make some really repetitive messages with call.=TRUE, so always turn it off
stopf = function(fmt, ..., domain = NULL) {
stop(gettextf(fmt, ..., domain = NULL), domain = NA, call. = FALSE)
stopf = function(fmt, ..., domain="R-potools") {
stop(gettextf(fmt, ..., domain=domain), domain = NA, call. = FALSE)
}

# uncovered for now, used in msgmerge.R
warningf = function(fmt, ..., immediate. = FALSE, noBreaks. = FALSE) {
warning(gettextf(fmt, ..., domain = NULL), domain = NA, call. = FALSE, immediate. = immediate., noBreaks. = noBreaks.)
warningf = function(fmt, ..., immediate.=FALSE, noBreaks.=FALSE, domain="R-potools") {
warning(gettextf(fmt, ..., domain=domain), domain = NA, call. = FALSE, immediate. = immediate., noBreaks. = noBreaks.)
}

messagef = function(fmt, ..., appendLF = TRUE) {
message(gettextf(fmt, ..., domain = NULL), domain = NA, appendLF = appendLF)
messagef = function(fmt, ..., appendLF=TRUE, domain="R-potools") {
message(gettextf(fmt, ..., domain=domain), domain = NA, appendLF = appendLF)
}

# not actually used yet in package src, so commenting out for test coverage, but leaving in for illustration
# packageStartupMessagef = function(fmt, ..., appendLF = TRUE) {
# packageStartupMessage(gettextf(fmt, ..., domain = NULL), domain = NA, appendLF = appendLF)
# packageStartupMessage(gettextf(fmt, ..., domain = domain), domain = NA, appendLF = appendLF)
# }
7 changes: 4 additions & 3 deletions R/explain_plurals.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ po_explain_plurals <- function(language, index) {

plural_metadata = .potools$PLURAL_RANGE_STRINGS[language_metadata, .SD, on = "plural"]
if (missing(index)) {
language_metadata[, message(domain = NA, sprintf(
language_metadata[, messagef(
ngettext(nplurals, "%s (%s) has %d plural form.", "%s (%s) has %d plural forms."),
full_name_eng, full_name_native, nplurals
))]
full_name_eng, full_name_native, nplurals,
domain = NA
)]
plural_metadata[, message(domain = NA, paste(
gettextf(" - plural_index = %d applies %s", plural_index, range_translation = gettext(range)),
collapse = "\n"
Expand Down
16 changes: 11 additions & 5 deletions R/find_fuzzy_messages.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ find_fuzzy_messages <- function(message_data, lang_file) {
old_message_data = get_po_messages(lang_file)

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

dashes = strrep('-', 0.9*getOption('width'))
old_message_data[idx & type == 'singular', {
if (.N > 0L) {
message(' ** SINGULAR MESSAGES **')
cat(' ** SINGULAR MESSAGES **\n')
cat(rbind(dashes, msgid, msgstr), sep='\n')
}
}]
old_message_data[idx & type == 'plural', {
if (.N > 0L) {
message(' ** PLURAL MESSAGES **')
cat(' ** PLURAL MESSAGES **\n')
cat(do.call(rbind, c(list(dashes), msgid_plural, msgstr_plural)), sep='\n')
}
}]
Expand Down
4 changes: 2 additions & 2 deletions R/get_message_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ get_message_data = function(
}
}

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

if (verbose && dir.exists(file.path(dir, "src"))) message('Getting src-level messages...')
if (verbose && dir.exists(file.path(dir, "src"))) cat('Getting src-level messages...\n')
src_message_data = get_src_messages(
dir,
custom_translation_functions = custom_translation_functions$src,
Expand Down
10 changes: 5 additions & 5 deletions R/msgmerge.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ run_msgmerge <- function(po_file, pot_file, previous = FALSE, verbose = TRUE) {
)

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

res <- tools::checkPoFile(po_file, strictPlural = TRUE)
Expand All @@ -45,7 +45,7 @@ run_msgfmt = function(po_file, mo_file, verbose) {
cmd = glue("msgfmt -o {shQuote(mo_file)} {shQuote(po_file)}") # nocov
}
if (verbose) {
message("Running system command ", cmd, "...")
catf("Running system command %s ...\n", cmd)
}
if (system(cmd) != 0L) {
warningf(
Expand Down Expand Up @@ -88,13 +88,13 @@ run_msginit <- function(po_path, pot_path, locale, width = 80L, verbose = TRUE)
"--no-translator" # don't consult user-email etc
)
if (verbose) {
message("Running system command msginit ", paste(msginit_args, collapse = " "), "...")
catf("Running system command msginit %s...\n", paste(msginit_args, collapse = " "))
}
val <- system2("msginit", msginit_args, stdout = TRUE, stderr = TRUE)
if (!identical(attr(val, "status", exact = TRUE), NULL)) {
stopf("Running msginit on '%s' failed", pot_path)
} else if (verbose) {
messagef(paste(val, collapse = "\n"))
writeLines(val)
}
invisible()
}
6 changes: 3 additions & 3 deletions R/po_compile.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ po_compile = function(dir = ".", package = NULL, lazy = TRUE, verbose = TRUE) {
to_delete <- mo_dirs[!basename(mo_dirs) %in% c(po_metadata$language, "en@quot")]

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

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

Expand Down
4 changes: 2 additions & 2 deletions R/po_create.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ po_create <- function(languages, dir = ".", verbose = !is_testing()) {
for (ii in seq_len(nrow(po_files))) {
row_ii <- po_files[ii]
if (file.exists(row_ii$po_path)) {
if (verbose) messagef("Updating '%s' %s translation", row_ii$language, row_ii$type)
if (verbose) catf("Updating '%s' %s translation\n", row_ii$language, row_ii$type)
run_msgmerge(row_ii$po_path, row_ii$pot_path, previous = TRUE, verbose = verbose)
} else {
if (verbose) messagef("Creating '%s' %s translation", row_ii$language, row_ii$type)
if (verbose) catf("Creating '%s' %s translation\n", row_ii$language, row_ii$type)
run_msginit(row_ii$po_path, row_ii$pot_path, locale = row_ii$language, verbose = verbose)
}
}
Expand Down
6 changes: 2 additions & 4 deletions R/po_extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ po_extract <- function(

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

po_dir <- file.path(dir, 'po')
dir.create(po_dir, showWarnings = FALSE)
Expand Down
2 changes: 1 addition & 1 deletion R/po_update.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ po_update <- function(dir = ".", lazy = TRUE, verbose = !is_testing()) {

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

Expand Down
30 changes: 15 additions & 15 deletions R/translate_package.R
Original file line number Diff line number Diff line change
Expand Up @@ -344,21 +344,21 @@ translate_package = function(
if (tr_update) {
# is it worthwhile to try and distinguish the creation time of the
# R pot file and the src pot file? probably not...
messagef(
"Updating translation template for package '%s' (last updated %s)",
catf(
"Updating translation template for package '%s' (last updated %s)\n",
package,
format(file.info(r_potfile)$atime)
)
} else {
messagef("Starting translations for package '%s'", package)
catf("Starting translations for package '%s'\n", package)
}
}
if (!tr_update) dir.create(po_dir, showWarnings = FALSE)

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

if (!nrow(message_data)) {
if (verbose) message('No messages to translate; finishing')
if (verbose) cat('No messages to translate; finishing\n')
return(invisible())
}

Expand All @@ -369,7 +369,7 @@ translate_package = function(
set_prompt_conn()
on.exit(unset_prompt_conn())

if (verbose) message('Running message diagnostics...')
if (verbose) cat('Running message diagnostics...\n')

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


if (is.null(languages)) {
if (verbose) message('No languages provided; finishing')
if (verbose) cat('No languages provided; finishing\n')
return(invisible())
}

Expand All @@ -411,8 +411,8 @@ translate_package = function(
lang_file <- file.path(po_dir, glue("R-{language}.po"))
if (tr_update && file.exists(lang_file)) {
if (verbose) {
messagef(
'Found existing R translations for %s (%s/%s) in %s. Running msgmerge...',
catf(
'Found existing R translations for %s (%s/%s) in %s. Running msgmerge...\n',
language, metadata$full_name_eng, metadata$full_name_native, lang_file
)
}
Expand All @@ -426,8 +426,8 @@ translate_package = function(
lang_file <- file.path(po_dir, glue("{language}.po"))
if (tr_update && file.exists(lang_file)) {
if (verbose) {
messagef(
'Found existing src translations for %s (%s/%s) in %s. Running msgmerge...',
catf(
'Found existing src translations for %s (%s/%s) in %s. Running msgmerge...\n',
language, metadata$full_name_eng, metadata$full_name_native, lang_file
)
}
Expand All @@ -448,15 +448,15 @@ translate_package = function(
]

if (!length(new_idx)) {
if (verbose) messagef('Translations for %s are up to date! Skipping.', language)
if (verbose) catf('Translations for %s are up to date! Skipping.\n', language)
next
}
if (verbose) {
messagef(
'Beginning new translations for %s (%s/%s); found %d untranslated messages',
catf(
'Beginning new translations for %s (%s/%s); found %d untranslated messages\n',
language, metadata$full_name_eng, metadata$full_name_native, length(new_idx)
)
message("(To quit translating, press 'Esc'; progress will be saved)")
cat("(To quit translating, press 'Esc'; progress will be saved)\n")
}

po_params$author = prompt('Thanks! Who should be credited with these translations?')
Expand Down
8 changes: 5 additions & 3 deletions R/update_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
update_metadata = function(language) {
metadata = data.table(code = language)

messagef("'%s' is not a known language. ", language, appendLF=FALSE)
# perhaps refer to http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html instead
message("Please help supply some metadata about it. You can check https://l10n.gnome.org/teams/<language>")
catf(
"'%s' is not a known language. Please help supply some metadata about it. You can check %s.\n",
language, "https://l10n.gnome.org/teams/<language>"
)
metadata[ , "full_name_eng" := prompt("How would you refer to this language in English?")]
metadata[ , "full_name_native" := prompt("How would you refer to this language in the language itself?")]
metadata[ , "nplurals" := prompt(
Expand Down Expand Up @@ -38,7 +40,7 @@ update_metadata = function(language) {
}
.potools$KNOWN_LANGUAGES = rbind(.potools$KNOWN_LANGUAGES, metadata)
setkeyv(.potools$KNOWN_LANGUAGES, "code")
message("Thanks! Please file an issue on GitHub to get this language recognized permanently")
cat("Thanks! Please file an issue on GitHub to get this language recognized permanently\n")
metadata
}

Expand Down
2 changes: 1 addition & 1 deletion R/write_po_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ write_po_files <- function(message_data, po_dir, params, template = FALSE, use_b
))
}

if (verbose) messagef('Writing %s', r_file)
if (verbose) catf('Writing %s\n', r_file)

write_po_file(
message_data[message_source == "R"],
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/unix/po_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Code
po_create("jp", verbose = TRUE)
Message
Output
Creating 'jp' R translation
Running system command msginit -i './po/R-test.pot' -o './po/R-jp.po' -l 'jp' -w 80 --no-translator...
Created ./po/R-jp.po.
Expand All @@ -11,8 +11,8 @@

Code
po_create("jp", verbose = TRUE)
Message
Output
Updating 'jp' R translation
Running system command msgmerge --update './po/R-jp.po' --backup=off --previous './po/R-test.pot'...
Running system command msgmerge --update './po/R-jp.po' --backup=off --previous './po/R-test.pot' ...
. done.

6 changes: 3 additions & 3 deletions tests/testthat/_snaps/unix/po_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

Code
po_update(verbose = TRUE, lazy = FALSE)
Message
Output
Updating 'fr' R translation
Running system command msgmerge --update './po/R-fr.po' --backup=off --previous './po/R-test.pot'.
Running system command msgmerge --update './po/R-fr.po' --backup=off --previous './po/R-test.pot' .
. done.
Updating 'ja' R translation
Running system command msgmerge --update './po/R-ja.po' --backup=off --previous './po/R-test.pot'.
Running system command msgmerge --update './po/R-ja.po' --backup=off --previous './po/R-test.pot' .
. done.

6 changes: 3 additions & 3 deletions tests/testthat/_snaps/windows/po_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Code
po_create("jp", verbose = TRUE)
Message
Output
Creating 'jp' R translation
Running system command msginit -i "./po/R-test.pot" -o "./po/R-jp.po" -l "jp" -w 80 --no-translator...
Created ./po/R-jp.po.
Expand All @@ -11,8 +11,8 @@

Code
po_create("jp", verbose = TRUE)
Message
Output
Updating 'jp' R translation
Running system command msgmerge --update "./po/R-jp.po" --backup=off --previous "./po/R-test.pot"...
Running system command msgmerge --update "./po/R-jp.po" --backup=off --previous "./po/R-test.pot" ...
. done.

6 changes: 3 additions & 3 deletions tests/testthat/_snaps/windows/po_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

Code
po_update(verbose = TRUE, lazy = FALSE)
Message
Output
Updating 'fr' R translation
Running system command msgmerge --update "./po/R-fr.po" --backup=off --previous "./po/R-test.pot".
Running system command msgmerge --update "./po/R-fr.po" --backup=off --previous "./po/R-test.pot" .
. done.
Updating 'ja' R translation
Running system command msgmerge --update "./po/R-ja.po" --backup=off --previous "./po/R-test.pot".
Running system command msgmerge --update "./po/R-ja.po" --backup=off --previous "./po/R-test.pot" .
. done.

2 changes: 1 addition & 1 deletion tests/testthat/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ restore_package <- function(dir, expr, tmp_conn) {
on.exit(options(old), add = TRUE)
}

invisible(capture.output(expr))
invisible(expr)
}

# TODO: I think this can just be replaced by expect_match and expect_no_match in current testthat dev
Expand Down
Loading