-
Notifications
You must be signed in to change notification settings - Fork 242
Support additional languages for random phrases #538
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Updating the language in the app sets `tutorial.language` in the request that can be read via `read_request()` - That language option is added to the `exercise` object in Shiny - evaluate_exercise() sets the knitr option `tutorial.language` to match that language via `i18n_set_language_option()` - Exercise code can read the current language via `i18n_get_language_option()`
Avoids having to make sure that all of the chunk options fit on one line
schloerke
reviewed
Jun 14, 2021
schloerke
reviewed
Jun 14, 2021
Co-authored-by: Barret Schloerke <barret@rstudio.com>
schloerke
reviewed
Jun 23, 2021
schloerke
reviewed
Jun 23, 2021
schloerke
reviewed
Jun 23, 2021
schloerke
reviewed
Jun 23, 2021
schloerke
approved these changes
Jun 23, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #534
Random phrases are now stored in
data-raw/i18n_random-phrases.yml.New phrases in other languages (not direct translations of the English phrases) can be added to that YAML file. Maintainers will need to run
data-raw/i18n_translations.Rto update the internal data.I added a
languageargument torandom_praise()andrandom_encouragement()that can be used to set the language of the phrase. I don't recommend that argument be used other than for testing. Instead, authors can rely on these functions automatically determining which language is currently in use in the tutorial using the existinglanguageoption in the tutorial YAML header. (So language keys ini18n_random-phrases.ymlneed to match those ini18n_translations.yml.)The primary challenge with this PR is that the language of the tutorial needs to be communicated from the app to the exercise evaluation, since technically the language can change even during runtime. To address this:
sessionobject (viawrite_request())exercise$tutorial$language(viaread_request())evaluate_exercise()finds this language or the default language value and sets a knitr option for the exercise evaluationrandom_phrases()(the helper behind praise/encouragement) will look for the knitr option when picking a language, falling back to English if not set or if there aren't random phrases in the current language.I added a helper
random_phrases_add()that lets users declare random phrases for use in the tutorial outside of contributing them directly to learnr. The process is additive, so phrases are added rather than overwriting existing phrases for a given language.The mechanism of communication here is to use shiny prerendered chunks if
random_phrases_add()is called in a standard R chunk that calls an internal function to set theexercise.random_phraseschunk option. If called in the global setup chunk, then the internal function is called directly (without the prerendered chunk). The end effect is that all of the exercises will pick up the random phrases chunk option setting, which ensures that these random phrases are available when evaluating user code.Getting the above to work required a small change to the exercise rendering. Rather than writing the exercise chunk options into the user code chunk, we now rely on chunk options being set globally in when
rmarkdown::render()is called. This skips writing R objects into character strings, which was breaking when trying to embed a large list into these options since chunk headers can't be multiline. We already have the options as an R object so we just use it to directly set the chunk options viaknitr_optionsinrmarkdown::output_format().Finally, I bumped the current exercise version to
3, since we've now added the expectation thattutorial$languageis present. (I'm not totally certain how necessary the version bump is and could be convinced to roll that back;random_phrases()will fall back to"en"if that field isn't present.)