Skip to content

Commit

Permalink
Refactor i18n and add additional translation keys (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie authored Feb 2, 2021
1 parent 6694842 commit 7ab246f
Show file tree
Hide file tree
Showing 18 changed files with 517 additions and 186 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ rsconnect/
^doc$
^Meta$
^\.github
^data-raw$
109 changes: 70 additions & 39 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,57 +1,88 @@
Package: learnr
Type: Package
Package: learnr
Title: Interactive Tutorials for R
Version: 0.10.1.9007
Authors@R: c(
person("Barret", "Schloerke", role = c("aut", "cre"), email = "barret@rstudio.com",
comment = c(ORCID = "0000-0001-9986-114X")),
person("JJ", "Allaire", role = "aut", email = "jj@rstudio.com"),
person("Barbara", "Borges", role = "aut", email = "barbara@rstudio.com"),
person("Angela", "Li", role = "ctb", email = "angelali921@gmail.com", comment = "vignette"),
person("RStudio", role = c("cph", "fnd")),
person(family = "Ajax.org B.V.", role=c("ctb", "cph"), comment= "Ace library"),
person("Zeno", "Rocha", role = c("ctb", "cph"), comment = "clipboard.js library"),
person("Nick", "Payne", role = c("ctb", "cph"), comment = "Bootbox library"),
person("Jake", "Archibald", role = c("ctb", "cph"), comment = "idb-keyval library")
)
Description: Create interactive tutorials using R Markdown. Use a combination
of narrative, figures, videos, exercises, and quizzes to create self-paced
tutorials for learning about R and R packages.
Authors@R:
c(person(given = "Barret",
family = "Schloerke",
role = c("aut", "cre"),
email = "barret@rstudio.com",
comment = c(ORCID = "0000-0001-9986-114X")),
person(given = "JJ",
family = "Allaire",
role = "aut",
email = "jj@rstudio.com"),
person(given = "Barbara",
family = "Borges",
role = "aut",
email = "barbara@rstudio.com"),
person(given = "Garrick",
family = "Aden-Buie",
email = "garrick@rstudio.com",
role = "aut",
comment = c(ORCID = "0000-0002-7111-0077")),
person(given = "Angela",
family = "Li",
role = "ctb",
email = "angelali921@gmail.com",
comment = "vignette"),
person(given = "RStudio",
role = c("cph", "fnd")),
person(family = "Ajax.org B.V.",
role = c("ctb", "cph"),
comment = "Ace library"),
person(given = "Zeno",
family = "Rocha",
role = c("ctb", "cph"),
comment = "clipboard.js library"),
person(given = "Nick",
family = "Payne",
role = c("ctb", "cph"),
comment = "Bootbox library"),
person(given = "Jake",
family = "Archibald",
role = c("ctb", "cph"),
comment = "idb-keyval library"))
Description: Create interactive tutorials using R Markdown. Use
a combination of narrative, figures, videos, exercises, and quizzes to
create self-paced tutorials for learning about R and R packages.
License: Apache License (>= 2.0)
URL: https://rstudio.github.io/learnr/, https://github.com/rstudio/learnr
URL: https://rstudio.github.io/learnr/,
https://github.com/rstudio/learnr
BugReports: https://github.com/rstudio/learnr/issues
Imports:
utils,
parallel,
withr,
rappdirs,
rprojroot,
jsonlite,
checkmate,
curl,
digest,
ellipsis (>= 0.2.0.1),
evaluate,
htmltools (>= 0.3.5),
htmlwidgets,
evaluate,
jsonlite,
knitr (>= 1.14),
markdown,
shiny (>= 1.0),
rmarkdown (>= 1.12.0),
ellipsis (>= 0.2.0.1),
checkmate,
renv (>= 0.8.0),
curl,
parallel,
promises,
digest
rappdirs,
renv (>= 0.8.0),
rlang,
rmarkdown (>= 1.12.0),
rprojroot,
shiny (>= 1.0),
utils,
withr
Suggests:
callr,
httpuv,
later,
shinytest (>= 1.4.0.9002),
testthat (>= 2.1.0)
VignetteBuilder:
knitr
Remotes:
rstudio/htmltools,
rstudio/shinytest
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
Suggests:
testthat (>= 2.1.0),
shinytest (>= 1.4.0.9002),
callr,
rlang,
httpuv,
later
VignetteBuilder: knitr
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ learnr (development version)
* Added an event handler system, with the functions `event_register_handler()` and `one_time()`. There is also a new event `"section_viewed"`, which is triggered when a new section becomes visible. ([#398](https://github.com/rstudio/learnr/pull/398))
* Previously, when a question submission was reset, it would be recorded as a `"question_submission"` event with the value `reset=TRUE`. Now it a separate event, `"reset_question_submission"`. ([#398](https://github.com/rstudio/learnr/pull/398))
* Added a new `polyglot` tutorial to learnr. This tutorial displays mixing R, python, and sql exercises. See [`run_tutorial("polyglot", "learnr")`](https://learnr-examples.shinyapps.io/polyglot) for a an example. ([#397](https://github.com/rstudio/learnr/pull/397))
* Text throughout the learnr interface can be customized or localized using the new `language` argument of `tutorial()`. Translations for English and French are provided and contributes will be welcomed. Read more about these features in `vignette("multilang", package = "learnr")`. ([#456](https://github.com/rstudio/learnr/pull/456))
* Text throughout the learnr interface can be customized or localized using the new `language` argument of `tutorial()`. Translations for English and French are provided and contributes will be welcomed. Read more about these features in `vignette("multilang", package = "learnr")`. ([#456](https://github.com/rstudio/learnr/pull/456), [#479](https://github.com/rstudio/learnr/pull/479))

## Minor new features and improvements

Expand Down
2 changes: 1 addition & 1 deletion R/html-dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ tutorial_i18n_html_dependency <- function(language = NULL) {
name = "i18n",
version = "1.2.0",
src = system.file("lib/i18n", package = "learnr"),
script = c("i18next.min.js", "jquery-i18next.min.js", "tutorial-i18n-init.js"),
script = c("i18next.min.js", "tutorial-i18n-init.js"),
head = format(htmltools::tags$script(
id = "i18n-cstm-trns",
type = "application/json",
Expand Down
81 changes: 10 additions & 71 deletions R/i18n.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,76 +119,15 @@ i18n_validate_customization <- function(lng) {
lng
}

i18n_span <- function(key, ..., opts = NULL) {
if (!is.null(opts)) {
opts <- jsonlite::toJSON(opts, auto_unbox = TRUE, pretty = FALSE)
}
x <- htmltools::span(..., `data-i18n` = key, `data-i18n-opts` = opts)
# return an html character object instead of a shiny.tag
htmltools::HTML(format(x))
}

i18n_translations <- function() {
list(
en = list(
translation = list(
button = list(
runcode = "Run Code",
hints = "Hints",
startover = "Start Over",
continue = "Continue",
submitanswer = "Submit Answer",
previoustopic = "Previous Topic",
nexttopic = "Next Topic",
questionsubmit = "Submit Answer",
questiontryagain = "Try Again"
),
text = list(
startover = "Start Over",
areyousure = "Are you sure you want to start over? (all exercise progress will be reset)",
youmustcomplete = "You must complete the",
exercise = "exercise",
exercise_plural = "exercises",
inthissection = "in this section before continuing."
)
)
),
fr = list(
translation = list(
button = list(
runcode = "Lancer le Code",
hints = "Indice",
startover = "Recommencer",
continue = "Continuer",
submitanswer = "Soumettre",
previoustopic = "Chapitre Pr\u00e9c\u00e9dent",
nexttopic = "Chapitre Suivant",
questionsubmit = "Soumettre",
questiontryagain = "R\u00e9essayer"
),
text = list(
startover = "Recommencer",
areyousure = "\u00cates-vous certains de vouloir recommencer? (La progression sera remise \u00e0 z\u00e9ro)",
youmustcomplete = "Vous devez d'abord compl\u00e9ter",
inthissection = "de cette section avec de continuer.",
exercise = "l'exercice",
exercise_plural = "des exercices"
)
)
),
emo = list(
translation = list(
button = list(
runcode = "\U0001f3c3",
hints = "\U0001f50e",
startover = "\u23ee",
continue = "\u2705",
submitanswer = "\U0001f197",
previoustopic = "\u2b05",
nexttopic = "\u27a1",
questionsubmit = "\U0001f197",
questiontryagain = "\U0001f501"
),
text = list(
startover = "\u23ee",
areyousure = "\U0001f914",
youmustcomplete = "\u26a0 \U0001f449",
exercise = "",
exercise_plural = "",
inthissection = "."
)
)
)
)
readRDS(system.file("i18n_translations", package = "learnr"))
}
8 changes: 6 additions & 2 deletions R/knitr-hooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -398,13 +398,17 @@ install_knitr_hooks <- function() {
# else, return as the user provided
options$engine
)
paste0(cap_engine_val, " code")
i18n_span(
"text.enginecap",
paste(cap_engine_val, "Code"),
opts = list(engine = cap_engine_val)
)
}
}
ui_options <- list(
engine = options$engine,
has_checker = (!is.null(check_chunk) || !is.null(code_check_chunk)),
caption = caption
caption = as.character(caption)
)
extra_html <- c('<script type="application/json" data-ui-opts="1">',
jsonlite::toJSON(ui_options, auto_unbox = TRUE),
Expand Down
50 changes: 30 additions & 20 deletions R/quiz.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
#' @seealso \code{\link{random_praise}}, \code{\link{random_encouragement}}
#' @export
#' @rdname quiz
quiz <- function(..., caption = "Quiz") {
quiz <- function(..., caption = rlang::missing_arg()) {

# create table rows from questions
index <- 1
Expand All @@ -90,10 +90,14 @@ quiz <- function(..., caption = "Quiz") {
question
})

ret <- list(
caption = if(!is.null(caption)) quiz_text(caption),
questions = questions
)
caption <-
if (rlang::is_missing(caption)) {
i18n_span("text.quiz", "Quiz")
} else if (!is.null(caption)) {
quiz_text(caption)
}

ret <- list(caption = caption, questions = questions)
class(ret) <- "tutorial_quiz"
ret
}
Expand All @@ -111,8 +115,8 @@ question <- function(text,
message = NULL,
post_message = NULL,
loading = c("**Loading:** ", format(text), "<br/><br/><br/>"),
submit_button = "Submit Answer",
try_again_button = "Try Again",
submit_button = rlang::missing_arg(),
try_again_button = rlang::missing_arg(),
allow_retry = FALSE,
random_answer_order = FALSE,
options = list()
Expand Down Expand Up @@ -163,14 +167,29 @@ question <- function(text,
label <- knitr::opts_current$get('label')
q_id <- label %||% random_question_id()

# i18nize button labels if default values are used
submit_button <-
if (rlang::is_missing(submit_button)) {
i18n_span("button.questionsubmit", "Submit Answer")
} else {
quiz_text(submit_button)
}

try_again_button <-
if (rlang::is_missing(try_again_button)) {
i18n_span("button.questiontryagain", "Try Again")
} else {
quiz_text(try_again_button)
}

ret <- list(
type = type,
label = label,
question = quiz_text(text),
answers = answers,
button_labels = list(
submit = quiz_text(submit_button),
try_again = quiz_text(try_again_button)
submit = submit_button,
try_again = try_again_button
),
messages = list(
correct = quiz_text(correct),
Expand Down Expand Up @@ -569,13 +588,6 @@ question_button_label <- function(question, label_type = "submit", is_valid = TR
button_label <- question$button_labels[[label_type]]
is_valid <- isTRUE(is_valid)

# We don't want to localize button labels that were customized by the user
# If they're default labels, we'll add the `data-i18n` attribute for localization
default_label <- eval(formals("question")[[paste0(label_type, "_button")]])
# At this point, `button_label` has been upgraded to HTML.
# Need to format() for a fair comparison
is_default_label <- identical(format(button_label), default_label)

default_class <- "btn-primary"
warning_class <- "btn-warning"

Expand All @@ -584,8 +596,7 @@ question_button_label <- function(question, label_type = "submit", is_valid = TR
if (label_type == "submit") {
button <- actionButton(
action_button_id, button_label,
class = default_class,
`data-i18n` = if (is_default_label) "button.questionsubmit"
class = default_class
)
if (!is_valid) {
button <- disable_all_tags(button)
Expand All @@ -595,8 +606,7 @@ question_button_label <- function(question, label_type = "submit", is_valid = TR
mutate_tags(
actionButton(
action_button_id, button_label,
class = warning_class,
`data-i18n` = if (is_default_label) "button.questiontryagain"
class = warning_class
),
paste0("#", action_button_id),
function(ele) {
Expand Down
Loading

0 comments on commit 7ab246f

Please sign in to comment.