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

Implement po_create()/po_update() for creating/updating translations #235

Merged
merged 36 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
3affcc0
Implement tr_add() for adding new translations
MichaelChirico Jul 12, 2021
63dcbc5
Add or update as necessary
hadley Nov 1, 2021
cfe1eff
Only add previous for tr_add()
hadley Nov 1, 2021
b205c34
Fix typo
hadley Nov 1, 2021
6124465
Split tr_add() into po_create() and po_update()
hadley Nov 4, 2021
c644cb7
lang->languages
MichaelChirico Nov 6, 2021
a088663
lang->languages
MichaelChirico Nov 6, 2021
28f3e28
Merge commit 'bd9c91a2abc717fe52b56cf6879f83f233a299a1'
hadley Nov 9, 2021
310f2b0
Move to own file
hadley Nov 9, 2021
10e16fd
Mark .pot file as UTF-8
hadley Nov 9, 2021
42885b1
Split po_create() and po_update() into pieces
hadley Nov 9, 2021
205ee8f
Fix broken tests
hadley Nov 9, 2021
0d3391d
Add test for po_create()
hadley Nov 9, 2021
d8880e0
WS
hadley Nov 9, 2021
efa92b6
Extract out local_test_package() helper
hadley Nov 9, 2021
511fad1
Extract & test po_language_files()
hadley Nov 9, 2021
d7c3882
Add tests for create and update
hadley Nov 9, 2021
0daabcd
Add missing line
hadley Nov 9, 2021
b604281
might as well use fifelse
MichaelChirico Nov 10, 2021
ef5449f
Add links to solaris docs
hadley Nov 10, 2021
7322e55
Revert unintentional change
hadley Nov 10, 2021
78afb31
Move local_test_package() to better home
hadley Nov 10, 2021
7133453
Revert CHARSET -> UTF-8 change
hadley Nov 10, 2021
df99ea4
Merge commit '3eaf727e03b1238d01492b99f58f938587b4b73b'
hadley Nov 10, 2021
f081b01
Merged origin/add-translation into add-translation
hadley Nov 10, 2021
6fd0c30
More docs about updating
hadley Nov 10, 2021
0e4371c
Standardise number of dots
hadley Nov 10, 2021
4b3353f
Improve docs
hadley Nov 10, 2021
ae5c5b7
Tweak messaging
hadley Nov 10, 2021
b4e71bd
Merge branch 'master' into add-translation
hadley Nov 10, 2021
b8e17e9
Revert accidental doc changes
hadley Nov 10, 2021
0f93068
add TODO
MichaelChirico Nov 11, 2021
3ff40d5
another TODO
MichaelChirico Nov 11, 2021
5efd88f
typo
MichaelChirico Nov 11, 2021
e6788bd
clarify fuzzy description
MichaelChirico Nov 11, 2021
a36031a
comment need for standardise_dots & americanize 🇺🇸
MichaelChirico Nov 11, 2021
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ URL: https://github.com/MichaelChirico/potools
BugReports: https://github.com/MichaelChirico/potools/issues
Encoding: UTF-8
VignetteBuilder: knitr
RoxygenNote: 7.1.2
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export(translate_package)
export(get_message_data)
export(write_po_file, po_metadata)

export(po_create)
export(po_update)

export(check_cracked_messages, check_untranslated_cat, check_untranslated_src)

export(check_potools_sys_reqs)
Expand Down
76 changes: 74 additions & 2 deletions R/msgmerge.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# 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) {

# https://www.gnu.org/software/gettext/manual/html_node/msgmerge-Invocation.html
run_msgmerge = function(po_file, pot_file, previous = FALSE) {
cmd <- paste("msgmerge",
"--update",
if (previous) "--previous", # show previous match for fuzzy matches
shQuote(path.expand(po_file)),
shQuote(path.expand(pot_file))
)

if (system(cmd) != 0L) {
# nocov these warnings? i don't know how to trigger them as of this writing.
warningf("Running msgmerge on '%s' failed.", po_file)
}
Expand Down Expand Up @@ -70,3 +79,66 @@ update_en_quot_mo_files <- function(dir, verbose) {
}
return(invisible())
}



#' Create or update a `.po` file containing translations
#'
#' @description
#' * `po_create()` creates a new `po/{languages}.po` containing the messages to be
#' translated.
#' * `po_update()` updates an existing `.po` file after the messages in a
#' package have changed. The translations for existing messages are preserved;
#' new messages are added; and translations for deleted message are marked
#' as deprecated and moved to the bottom of the file.
#'
#' @param languages Language identifiers. These are typically two letters (e.g.
hadley marked this conversation as resolved.
Show resolved Hide resolved
#' "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.)
#' @param dir Path to package root.
#' @param verbose If `TRUE`, explain what's happening.
po_create <- function(languages, dir = ".", verbose = TRUE) {
package <- get_desc_data(dir)[["Package"]]
po_path <- po_path(dir, languages)
if (file.exists(po_path)) {
po_update(languages, dir = dir, verbose = verbose)
return(invisible())
}

if (verbose) messagef("Adding new %s translation", languages)
run_msginit(pot_path(dir, package), po_path, languages)
}

#' @rdname po_create
po_update <- function(languages, dir = ".", verbose = TRUE) {
package <- get_desc_data(dir)[["Package"]]

if (verbose) messagef("Updating existing %s translation", languages)
run_msgmerge(po_path(dir, languages), pot_path(dir, package), previous = TRUE)
}

po_path <- function(dir, lang) {
file.path(dir, "po", paste0("R-", lang, ".po"))
hadley marked this conversation as resolved.
Show resolved Hide resolved
}
pot_path <- function(dir, package) {
file.path(dir, "po", paste0("R-", package, ".pot"))
}

# https://www.gnu.org/software/gettext/manual/html_node/msginit-Invocation.html
run_msginit <- function(pot_path, po_path, locale, width = 80) {
cmd <- paste("msginit",
"-i", shQuote(path.expand(pot_path)),
"-o", shQuote(path.expand(po_path)),
"-l", shQuote(locale),
"-w", width,
"--no-translator" # don't consult user-email etc
)
if (system(cmd) != 0L) {
stopf("running msginit on '%s' failed", pot_path)
}
return(invisible())
}

31 changes: 31 additions & 0 deletions man/po_create.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
hadley marked this conversation as resolved.
Show resolved Hide resolved