-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmsgmerge.R
More file actions
62 lines (55 loc) · 2.28 KB
/
msgmerge.R
File metadata and controls
62 lines (55 loc) · 2.28 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
# split off from tools::update_pkg_po() to only run the msgmerge & checkPoFile steps
run_msgmerge = function(po_file, pot_file) {
if (system(sprintf("msgmerge --update %s %s", po_file, shQuote(pot_file))) != 0L) {
# nocov these warnings? i don't know how to trigger them as of this writing.
warning(domain = NA, gettextf("Running msgmerge on '%s' failed.", po_file))
}
res <- checkPoFile(po_file, strictPlural = TRUE)
if (nrow(res)) {
warning(domain = NA, gettextf("tools::checkPoFile() found some issues in %s", po_file))
print(res)
}
return(invisible())
}
run_msgfmt = function(po_file, mo_file, verbose) {
use_stats <- if (verbose) '--statistics' else ''
if (system(sprintf("msgfmt -c %s -o %s %s", use_stats, shQuote(mo_file), shQuote(po_file))) != 0L) {
warning(domain = NA, gettextf("running msgfmt on %s failed", basename(po_file)), immediate. = TRUE)
}
return(invisible())
}
update_mo_files = function(dir, package, verbose) {
inst_dir <- file.path(dir, "inst", "po")
lang_regex <- "^(R-)?([a-zA-Z_]+)\\.po$"
po_files <- list.files(file.path(dir, "po"), pattern = "\\.po$")
languages <- gsub(lang_regex, "\\2", po_files)
mo_files <- gsub(lang_regex, sprintf("\\1%s.mo", package), po_files)
mo_dirs <- file.path(inst_dir, languages, "LC_MESSAGES")
# NB: dir.create() only accepts one directory at a time...
for (mo_dir in unique(mo_dirs)) dir.create(mo_dir, recursive = TRUE, showWarnings = FALSE)
for (ii in seq_along(po_files)) {
run_msgfmt(
po_file = file.path(dir, "po", po_files[ii]),
mo_file = file.path(mo_dirs[ii], mo_files[ii]),
verbose = verbose
)
}
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) {
po_file <- tempfile()
# 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
)
unlink(po_file)
}
return(invisible())
}