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

More internationalization #456

Merged
merged 35 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
6892a6c
PoC internationalization
ColinFay Sep 30, 2020
cc14a86
Added html deps
ColinFay Oct 1, 2020
c3e0bda
libs and example
ColinFay Oct 1, 2020
70852d9
This implement the internationalization of learnr UI
ColinFay Oct 7, 2020
1888d7e
documentation of the new param
ColinFay Oct 7, 2020
f87c89e
more info in the vignette
ColinFay Oct 7, 2020
83d0854
rm language from sandbox.Rmd
ColinFay Oct 7, 2020
10464cd
html dep is written in a tempfile
ColinFay Oct 13, 2020
6463547
bootbox is now internationalized
ColinFay Oct 13, 2020
24af04b
Added a note about bootbox
ColinFay Oct 13, 2020
e4b0a99
Update R/html-dependencies.R
ColinFay Oct 19, 2020
621c9b4
Extend internationalization to expose customization to authors
gadenbuie Dec 23, 2020
8c38979
Rewrite multilang vignette
gadenbuie Dec 23, 2020
4e74ca6
Fix namespacing learnr functions in vignette/multilang
gadenbuie Dec 23, 2020
2a30ba3
Escape all unicode characters
gadenbuie Dec 23, 2020
7af49e9
Backport dQuote() with q argument
gadenbuie Dec 23, 2020
2159e63
Check submit button parent element for data-check attribute
gadenbuie Dec 28, 2020
878f5ae
Fix use of dQuote in multilang vignette
gadenbuie Dec 28, 2020
fc8dc5d
Refactor internationalization
gadenbuie Jan 12, 2021
359fa1f
Add note that only first language is used
gadenbuie Jan 12, 2021
17a2430
Add localize method to window.tutorial
gadenbuie Jan 12, 2021
5d53ccd
Rename and update sandbox demo
gadenbuie Jan 12, 2021
7db49a9
Remove unused backport of dQuote()
gadenbuie Jan 12, 2021
7ef80fc
More cleaning up of sandbox
gadenbuie Jan 12, 2021
be28f46
Add "localize" shiny message handler
gadenbuie Jan 12, 2021
b1a6044
Add translation capability to question buttons
gadenbuie Jan 12, 2021
5167298
Only localize default question button labels
gadenbuie Jan 12, 2021
4571848
Fix mime type of script tag containing json
gadenbuie Jan 13, 2021
622f9ea
Add notes about motivation in comments
gadenbuie Jan 13, 2021
a546490
Explicitly point to vignette() in language param docs
gadenbuie Jan 13, 2021
a46069f
Use arrays in i18next.t() to provide fallback and add translation for…
gadenbuie Jan 13, 2021
649d04a
Add news for #456
gadenbuie Jan 13, 2021
95748fa
Don't use "custom" namespace, instead customizations overwrite defaul…
gadenbuie Jan 15, 2021
d885520
learnr-i18n-init.js -> tutorial-i18n-init.js
gadenbuie Jan 15, 2021
2ac6219
Use ES5 compatible syntax
gadenbuie Jan 15, 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 NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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))

## Minor new features and improvements

Expand Down
12 changes: 9 additions & 3 deletions R/i18n.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ i18n_translations <- function() {
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."
)
)
Expand All @@ -157,9 +159,11 @@ i18n_translations <- function() {
),
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 l'exercice",
inthissection = "de cette section avec de continuer."
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"
)
)
),
Expand All @@ -180,6 +184,8 @@ i18n_translations <- function() {
startover = "\u23ee",
areyousure = "\U0001f914",
youmustcomplete = "\u26a0 \U0001f449",
exercise = "",
exercise_plural = "",
inthissection = "."
)
)
Expand Down
2 changes: 2 additions & 0 deletions R/quiz.R
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,8 @@ question_button_label <- function(question, label_type = "submit", is_valid = TR
# 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)
gadenbuie marked this conversation as resolved.
Show resolved Hide resolved

default_class <- "btn-primary"
Expand Down
6 changes: 3 additions & 3 deletions R/tutorial-format.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
#' \code{---} to em-dashes, \code{--} to en-dashes, and \code{...} to ellipses.
#' Deprecated in \pkg{rmarkdown} v2.2.0.
#' @param ... Forward parameters to html_document
#' @param language Language or custom text of the UI elements. See the
#' `multilang` vignette for more information about available options and
#' formatting.
#' @param language Language or custom text of the UI elements. See
#' `vignette("multilang", package = "learnr")` for more information
#' about available options and formatting
#'
#' @export
#' @importFrom utils getFromNamespace
Expand Down
45 changes: 18 additions & 27 deletions inst/rmarkdown/templates/tutorial/resources/tutorial-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ $(document).ready(function() {

}

function i18nextLang(fallbackLng = 'en') {
gadenbuie marked this conversation as resolved.
Show resolved Hide resolved
return i18next.language || window.localStorage.i18nextLng || fallbackLng || 'en';
}
schloerke marked this conversation as resolved.
Show resolved Hide resolved

function handleSkipClick(event) {
$(this).data('n_clicks', $(this).data('n_clicks') + 1)

Expand All @@ -176,21 +180,15 @@ $(document).ready(function() {
})
// if the section has exercises and is not complete, don't skip - put up message
if (section.exercises.length && !section.completed && !section.allowSkip) {
var exs = section.exercises.length == 1 ? 'exercise' : 'exercises';

var youmustcomplete = i18next.t("text.youmustcomplete")
// Checking that the translation exists, if not, default to english
if (youmustcomplete == "text.youmustcomplete") {
youmustcomplete = "You must complete the"
}
var inthissection = i18next.t("text.inthissection")
// Checking that the translation exists, if not, default to english
if (inthissection == "text.inthissection") {
inthissection = "in this section before continuing."
}
// defaulting to english if nothing works
bootbox.setLocale(i18next.language || window.localStorage.i18nextLng || "en")
bootbox.alert(inthissection + " " + exs + " " + inthissection);
var exs = i18next.t(
['text.exercise', 'exercise'],
{count: section.exercises.length, lngs: [i18nextLang(), 'en']}
schloerke marked this conversation as resolved.
Show resolved Hide resolved
);
var youmustcomplete = i18next.t(['text.youmustcomplete', 'You must complete the']);
var inthissection = i18next.t(['text.inthissection', 'in this section before continuing.']);

bootbox.setLocale(i18nextLang());
bootbox.alert(youmustcomplete + " " + exs + " " + inthissection);
}
else {
if (sectionIndex == topic.sections.length - 1) {
Expand Down Expand Up @@ -356,18 +354,11 @@ $(document).ready(function() {

var resetButton = $('<span class="resetButton" data-i18n="text.startover">Start Over</span>');
resetButton.on('click', function() {
var areyousure = i18next.t("text.areyousure")
// Default to english if no translation is found
if (areyousure == "text.areyousure") {
areyousure = "Are you sure you want to start over? (all exercise progress will be reset)"
}
// defaulting to english if nothing works
bootbox.setLocale(i18next.language || window.localStorage.i18nextLng || "en")
bootbox.confirm(areyousure,
function(result) {
if (result)
tutorial.startOver();
});

var areyousure = i18next.t(['text.areyousure', 'Are you sure you want to start over? (all exercise progress will be reset)']);

bootbox.setLocale(i18nextLang());
bootbox.confirm(areyousure, function(result) { result && tutorial.startOver(); });
});
topicsFooter.append(resetButton);
topicsList.append(topicsFooter);
Expand Down
10 changes: 6 additions & 4 deletions man/tutorial.Rd

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