-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmsgmerge.R
More file actions
93 lines (84 loc) · 3.5 KB
/
msgmerge.R
File metadata and controls
93 lines (84 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# split off from tools::update_pkg_po() to only run the msgmerge & checkPoFile steps
# https://www.gnu.org/software/gettext/manual/html_node/msgmerge-Invocation.html
# https://docs.oracle.com/cd/E36784_01/html/E36870/msgmerge-1.html#scrolltoc
run_msgmerge <- function(po_file, pot_file, previous = FALSE, verbose = TRUE) {
args <- c(
"--update", shQuote(path.expand(po_file)),
"--backup=off",
if (previous) "--previous", #show previous match for fuzzy matches
shQuote(path.expand(pot_file))
)
val <- system2("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"))
}
res <- tools::checkPoFile(po_file, strictPlural = TRUE)
if (nrow(res)) {
warningf("tools::checkPoFile() found some issues in %s", po_file)
print(res)
}
return(invisible())
}
run_msgfmt = function(po_file, mo_file, verbose) {
po_file <- path.expand(po_file)
mo_file <- path.expand(mo_file)
# See #218. Solaris msgfmt (non-GNU on CRAN) doesn't support --check or --statistics;
# see also https://bugs.r-project.org/show_bug.cgi?id=18150
args = character()
if (is_gnu_gettext()) {
args = c("--check", if (verbose) '--statistics')
}
val = system2("msgfmt", c(args, "-o", shQuote(mo_file), shQuote(po_file)), stdout = TRUE, stderr = TRUE)
if (!identical(attr(val, "status", exact = TRUE), NULL)) {
warningf(
"running msgfmt on %s failed; output:\n %s\nHere is the po file:\n%s",
basename(po_file), paste(val, collapse = "\n"), paste(readLines(po_file), collapse = "\n"),
immediate. = TRUE
)
} else if (verbose) {
messagef(
"running msgfmt on %s succeeded; output:\n %s",
basename(po_file), paste(val, collapse = "\n")
)
}
return(invisible())
}
update_en_quot_mo_files <- function(dir, verbose) {
pot_files <- list.files(file.path(dir, "po"), pattern = "\\.pot$", full.names = TRUE)
mo_dir <- file.path(dir, "inst", "po", "en@quot", "LC_MESSAGES")
dir.create(mo_dir, recursive = TRUE, showWarnings = FALSE)
for (pot_file in pot_files) {
# don't use tempfile() -- want a static basename() to keep verbose output non-random
po_file <- file.path(tempdir(), if (startsWith(basename(pot_file), "R-")) "R-en@quot.po" else "en@quot.po")
on.exit(unlink(po_file))
# tools:::en_quote is blocked, but we still need it for now
get("en_quote", envir=asNamespace("tools"))(pot_file, po_file)
run_msgfmt(
po_file = po_file,
mo_file = file.path(mo_dir, gsub("\\.pot$", ".mo", basename(pot_file))),
verbose = verbose
)
}
return(invisible())
}
# https://www.gnu.org/software/gettext/manual/html_node/msginit-Invocation.html
# https://docs.oracle.com/cd/E36784_01/html/E36870/msginit-1.html#scrolltoc
run_msginit <- function(po_path, pot_path, locale, width = 80, verbose = TRUE) {
args <- c(
"-i", shQuote(path.expand(pot_path)),
"-o", shQuote(path.expand(po_path)),
"-l", shQuote(locale),
"-w", width,
"--no-translator" # don't consult user-email etc
)
val <- system2("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"))
}
return(invisible())
}