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

[pull] l2d from carpentries:main #52

Merged
43 commits merged into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
f8282c4
add more detail and links to translations vignette
zkamvar Dec 8, 2023
18380bb
update data flow vignette to include translations
zkamvar Dec 12, 2023
86b01af
Merge branch 'main' into update-vignettes
zkamvar Dec 12, 2023
962842b
update NEWS
zkamvar Dec 12, 2023
73c0839
Merge pull request #553 from carpentries/update-vignettes
zkamvar Dec 12, 2023
d22ce96
add tests for translations
zkamvar Dec 12, 2023
fe6dbff
add country-specific context for translators
zkamvar Dec 12, 2023
574d24a
allow for country codes
zkamvar Dec 12, 2023
3dac60d
add tests for generated content
zkamvar Dec 12, 2023
83b0907
first attempt to centralise translations
zkamvar Dec 12, 2023
7d9cc2c
do not use global cache in build_instructor_notes
zkamvar Dec 12, 2023
b7d380e
remove global var translations for aggregators
zkamvar Dec 13, 2023
b11bf87
update documentation
zkamvar Dec 13, 2023
9a48be5
Merge pull request #554 from carpentries/ensure-country-codes
zkamvar Dec 13, 2023
19e52c9
Release sandpaper 0.16.0
froggleston Dec 13, 2023
fb6b5e4
Remove dev tinkr in DESCRIPTION to avoid test failures
froggleston Dec 13, 2023
50bfd5a
Merge pull request #555 from carpentries/release-0.16.0
zkamvar Dec 13, 2023
9733f3a
add failing test for callout headings
zkamvar Dec 14, 2023
bd0de30
use /text() XPath to extract heading text for mod
zkamvar Dec 14, 2023
83e289b
bump version; add news
zkamvar Dec 14, 2023
9ef1e34
Merge pull request #557 from carpentries/fix-556
zkamvar Dec 14, 2023
2f68768
refactor test-translate
zkamvar Dec 15, 2023
2e0fcd2
rearrange tests in page types
zkamvar Dec 15, 2023
dc0d211
add helpers for title elements; test 404 page
zkamvar Dec 15, 2023
0349a41
add tests for navigation
zkamvar Dec 15, 2023
8e11604
Merge pull request #558 from carpentries/refactor-translation-tests
zkamvar Dec 15, 2023
7f3ac97
first working refactor of centralising translation
zkamvar Dec 16, 2023
9be8430
use cached translations inside of utils-translate
zkamvar Dec 16, 2023
14ee25a
apply translations during setup
zkamvar Dec 18, 2023
bf4373a
simplify translation pre-compute; re-add tests
zkamvar Dec 18, 2023
465b6e0
centralise translate list; add documentation
zkamvar Dec 18, 2023
dfcf7ab
fix manual page for translations
zkamvar Dec 18, 2023
fa072c1
Merge pull request #559 from carpentries/refactor-translations
zkamvar Dec 18, 2023
6aac82d
add tr_ helpers; update tests
zkamvar Dec 19, 2023
1fbf3e0
update line numbers in translations
zkamvar Dec 19, 2023
7ae9c18
test translations using keys not values
zkamvar Dec 19, 2023
8406e7a
replace raw trans env calls with helper functions
zkamvar Dec 19, 2023
f992d4a
update documentation
zkamvar Dec 19, 2023
a33a128
clean up and document tests a bit
zkamvar Dec 19, 2023
1529dfd
Merge pull request #560 from carpentries/finalise-translate-tests
zkamvar Dec 19, 2023
936cf08
update metadata to include language
zkamvar Dec 19, 2023
155efd6
bump version to 0.16.2
zkamvar Dec 19, 2023
ed0bd3f
Merge pull request #561 from carpentries/release-0.16.2
zkamvar Dec 19, 2023
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
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: sandpaper
Title: Create and Curate Carpentries Lessons
Version: 0.15.0.9000
Version: 0.16.0
Authors@R: c(
person(given = "Zhian N.",
family = "Kamvar",
Expand Down Expand Up @@ -97,7 +97,6 @@ Suggests:
varnish (>= 0.3.0)
Additional_repositories: https://carpentries.r-universe.dev/
Remotes:
ropensci/tinkr,
carpentries/pegboard,
carpentries/varnish
SystemRequirements: pandoc (>= 2.11.4) - https://pandoc.org
Expand Down
7 changes: 5 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# sandpaper 0.15.0.9000 (unreleased)
# sandpaper 0.16.0 (2023-12-13)

## NEW FEATURES

Expand All @@ -12,7 +12,10 @@

* A new vignette `vignette("translation", package = "sandpaper")` describes how
translation of template components works and how to submit new/update
translations.
translations (added: @zkamvar, #546).
- A new vignette about data flow `vignette("data-flow", package = "sandpaper")`
describes how templating, translations, and lesson metadata flows from
{sandpaper} to {varnish} (added: @zkamvar, #553)

## BUG FIX

Expand Down
2 changes: 1 addition & 1 deletion R/utils-translate.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ known_languages <- function() {

is_known_language <- function(lang = NULL, warn = FALSE) {
lang <- lang %||% "en"
not_known <- lang %nin% known_languages()
not_known <- strsplit(lang, "_")[[1]][1] %nin% known_languages()
if (not_known && warn) {
warn_no_language(lang)
}
Expand Down
8 changes: 4 additions & 4 deletions man/fill_translation_vars.Rd

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

1 change: 1 addition & 0 deletions man/known_languages.Rd

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

1 change: 1 addition & 0 deletions man/sandpaper-package.Rd

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

80 changes: 79 additions & 1 deletion tests/testthat/test-translate.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ test_that("set_language() uses english by default", {
})


test_that("set_language() can use country codes", {

os <- tolower(Sys.info()[["sysname"]])
ver <- getRversion()
skip_if(os == "windows" && ver < "4.2")

expect_silent(set_language("es_AR"))
OUTAR <- tr_("OUTPUT")
expect_false(identical(OUTAR, "OUTPUT"))

# the country codes will fall back to language code if they don't exist
expect_silent(set_language("es"))
expect_equal(tr_("OUTPUT"), OUTAR)

})


test_that("is_known_language returns a warning for an unknown language", {

os <- tolower(Sys.info()[["sysname"]])
Expand Down Expand Up @@ -61,10 +78,39 @@ test_that("Lessons can be translated with lang setting", {

# Build lesson
suppressMessages(build_lesson(tmp, preview = FALSE, quiet = TRUE))

# GENERATED PAGES ------------------------------------------------
# Check generated page headers
inst_note <- xml2::read_html(fs::path(sitepath, "instructor/instructor-notes.html"))
h1_inst <- xml2::xml_find_first(inst_note, "//main/div/h1")
expect_equal(
xml2::xml_text(h1_inst),
withr::with_language("ja", tr_("Instructor Notes"))
)
expect_false(
identical(
xml2::xml_text(h1_inst),
withr::with_language("en", tr_("Instructor Notes"))
)
)
profiles <- xml2::read_html(fs::path(sitepath, "profiles.html"))
h1_profiles <- xml2::xml_find_first(profiles, "//main/div/h1")
expect_equal(
xml2::xml_text(h1_profiles),
withr::with_language("ja", tr_("Learner Profiles"))
)
expect_false(
identical(
xml2::xml_text(h1_profiles),
withr::with_language("en", tr_("Learner Profiles"))
)
)


# Extract first header (Summary and Setup) from index
xml <- xml2::read_html(fs::path(sitepath, "index.html"))
h1_header <- xml2::xml_find_all(xml, "//h1[@class='schedule-heading']")
nav_links <- xml2::xml_find_all(xml, "//a[starts-with(@class,'nav-link')]")

# language should be set to japanese
expect_equal(xml2::xml_attr(xml, "lang"), "ja")
Expand All @@ -76,6 +122,21 @@ test_that("Lessons can be translated with lang setting", {
withr::with_language("ja", tr_("Summary and Setup"))
)
)
# Navbar has expected text
expect_equal(
xml2::xml_text(nav_links),
withr::with_language("ja",
c(tr_("Key Points"), tr_("Glossary"), tr_("Learner Profiles"))
)
)
expect_false(
identical(
xml2::xml_text(nav_links),
withr::with_language("en",
c(tr_("Key Points"), tr_("Glossary"), tr_("Learner Profiles"))
)
)
)

# Header should no longer be in English
expect_false(
Expand All @@ -84,7 +145,7 @@ test_that("Lessons can be translated with lang setting", {
withr::with_language("en", tr_("Summary and Setup"))
)
)

# aria labels should be translated
arias <- c("Main Navigation", "Toggle Navigation", "Search", "search button",
"Lesson Progress", "close menu", "Next Chapter", "anchor", "Back To Top")
Expand All @@ -100,6 +161,23 @@ test_that("Lessons can be translated with lang setting", {
# Episode elements -------------------------------------------------
# We use here the Instructor view because it is more fully featured
xml <- xml2::read_html(fs::path(sitepath, "instructor", "introduction.html"))
nav_links <- xml2::xml_find_all(xml, "//a[starts-with(@class,'nav-link')]")

# navbar has expected text
expect_equal(
xml2::xml_text(nav_links),
withr::with_language("ja",
c(tr_("Key Points"), tr_("Instructor Notes"), tr_("Extract All Images"))
)
)
expect_false(
identical(
xml2::xml_text(nav_links),
withr::with_language("en",
c(tr_("Key Points"), tr_("Instructor Notes"), tr_("Extract All Images"))
)
)
)

# overview, objectives, and questions
overview_card <- xml2::xml_find_first(xml, ".//div[@class='overview card']")
Expand Down
88 changes: 86 additions & 2 deletions vignettes/data-flow.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,83 @@ updated with local data (e.g. the sidebar needs to include headings for the
current page).

```{r globals}
snd <- asNamespace("sandpaper")
snd$instructor_globals$get()
snd$learner_globals$get()
```

### Translations

In the majority of the {varnish} templates are keys that need translations that
are in the format of `{{ translate.ThingInPascalCase }}`:

```{r overview, results="asis", echo = FALSE, comment = ""}
writeLines("```html")
writeLines(readLines(system.file("pkgdown/templates/content-overview.html",
package = "varnish")))
writeLines("```")
```

These variables are in {sandpaper} and are _expected to exist_. They get
generated by the `set_language()` function, triggered when `build_site()` is
run. When this gets triggered a new list item called "translate" is added to the
instructor and learner globals.

```{r set-language}
snd <- asNamespace("sandpaper")
length(snd$instructor_globals$get()[["translate"]])
snd$set_language("en")
Sys.getenv("LANGUAGE")
length(snd$instructor_globals$get()[["translate"]])
```

These variables are passed directly to {varnish} templates, which eventually get
processed by {whisker}:

```{r whisker}
whisker::whisker.render("Edit this page: {{ translate.EditThisPage }}",
data = snd$instructor_globals$get()
)
```

This is key to building lessons in other languages, regardless of your default
language. The lesson author sets the `lang:` config key to the two-letter
language code that the lesson is written in. This gets passed to the
`set_language()` function, which modifies the translations inside the global
data. Importantly, it is bound to the calling environment, so when the original
calling environment exits, the language switches back to your default language.

```{r set-language-es}
local({
snd <- asNamespace("sandpaper")
snd$set_language("es")
print(Sys.getenv("LANGUAGE"))
whisker::whisker.render("Edit this page: {{ translate.EditThisPage }}",
data = snd$instructor_globals$get()
)
})
Sys.getenv("LANGUAGE")
```

### Translation Variables

There are `r length(snd$instructor_globals$get()[["translate"]])` translations
generated by `set_language()` that correspond to the following variables in
{varnish}:

```{r translation-list, results = "asis", echo = FALSE}
withr::with_language("en", snd$add_varnish_translations())
res <- glue::glue("| `translate.{names(tr)}` | `{tr}` |",
tr = snd$instructor_globals$get()[["translate"]])
writeLines(c("| variable | string |\n| --- | --- |", res))
```

In addition, there are other translations that are inserted _before_ they get
to {varnish}, which you can find by [searching for `tr_` in
sandpaper](https://github.com/search?q=repo%3Acarpentries%2Fsandpaper+tr_%28+language%3AR+path%3A%2F%5ER%5C%2F%2F&type=code).

Some general ones can be found in the `get_codeblock_translations()`,
`get_callout_translations()`, and `get_accordion_translations()`,

## pkgdown metadata

Expand All @@ -422,12 +495,23 @@ writeLines("```")

This file is what allows {pkgdown} to recognise that a website needs to be
built. These variables are compiled into the `pkg` variable that is passed to
all downstream files from `build_site()`:
all downstream files from `build_site()`. The important elements we use are

- `$lang` stores the language variable
- `$src_path` stores the source path of the lesson (the `site/` directory, or
the location of the `SANDPAPER_SITE` environment variable).
- `$dst_path` stores the destination path to the lesson (`site/docs`).
**NOTE:** this particular path _could_ be changed in `build_site()` so that
we update the name of the path so that it's less confusing. The docs folder
was a mechanism for pkgdown to locally deploy the site without having to
affect the package structure (it was also a mechanism to serve GitHub pages
in the past).
- `$meta` a list of items passed on to {varnish}

```{r pkgdown}
snd <- asNamespace("sandpaper")
pkg <- pkgdown::as_pkgdown(snd$path_site(lsn))
pkg
pkg[c("lang", "src_path", "dst_path", "meta")]
```

Before being sent to be rendered, it goes through one more transformation, which
Expand Down
Loading