-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement po_create()/po_update() for creating/updating translations (#…
…235) * Implement tr_add() for adding new translations * Add or update as necessary * Only add previous for tr_add() * Fix typo * Split tr_add() into po_create() and po_update() * lang->languages * lang->languages * Mark .pot file as UTF-8 * Split po_create() and po_update() into pieces And fundamentally change approach * Fix broken tests * Add test for po_create() Bringing in system2() code from #257 * WS * Extract out local_test_package() helper * Extract & test po_language_files() * Add tests for create and update And fix the bugs thus revealed * Add missing line * might as well use fifelse * Add links to solaris docs * Revert unintentional change * Move local_test_package() to better home * Revert CHARSET -> UTF-8 change * More docs about updating * Standardise number of dots * Improve docs * Tweak messaging * Revert accidental doc changes * add TODO * another TODO * typo * clarify fuzzy description * comment need for standardise_dots & americanize 🇺🇸 Co-authored-by: Michael Chirico <[email protected]>
- Loading branch information
1 parent
ff188d4
commit e9f85ff
Showing
15 changed files
with
321 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#' Create a new `.po` file | ||
#' | ||
#' @description | ||
#' `po_create()` creates a new `po/{languages}.po` containing the messages to be | ||
#' translated. | ||
#' | ||
#' Generally, we expect you to use `po_create()` to create new `.po` files | ||
#' but if you call it with an existing translation, it will update it with any | ||
#' changes from the `.pot`. See [po_update()] for details. | ||
#' | ||
#' @param languages Language identifiers. These are typically two letters (e.g. | ||
#' "en" = English, "fr" = French, "es" = Spanish, "zh" = Chinese), but | ||
#' can include an additional suffix for languages that have regional | ||
#' variations (e.g. "fr_CN" = French Canadian, "zh_CN" = simplified | ||
#' characters as used in mainland China, "zh_TW" = traditional characters | ||
#' as used in Taiwan.) | ||
#' @inheritParams po_extract | ||
#' @export | ||
po_create <- function(languages, dir = ".", verbose = !is_testing()) { | ||
package <- get_desc_data(dir, "Package") | ||
po_files <- po_language_files(languages, dir) | ||
|
||
for (ii in seq_len(nrow(po_files))) { | ||
row <- po_files[ii] | ||
if (file.exists(row$po_path)) { | ||
if (verbose) messagef("Updating '%s' %s translation", row$language, row$type) | ||
run_msgmerge(row$po_path, row$pot_path, previous = TRUE, verbose = verbose) | ||
} else { | ||
if (verbose) messagef("Creating '%s' %s translation", row$language, row$type) | ||
run_msginit(row$po_path, row$pot_path, locale = row$language, verbose = verbose) | ||
} | ||
} | ||
|
||
invisible(po_files) | ||
} | ||
|
||
# TODO: make sure this works with translating/updating base, which | ||
# has the anti-pattern that src translations are in R.pot, not base.pot. | ||
po_language_files <- function(languages, dir = ".") { | ||
po_files <- data.table::CJ(type = pot_types(dir), language = languages) | ||
po_files[, "po_path" := file.path(dir, "po", paste0(po_prefix(po_files$type), po_files$language, ".po"))] | ||
po_files[, "pot_path" := pot_paths(dir, po_files$type)] | ||
po_files[] | ||
} | ||
|
||
# TODO: should this be po_paths, with a template=TRUE/FALSE argument? | ||
pot_paths <- function(dir, type, package = NULL) { | ||
if (is.null(package)) { | ||
package <- get_desc_data(dir, "Package") | ||
} | ||
if (length(type) == 0) { | ||
character() | ||
} else { | ||
file.path(dir, "po", paste0(po_prefix(type), package, ".pot")) | ||
} | ||
|
||
} | ||
po_prefix <- function(type = c("R", "src")) { | ||
data.table::fifelse(type == "R", "R-", "") | ||
} | ||
pot_types <- function(dir = ".") { | ||
types <- c("R", "src") | ||
types[file.exists(pot_paths(dir, types))] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#' Update all `.po` files with changes in `.pot` | ||
#' | ||
#' @description | ||
#' `po_update()` updates existing `.po` file after the `.pot` file has changed. | ||
#' There are four cases: | ||
#' | ||
#' * New messages: added with blank `msgstr`. | ||
#' | ||
#' * Deleted messages: marked as deprecated and moved to the bottom of the file. | ||
#' | ||
#' * Major changes to existing messages: appear as an addition and a deletion. | ||
#' | ||
#' * Minor changes to existing messages: will be flagged as fuzzy. | ||
#' | ||
#' ``` | ||
#' #, fuzzy, c-format | ||
#' #| msgid "Generating en@quot translations" | ||
#' msgid "Updating '%s' %s translation" | ||
#' msgstr "en@quot翻訳生成中。。。" | ||
#' ``` | ||
#' | ||
#' The previous message is given in comments starting with `#|`. | ||
#' Translators need to update the actual (uncommented) `msgstr` manually, | ||
#' using the old `msgid` as a potential reference, then | ||
#' delete the old translation and the `fuzzy` comment (c-format should | ||
#' remain, if present). | ||
#' | ||
#' @inheritParams po_extract | ||
#' @param lazy If `TRUE`, only `.po` files that are older than their | ||
#' corresponding `.pot` file will be updated. | ||
#' @export | ||
po_update <- function(dir = ".", lazy = TRUE, verbose = !is_testing()) { | ||
meta <- get_po_metadata(dir) | ||
if (lazy) { | ||
meta <- meta[is_outdated(meta$po, meta$pot)] | ||
} | ||
|
||
for (ii in seq_len(nrow(meta))) { | ||
row <- meta[ii] | ||
if (verbose) messagef("Updating '%s' %s translation", row$language, row$type) | ||
run_msgmerge(row$po, row$pot, previous = TRUE, verbose = verbose) | ||
} | ||
|
||
invisible(meta) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# the user is told what's happening | ||
|
||
Code | ||
po_create("jp", verbose = TRUE) | ||
Message <simpleMessage> | ||
Creating 'jp' R translation | ||
Created ./po/R-jp.po. | ||
|
||
--- | ||
|
||
Code | ||
po_create("jp", verbose = TRUE) | ||
Message <simpleMessage> | ||
Updating 'jp' R translation | ||
. done. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# user is told what's happening | ||
|
||
Code | ||
po_update(verbose = TRUE, lazy = FALSE) | ||
Message <simpleMessage> | ||
Updating 'fr' R translation | ||
. done. | ||
Updating 'ja' R translation | ||
. done. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
# metadata ---------------------------------------------------------------- | ||
|
||
test_that("can find R and src translations", { | ||
temp <- withr::local_tempdir() | ||
dir.create(file.path(temp, "po")) | ||
temp <- local_test_package() | ||
file.create(file.path(temp, "po", c("R-en.po", "en.po"))) | ||
|
||
meta <- withr::with_dir(temp, get_po_metadata(package = "test")) | ||
meta <- withr::with_dir(temp, get_po_metadata()) | ||
expect_equal(meta$language, c("en", "en")) | ||
expect_setequal(meta$type, c("R", "src")) | ||
}) | ||
|
||
test_that("get_po_metadata() returns 0 rows if no .po fles", { | ||
temp <- local_test_package() | ||
meta <- get_po_metadata(temp) | ||
expect_equal(nrow(meta), 0) | ||
}) |
Oops, something went wrong.