|
| 1 | +# Use msgfmt to check for untranslated/fuzzy messages, and for whether |
| 2 | +# the implied .mo compiled form matches that which is already checked in |
| 3 | +msgfmt_linter <- function(po_file) { |
| 4 | + mo_tmp <- tempfile() |
| 5 | + on.exit(unlink(mo_tmp)) |
| 6 | + |
| 7 | + res = system2("msgfmt", c("--statistics", po_file, "-o", mo_tmp)), stdout=TRUE, stderr=TRUE) |
| 8 | + if (any(grepl("untranslated message|fuzzy translation", res))) { |
| 9 | + cat(sprintf("In %s, found incomplete translations:\n%s\n", po_file, paste(res, collapse="\n"))) |
| 10 | + stop("Please fix.") |
| 11 | + } |
| 12 | + |
| 13 | + mo_ref = sprintf( |
| 14 | + "inst/%s/LC_MESSAGES/%sdata.table.mo", |
| 15 | + gsub("^R-|[.]po$", "", po_file), |
| 16 | + if (startsWith(basename(po_file), "R-")) "R-" else "" |
| 17 | + ) |
| 18 | + |
| 19 | + if (!file.exists(mo_ref)) { |
| 20 | + stop(po_file, " has not been compiled as ", mo_ref, ". Please fix.") |
| 21 | + } |
| 22 | + if (tools::md5sum(mo_ref) == tools::md5sum(mo_tmp)) return(invisible()) |
| 23 | + |
| 24 | + # NB: file.mtime() will probably be wrong, it will reflect the check-out time of the git repo. |
| 25 | + last_edit_time = system2("git", |
| 26 | + c("log", "-1", '--format="%ad"', "--date=format:'%Y-%m-%d %H:%M:%S'", "--", mo_ref), |
| 27 | + stdout=TRUE |
| 28 | + ) |
| 29 | + cat(sprintf( |
| 30 | + ".mo compilation %s of .po translation %s appears out of date! It was last updated %s\n", |
| 31 | + mo_ref, po_file, last_edit_time |
| 32 | + )) |
| 33 | + |
| 34 | + unmo_tmp = tempfile() |
| 35 | + unmo_ref = tempfile() |
| 36 | + on.exit(unlink(c(unmo_tmp, unmo_ref), add=TRUE)) |
| 37 | + system2("msgunfmt", c(mo_tmp, "-o", unmo_tmp)) |
| 38 | + system2("msgunfmt", c(mo_ref, "-o", unmo_ref)) |
| 39 | + cat("Here are the observed differences after converting back to .po:\n\n") |
| 40 | + system2("diff", c(unmo_tmp, unmo_ref)) |
| 41 | + stop("Please fix.") |
| 42 | +} |
0 commit comments