|
| 1 | +#' Create a new `.po` file |
| 2 | +#' |
| 3 | +#' @description |
| 4 | +#' `po_create()` creates a new `po/{languages}.po` containing the messages to be |
| 5 | +#' translated. |
| 6 | +#' |
| 7 | +#' Generally, we expect you to use `po_create()` to create new `.po` files |
| 8 | +#' but if you call it with an existing translation, it will update it with any |
| 9 | +#' changes from the `.pot`. See [po_update()] for details. |
| 10 | +#' |
| 11 | +#' @param languages Language identifiers. These are typically two letters (e.g. |
| 12 | +#' "en" = English, "fr" = French, "es" = Spanish, "zh" = Chinese), but |
| 13 | +#' can include an additional suffix for languages that have regional |
| 14 | +#' variations (e.g. "fr_CN" = French Canadian, "zh_CN" = simplified |
| 15 | +#' characters as used in mainland China, "zh_TW" = traditional characters |
| 16 | +#' as used in Taiwan.) |
| 17 | +#' @inheritParams po_extract |
| 18 | +#' @export |
| 19 | +po_create <- function(languages, dir = ".", verbose = !is_testing()) { |
| 20 | + package <- get_desc_data(dir, "Package") |
| 21 | + po_files <- po_language_files(languages, dir) |
| 22 | + |
| 23 | + for (ii in seq_len(nrow(po_files))) { |
| 24 | + row <- po_files[ii] |
| 25 | + if (file.exists(row$po_path)) { |
| 26 | + if (verbose) messagef("Updating '%s' %s translation", row$language, row$type) |
| 27 | + run_msgmerge(row$po_path, row$pot_path, previous = TRUE, verbose = verbose) |
| 28 | + } else { |
| 29 | + if (verbose) messagef("Creating '%s' %s translation", row$language, row$type) |
| 30 | + run_msginit(row$po_path, row$pot_path, locale = row$language, verbose = verbose) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + invisible(po_files) |
| 35 | +} |
| 36 | + |
| 37 | +# TODO: make sure this works with translating/updating base, which |
| 38 | +# has the anti-pattern that src translations are in R.pot, not base.pot. |
| 39 | +po_language_files <- function(languages, dir = ".") { |
| 40 | + po_files <- data.table::CJ(type = pot_types(dir), language = languages) |
| 41 | + po_files[, "po_path" := file.path(dir, "po", paste0(po_prefix(po_files$type), po_files$language, ".po"))] |
| 42 | + po_files[, "pot_path" := pot_paths(dir, po_files$type)] |
| 43 | + po_files[] |
| 44 | +} |
| 45 | + |
| 46 | +# TODO: should this be po_paths, with a template=TRUE/FALSE argument? |
| 47 | +pot_paths <- function(dir, type, package = NULL) { |
| 48 | + if (is.null(package)) { |
| 49 | + package <- get_desc_data(dir, "Package") |
| 50 | + } |
| 51 | + if (length(type) == 0) { |
| 52 | + character() |
| 53 | + } else { |
| 54 | + file.path(dir, "po", paste0(po_prefix(type), package, ".pot")) |
| 55 | + } |
| 56 | + |
| 57 | +} |
| 58 | +po_prefix <- function(type = c("R", "src")) { |
| 59 | + data.table::fifelse(type == "R", "R-", "") |
| 60 | +} |
| 61 | +pot_types <- function(dir = ".") { |
| 62 | + types <- c("R", "src") |
| 63 | + types[file.exists(pot_paths(dir, types))] |
| 64 | +} |
0 commit comments