Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export po_compile() #234

Merged
merged 21 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Move po_compile() to own file
  • Loading branch information
hadley committed Nov 7, 2021
commit fb69e90f43365af1807a5ab3c65ee32921703523
64 changes: 0 additions & 64 deletions R/msgmerge.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,71 +35,7 @@ run_msgfmt = function(po_file, mo_file, verbose) {
return(invisible())
}

#' Compile `.po` files to `.mo`
#'
#' This function compiles the plain text `.po` files that translators work with
#' into the binary `.mo` files that are installed with packages and used for
#' live translations.
#'
#' @param dir Path to package root directory.
#' @param package Name of package. If not supplied, read from `DESCRIPTION`.
#' @param lazy If `TRUE`, only `.mo` functions that are older than `.po`
#' files be updated
#' @param verbose If `TRUE`, print information as it goes.
po_compile = function(dir = ".", package = NULL, lazy = TRUE, verbose = TRUE) {
po_metadata <- get_po_metadata(dir = dir, package = package)
dir_create(dirname(po_metadata$mo))

if (lazy) {
po_metadata <- po_metadata[is_outdated(po, mo)]
} else {
# Clear out all older translations
mo_dirs <- dir(file.path(dir, "inst", "po"), full.names = 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",
basename(dir), dirname(dir)
)
unlink(dir, recursive = TRUE)
}
}

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

return(invisible())
}

get_po_metadata <- function(dir = ".", package = NULL) {
if (is.null(package)) {
package <- get_desc_data(dir)[["Package"]]
}

lang_regex <- "^(R-)?([a-z]{2}(?:_[A-Z]{2})?)\\.po$"
po_paths <- list.files(file.path(dir, "po"), pattern = lang_regex, full.names = TRUE)

languages <- gsub(lang_regex, "\\2", basename(po_paths))
type <- ifelse(grepl("^(R-)", basename(po_paths)), "R", "src")

mo_names <- gsub(lang_regex, sprintf("\\1%s.mo", package), basename(po_paths))
mo_paths <- file.path(dir, "inst", "po", languages, "LC_MESSAGES", mo_names)

data.table(
language = languages,
type = type,
po = po_paths,
mo = mo_paths
)
}

update_en_quot_mo_files <- function(dir, verbose) {
pot_files <- list.files(file.path(dir, "po"), pattern = "\\.pot$", full.names = TRUE)
Expand Down
67 changes: 67 additions & 0 deletions R/po_compile.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#' Compile `.po` files to `.mo`
#'
#' This function compiles the plain text `.po` files that translators work with
#' into the binary `.mo` files that are installed with packages and used for
#' live translations.
#'
#' @param dir Path to package root directory.
#' @param package Name of package. If not supplied, read from `DESCRIPTION`.
#' @param lazy If `TRUE`, only `.mo` functions that are older than `.po`
#' files be updated
#' @param verbose If `TRUE`, print information as it goes.
po_compile = function(dir = ".", package = NULL, lazy = TRUE, verbose = TRUE) {
po_metadata <- get_po_metadata(dir = dir, package = package)
dir_create(dirname(po_metadata$mo))

if (lazy) {
po_metadata <- po_metadata[is_outdated(po, mo)]
} else {
# Clear out all older translations
mo_dirs <- dir(file.path(dir, "inst", "po"), full.names = 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",
basename(dir), dirname(dir)
)
unlink(dir, recursive = TRUE)
}
}

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

return(invisible())
}


get_po_metadata <- function(dir = ".", package = NULL) {
if (is.null(package)) {
package <- get_desc_data(dir)[["Package"]]
}

lang_regex <- "^(R-)?([a-z]{2}(?:_[A-Z]{2})?)\\.po$"
lang_regex <- "^(R-)?([a-z]{2}(?:_[A-Z]{2})?)\\.po$"
po_paths <- list.files(file.path(dir, "po"), pattern = lang_regex, full.names = TRUE)

languages <- gsub(lang_regex, "\\2", basename(po_paths))
type <- ifelse(grepl("^(R-)", basename(po_paths)), "R", "src")

mo_names <- gsub(lang_regex, sprintf("\\1%s.mo", package), basename(po_paths))
mo_paths <- file.path(dir, "inst", "po", languages, "LC_MESSAGES", mo_names)

data.table(
language = languages,
type = type,
po = po_paths,
mo = mo_paths
)
}
4 changes: 2 additions & 2 deletions man/po_compile.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions potools.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ StripTrailingWhitespace: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd