Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
for R < 4.0.0
require(tools)
update_pkg_po2 <- function (pkgdir, pkg = NULL, version = NULL, copyright, bugs)
{
same <- function(a, b) {
tmpa <- readLines(a)
tmpb <- readLines(b)
tmpa <- tools:::filtergrep("^\"POT-Creation-Date:", tmpa)
tmpb <- tools:::filtergrep("^\"POT-Creation-Date:", tmpb)
identical(tmpa, tmpb)
}
pwd <- getwd()
coll <- Sys.getlocale("LC_COLLATE")
on.exit({
Sys.setlocale("LC_COLLATE", coll)
setwd(pwd)
})
Sys.setlocale("LC_COLLATE", "C")
setwd(pkgdir)
dir.create("po", FALSE)
files <- dir("po")
desc <- "DESCRIPTION"
if (file.exists(desc)) {
desc <- read.dcf(desc, fields = c("Package", "Version"))
pkg <- name <- desc[1L]
version <- desc[2L]
if (missing(copyright))
copyright <- NULL
if (missing(bugs))
bugs <- NULL
stem <- file.path("inst", "po")
}
else {
pkg <- basename(pkgdir)
name <- "R"
version <- as.character(getRversion())
copyright <- "The R Core Team"
bugs <- "bugs.r-project.org"
stem <- file.path("..", "translations", "inst")
}
is_base <- (pkg == "base")
have_src <- paste0(pkg, ".pot") %in% files
ofile <- tempfile()
xgettext2pot(".", ofile, name, version, bugs)
potfile <- file.path("po", paste0("R-", pkg, ".pot"))
if (file.exists(potfile) && same(potfile, ofile)) {
}
else file.copy(ofile, potfile, overwrite = TRUE)
pofiles <- dir("po", pattern = "R-.*[.]po$", full.names = TRUE)
pofiles <- pofiles[pofiles != "po/R-en@quot.po"]
for (f in pofiles) {
lang <- sub("^R-(.*)[.]po$", "\\1", basename(f))
message(" R-", lang, ":", appendLF = FALSE, domain = NA)
cmd <- paste("msgmerge --update", f, shQuote(potfile))
if (system(cmd) != 0L) {
warning("running msgmerge on ", sQuote(f), " failed",
domain = NA)
next
}
res <- checkPoFile(f, TRUE)
if (nrow(res)) {
print(res)
message("not installing", domain = NA)
next
}
dest <- file.path(stem, lang, "LC_MESSAGES")
dir.create(dest, FALSE, TRUE)
dest <- file.path(dest, sprintf("R-%s.mo", pkg))
cmd <- paste("msgfmt -c --statistics -o", shQuote(dest),
shQuote(f))
if (system(cmd) != 0L)
warning(sprintf("running msgfmt on %s failed", basename(f)),
domain = NA, immediate. = TRUE)
}
print("ahosine")
if (l10n_info()[["UTF-8"]]) {
lang <- "en@quot"
message(" R-", lang, ":", domain = NA)
f <- tempfile()
en_quote(potfile, f)
dest <- file.path(stem, lang, "LC_MESSAGES")
dir.create(dest, FALSE, TRUE)
dest <- file.path(dest, sprintf("R-%s.mo", pkg))
cmd <- paste("msgfmt -c --statistics -o", shQuote(dest),
shQuote(f))
if (system(cmd) != 0L)
warning(sprintf("running msgfmt on %s failed", basename(f)),
domain = NA, immediate. = TRUE)
}
print("kusoga")
if (!(is_base || have_src))
return(invisible())
ofile <- tempfile()
if (!is_base) {
dom <- pkg
od <- setwd("src")
exts <- "[.](c|cc|cpp|m|mm)$"
cfiles <- dir(".", pattern = exts)
if (file.exists("windows"))
cfiles <- c(cfiles, dir("windows", pattern = exts,
full.names = TRUE))
}
else {
dom <- "R"
od <- setwd("../../..")
cfiles <- tools:::filtergrep("^#", readLines("po/POTFILES"))
}
cmd <- sprintf("xgettext --keyword=_ --keyword=N_ -o %s",
shQuote(ofile))
cmd <- c(cmd, paste0("--package-name=", name), paste0("--package-version=",
version), "--add-comments=TRANSLATORS:", if (!is.null(copyright)) sprintf("--copyright-holder=\"%s\"",
copyright), if (!is.null(bugs)) sprintf("--msgid-bugs-address=\"%s\"",
bugs), if (is_base) "-C")
cmd <- paste(c(cmd, cfiles), collapse = " ")
if (system(cmd) != 0L)
stop("running xgettext failed", domain = NA)
setwd(od)
potfile <- file.path("po", paste0(dom, ".pot"))
if (!same(potfile, ofile))
file.copy(ofile, potfile, overwrite = TRUE)
pofiles <- dir("po", pattern = "^[^R].*[.]po$", full.names = TRUE)
pofiles <- pofiles[pofiles != "po/en@quot.po"]
for (f in pofiles) {
lang <- sub("[.]po", "", basename(f))
message(" ", lang, ":", appendLF = FALSE, domain = NA)
cmd <- paste("msgmerge --update", shQuote(f), shQuote(potfile))
if (system(cmd) != 0L) {
warning("running msgmerge on ", f, " failed", domain = NA)
next
}
res <- checkPoFile(f, TRUE)
if (nrow(res)) {
print(res)
message("not installing", domain = NA)
next
}
dest <- file.path(stem, lang, "LC_MESSAGES")
dir.create(dest, FALSE, TRUE)
dest <- file.path(dest, sprintf("%s.mo", dom))
cmd <- paste("msgfmt -c --statistics -o", shQuote(dest),
shQuote(f))
if (system(cmd) != 0L)
warning(sprintf("running msgfmt on %s failed", basename(f)),
domain = NA)
}
print("unko")
if (l10n_info()[["UTF-8"]]) {
lang <- "en@quot"
message(" ", lang, ":", domain = NA)
f <- tempfile()
en_quote(potfile, f)
dest <- file.path(stem, lang, "LC_MESSAGES")
dir.create(dest, FALSE, TRUE)
dest <- file.path(dest, sprintf("%s.mo", dom))
cmd <- paste("msgfmt -c --statistics -o", shQuote(dest),
shQuote(f))
if (system(cmd) != 0L)
warning(sprintf("running msgfmt on %s failed", basename(f)),
domain = NA)
}
invisible()
}
en_quote <- function (potfile, outfile)
{
tfile <- tempfile()
cmd <- paste("msginit -i", potfile, "--no-translator -l en -o",
tfile)
if (system(cmd, ignore.stderr = TRUE) != 0L)
stop("running msginit failed", domain = NA)
tfile2 <- tempfile()
cmd <- paste("msgconv -t UTF-8 -o", tfile2, tfile)
if (system(cmd) != 0L)
stop("running msgconv failed", domain = NA)
lines <- readLines(tfile2)
starts <- which(startsWith(lines, "msgstr"))
current <- 1L
out <- character()
for (s in starts) {
if (current < s)
out <- c(out, lines[seq.int(current, s - 1L, 1L)])
start <- sub("([^\"]*)\"(.*)\"$", "\\1", lines[s])
this <- sub("([^\"]*)\"(.*)\"$", "\\2", lines[s])
current <- s + 1L
while (startsWith(lines[current], "\"") && !is.na(lines[current])) {
this <- c(this, sub("^\"(.*)\"$", "\\1", lines[current]))
current <- current + 1L
}
nc <- nchar(this)
n <- length(nc)
this <- paste0(this, collapse = "")
this <- gsub("^'([^`']*)'", "‘\\1’", this)
this <- gsub("([^[:alpha:]]|\\\\n)'([^`']*)'", "\\1‘\\2’",
this)
out <- if (n > 1L) {
this1 <- character()
sc <- c(0, cumsum(nc))
for (i in seq_along(nc)) {
if (!nc[i])
this1 <- c(this1, "")
else {
this1 <- c(this1, substr(this, sc[i] + 1L,
sc[i + 1]))
}
}
c(out, paste0(start, "\"", this1[1L], "\""), paste0("\"",
this1[-1L], "\""))
}
else c(out, paste0(start, "\"", this, "\""))
}
if (current <= length(lines))
out <- c(out, lines[seq.int(current, length(lines), 1L)])
con <- file(outfile, "wb")
writeLines(out, con, useBytes = TRUE)
close(con)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment