Skip to content

Commit

Permalink
Merge pull request #17 from reside-ic/reside-225
Browse files Browse the repository at this point in the history
Add resources as explicit arg of translator_register and add additional test
  • Loading branch information
richfitz authored Mar 19, 2021
2 parents bc67d1b + d0832ce commit cdfd90e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: traduire
Title: Example Translation in a Package
Version: 0.0.5
Version: 0.0.6
Authors@R:
c(person(given = "Rich",
family = "FitzJohn",
Expand Down
6 changes: 4 additions & 2 deletions R/translator.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
##'
##' @title Register a translator
##'
##' @inheritParams i18n
##'
##' @param ... For \code{translator_register}, arguments passed to
##' \code{\link{traduire_options}} to build the translator object. All
##' arguments are accepted. For \code{translator_translate} and
Expand Down Expand Up @@ -85,9 +87,9 @@
##' traduire::t_("hello", language = "fr", name = "myexample")
##' "myexample" %in% traduire::translator_list()
##' traduire::translator_unregister("myexample")
translator_register <- function(..., name = NULL) {
translator_register <- function(resources, ..., name = NULL) {
name <- name_from_context(name, NULL, strict = TRUE)
translators[[name]] <- i18n(...)
translators[[name]] <- i18n(resources = resources, ...)
}


Expand Down
7 changes: 6 additions & 1 deletion man/translator.Rd

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

26 changes: 26 additions & 0 deletions tests/testthat/test-translator.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,29 @@ test_that("translate from another package", {
})
mockery::expect_args(mock_translator, 1, "name", "package")
})


test_that("translator registered with resource_pattern", {
root <- traduire_file("examples/structured")
name <- rand_str()
res <- translator_register(
NULL,
name = name,
resource_pattern = sprintf("%s/{language}-{namespace}.json", root),
default_namespace = "common",
namespaces = c("common", "login"),
language = "en",
languages = c("en", "fr"))
expect_is(res, "i18n")
expect_true(name %in% translator_list())

expect_identical(translator(name), res)
expect_equal(translator_translate("hello", language = "fr", name = name),
"salut le monde")
expect_equal(t_("hello", language = "fr", name = name),
"salut le monde")
expect_true(exists(name, translators))
translator_unregister(name)
expect_false(exists(name, translators))
expect_false(name %in% translator_list())
})

0 comments on commit cdfd90e

Please sign in to comment.