MichaelChirico / potools

Tools for working with translations in R
https://michaelchirico.github.io/potools/
58 stars 2 forks source link

translate_package(): How to detect own utility functions, e.g. stopf(...) short for stop(gettextf(...))? #223

Closed HenrikBengtsson closed 3 years ago

HenrikBengtsson commented 3 years ago

Hi, I use internal utility functions like:

stopf <- function(fmt, ..., call. = TRUE, domain = NA) {
  stop(gettextf(fmt, ...), call. = call., domain = domain)
}

warnf <- function(fmt, ..., call. = TRUE, immediate. = FALSE, domain = NA) {
  warning(gettextf(fmt, ...), call. = call., immediate. = immediate., domain = domain)
}

so that I can write:

if (x < 0) stopf("Argument 'x' must not be negative: %d", x)
if (length(y) != 1) warnf("Argument 'y' must be of length one: %d", length(y))

Is it possible to have potools::translate_package() detect these calls and produce pot entries for them?

hadley commented 3 years ago

You can use the custom_translation_functions argument

MichaelChirico commented 3 years ago

Yep! Let me know if the write-up here needs improvement:

https://github.com/MichaelChirico/potools#customized-translators

HenrikBengtsson commented 3 years ago

Thank you both. Using:

res <- potools::translate_package(custom_translation_functions = list(R = "stopf:fmt|1"))

works great.

... Let me know if the write-up here needs improvement: ...

The ?potools::translate_package page LGTM. But, assuming most people with use translate_package() and not get_message_data() [correct?], I think it would help if the README.md would use translate_package() instead of get_message_data() for the example.

hadley commented 3 years ago

stopf() and warningf() are now handled automatically by po_extract(), so I think this can be closed.