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

WIP: Add Support for Translations #546

Merged
merged 90 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
fe7d872
document
joelnitta Nov 22, 2023
e0f460e
Add scaffolding to translate with PO files
joelnitta Nov 22, 2023
3b59e12
Flag strings for translation
joelnitta Nov 22, 2023
c34b7e1
Add POT file
joelnitta Nov 22, 2023
55a9af6
Add Japanese translations
joelnitta Nov 22, 2023
dfa3390
Add test for translations
joelnitta Nov 22, 2023
af5fe22
Use local_envvar_pkgdown() instead of section_init()
joelnitta Nov 23, 2023
f6a23fe
Delete un-needed functions
joelnitta Nov 23, 2023
457e14c
Remove superfluous line
joelnitta Nov 23, 2023
0c45501
Delete un-needed function
joelnitta Nov 23, 2023
94f247b
Insert translations for mustache elements
joelnitta Nov 24, 2023
de69eae
Add JA translations
joelnitta Nov 24, 2023
31876a4
allow translated vars to be included in site data
zkamvar Nov 27, 2023
ffcd994
fix typo
zkamvar Nov 27, 2023
1b64544
add translation terms that I found in varnish
zkamvar Nov 27, 2023
3862270
fix typo
zkamvar Nov 27, 2023
ef431f7
fix more typos
zkamvar Nov 27, 2023
5eb0acb
apply variables to translation strings
zkamvar Nov 27, 2023
f302a25
begin work on fine-tuning translated strings
zkamvar Nov 28, 2023
5634644
clean up varnish terms a bit
zkamvar Nov 28, 2023
57a4a86
re-run potools::po_extract()
zkamvar Nov 28, 2023
3bd6875
run potools::po_update() and fix fuzzy matches
zkamvar Nov 28, 2023
ef7de79
update variable names
zkamvar Nov 28, 2023
b8ad8cb
add lang attribute to pkgdown template
zkamvar Nov 28, 2023
900adfa
add fill_translation_vars function to glue data
zkamvar Nov 28, 2023
fd352e6
Add JA translations
joelnitta Nov 29, 2023
de96250
fix license text with hyperlink
zkamvar Nov 29, 2023
3a17e12
organising a bit more...
zkamvar Nov 30, 2023
9290d47
use for-loop for my gluing
zkamvar Nov 30, 2023
56e430a
use explainer variable
zkamvar Nov 30, 2023
6086fec
add mechanism to include HTML templating
zkamvar Nov 30, 2023
f514620
rerun potools suite and fix fuzzy messages in ja
zkamvar Nov 30, 2023
d647962
compile ja translations to confirm it works.
zkamvar Nov 30, 2023
00e8f30
fixed index sidebar item; add documentation
zkamvar Nov 30, 2023
be2c907
fix complex BuiltWith string and template license
zkamvar Dec 1, 2023
942bac3
fix url templating for specific case
zkamvar Dec 1, 2023
d082e99
rerender po files (there is still one fuzzy)
zkamvar Dec 1, 2023
2d820b8
Update JA translations
joelnitta Dec 1, 2023
ba4d4ff
update license translation string; anchor string
zkamvar Dec 1, 2023
efbca92
add translation for Overview;Objectives;Questions
zkamvar Dec 1, 2023
7d049c8
collect, update, and compile messages
zkamvar Dec 1, 2023
8a8fab9
add documentation for the lang config item
zkamvar Dec 1, 2023
4745196
add Joel as an author and translator
zkamvar Dec 1, 2023
2577c03
document
zkamvar Dec 1, 2023
a071280
add translate_callout_heading()
zkamvar Dec 1, 2023
58a4c64
update translations
zkamvar Dec 1, 2023
52c49da
Merged origin/main into joelnitta-translate
zkamvar Dec 1, 2023
dc78b26
bump version to dev; add news
zkamvar Dec 1, 2023
152c5ad
Update JA translations
joelnitta Dec 2, 2023
8a5f41f
add test for code blocks
zkamvar Dec 4, 2023
ba111d2
update fix_codeblocks to include translations
zkamvar Dec 4, 2023
2884de1
add translations for output blocks
zkamvar Dec 4, 2023
6a4ca89
fix small bug in apply_translations
zkamvar Dec 4, 2023
45fa492
add translations that we will implement in future
zkamvar Dec 4, 2023
0c72780
collect translations; add my attempt for SpanToTop
zkamvar Dec 4, 2023
87ff576
compile translations
zkamvar Dec 4, 2023
8fd705e
update and recompile translations for JS
zkamvar Dec 4, 2023
a860caf
add known_languages() function and `Toggle Menu`
zkamvar Dec 4, 2023
e965a5a
add test for aria menu items
zkamvar Dec 4, 2023
5141dad
update pofiles
zkamvar Dec 4, 2023
3e2a105
update DECRIPTION and NEWS with withr
zkamvar Dec 4, 2023
c44058b
begin work on vignette
zkamvar Dec 4, 2023
7bf25c9
add translations vignette to pkgdown
zkamvar Dec 4, 2023
a7d6b6d
add another paragraph
zkamvar Dec 4, 2023
a462e16
fix breaking tests
zkamvar Dec 5, 2023
a7e934f
ensure the translation vars are always set
zkamvar Dec 5, 2023
7fc90c2
document and add more stuff to vignette
zkamvar Dec 5, 2023
e474e7d
update vignette to be more informative
zkamvar Dec 5, 2023
6181754
update pkgdown
zkamvar Dec 5, 2023
b9ff1e1
Make elements of images section translatable
joelnitta Dec 5, 2023
af4c1a1
Add JA translations
joelnitta Dec 5, 2023
201c1de
use normal glue braces for translation templating
zkamvar Dec 5, 2023
5dcfd5d
update vignette
zkamvar Dec 5, 2023
2413d2a
skip when we are on lower versions of windows
zkamvar Dec 5, 2023
e6ee35b
update NEWS
zkamvar Dec 5, 2023
0678511
make it explicitly less than 4.2
zkamvar Dec 5, 2023
edb6050
skip {renv} test on macOS
zkamvar Dec 5, 2023
4c8a154
skip actual weird test on mac
zkamvar Dec 6, 2023
86d6413
move translating functions into utils-translate
zkamvar Dec 6, 2023
5f72ef3
apply further tests for translations
zkamvar Dec 6, 2023
4ccf515
create code to fix the accordions
zkamvar Dec 6, 2023
d9b76f9
add xml_text_translate(); use for accordions
zkamvar Dec 6, 2023
6c4a820
replace translate_callout_heading
zkamvar Dec 6, 2023
9b43fe1
extract and update translations
zkamvar Dec 6, 2023
fd3f86d
no need to skip on macos anymore
zkamvar Dec 6, 2023
265e4f8
Update JA translations
joelnitta Dec 6, 2023
0cf0919
remove skipper
zkamvar Dec 7, 2023
b82d3e0
change `local_envvar_pkgdown()` to `set_language()`
zkamvar Dec 7, 2023
5f080b2
add catch to not set unknown language
zkamvar Dec 7, 2023
1f51cf1
add some spanish translations
zkamvar Dec 7, 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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ LazyData: true
Config/testthat/edition: 3
Config/testthat/parallel: false
Config/Needs/check: rstudio/renv
Config/potools/style: explicit
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
URL: https://carpentries.github.io/sandpaper/, https://github.com/carpentries/sandpaper/, https://carpentries.github.io/workbench/
Expand Down
2 changes: 1 addition & 1 deletion R/build_404.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ build_404 <- function(pkg, quiet = FALSE) {
this_dat <- list(
this_page = "404.html",
body = html,
pagetitle = "Page not found"
pagetitle = tr_("Page not found")
)
page_globals$instructor$update(this_dat)
page_globals$learner$update(this_dat)
Expand Down
2 changes: 1 addition & 1 deletion R/build_aio.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ build_aio <- function(pkg, pages = NULL, quiet = FALSE) {
build_agg_page(
pkg = pkg,
pages = pages,
title = "All in One View",
title = tr_("All in One View"),
slug = "aio",
aggregate = "*",
prefix = TRUE,
Expand Down
2 changes: 1 addition & 1 deletion R/build_episode.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ get_nav_data <- function(path_md, path_src = NULL, home = NULL,
pf_title <- NULL

if (!is.null(page_back)) {
pb_title <- if (page_back == "index.md") "Home" else get_trimmed_title(page_back)
pb_title <- if (page_back == "index.md") tr_("Home") else get_trimmed_title(page_back)
page_back <- as_html(page_back)
}
if (!is.null(page_forward)) {
Expand Down
6 changes: 3 additions & 3 deletions R/build_home.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ build_home <- function(pkg, quiet, next_page = NULL) {
needs_title <- nav$pagetitle == ""

if (needs_title) {
nav$pagetitle <- "Summary and Schedule"
nav$pagetitle <- tr_("Summary and Schedule")
}
nav$page_forward <- as_html(nav$page_forward, instructor = TRUE)
page_globals$instructor$update(nav)
Expand All @@ -55,7 +55,7 @@ build_home <- function(pkg, quiet, next_page = NULL) {
page_globals$instructor$set("setup", use_instructor(setup))

if (needs_title) {
nav$pagetitle <- "Summary and Setup"
nav$pagetitle <- tr_("Summary and Setup")
}
nav$page_forward <- as_html(nav$page_forward)
page_globals$learner$update(nav)
Expand Down Expand Up @@ -97,7 +97,7 @@ format_syllabus <- function(syl, use_col = TRUE) {
)
}
td1 <- glue::glue(td_template, cls = "col-md-2", thing = syl$timings)
td2 <- glue::glue(td_template, cls = "col-md-3", thing = c(links, "Finish"))
td2 <- glue::glue(td_template, cls = "col-md-3", thing = c(links, tr_("Finish")))
td3 <- glue::glue(td_template, cls = "col-md-7", thing = syl$questions)
out <- glue::glue_collapse(glue::glue("<tr>{td1}{td2}{td3}</tr>"), sep = "\n")
tmp <- tempfile(fileext = ".md")
Expand Down
2 changes: 1 addition & 1 deletion R/build_images.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ build_images <- function(pkg, pages = NULL, quiet = FALSE) {
build_agg_page(
pkg = pkg,
pages = pages,
title = "All Images",
title = tr_("All Images"),
slug = "images",
aggregate = "/img/..",
prefix = FALSE,
Expand Down
2 changes: 1 addition & 1 deletion R/build_instructor_notes.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ build_instructor_notes <- function(pkg, pages = NULL, built = NULL, quiet) {
this_dat <- list(
this_page = "instructor-notes.html",
body = use_instructor(html),
pagetitle = "Instructor Notes"
pagetitle = tr_("Instructor Notes")
)

page_globals$instructor$update(this_dat)
Expand Down
2 changes: 1 addition & 1 deletion R/build_keypoints.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ build_keypoints <- function(pkg, pages = NULL, quiet = FALSE) {
build_agg_page(
pkg = pkg,
pages = pages,
title = "Key Points",
title = tr_("Key Points"),
slug = "key-points",
aggregate = "/div[starts-with(@id, 'keypoints')]/div[@class='callout-inner']/div[@class='callout-content']/*",
prefix = FALSE,
Expand Down
2 changes: 1 addition & 1 deletion R/build_profiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ build_profiles <- function(pkg, quiet) {
this_dat <- list(
this_page = "profiles.html",
body = use_instructor(html),
pagetitle = "Learner Profiles"
pagetitle = tr_("Learner Profiles")
)
page_globals$instructor$update(this_dat)

Expand Down
1 change: 1 addition & 0 deletions R/build_site.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ build_site <- function(path = ".", quiet = !interactive(), preview = TRUE, overr
# Here we provision our website using pkgdown and either initialise it if it
# does not exist or update the CSS, HTML, and JS if it does exist.
pkg <- pkgdown::as_pkgdown(path_site(path), override = override)
pkg <- section_init(pkg, depth = 0, override = override)
joelnitta marked this conversation as resolved.
Show resolved Hide resolved
built_path <- fs::path(pkg$src_path, "built")
# NOTE: This is a kludge to prevent pkgdown from displaying a bunch of noise
# if the user asks for quiet.
Expand Down
3 changes: 3 additions & 0 deletions R/utils-potools.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tr_ <- function(...) {
enc2utf8(gettext(paste0(...), domain = "R-sandpaper"))
joelnitta marked this conversation as resolved.
Show resolved Hide resolved
}
71 changes: 71 additions & 0 deletions R/utils-translate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
local_envvar_pkgdown <- function(pkg, scope = parent.frame()) {
withr::local_envvar(
IN_PKGDOWN = "true",
LANGUAGE = pkg$meta$template$params$lang,
.local_envir = scope
)
}

section_init <- function(
pkg, depth, override = list(), .frame = parent.frame()) {
pkg <- pkgdown::as_pkgdown(pkg, override = override)

rstudio_save_all()
local_envvar_pkgdown(pkg, .frame)
local_options_link(pkg, depth = depth, .frame = .frame)

pkg
}

rstudio_save_all <- function() {
if (
rlang::is_installed("rstudioapi") &&
rstudioapi::hasFun("documentSaveAll")) {
rstudioapi::documentSaveAll()
}
}

local_options_link <- function(pkg, depth, .frame = parent.frame()) {
article_index <- article_index(pkg)
rdname <- get_rdname(pkg$topics)
topic_index <- unlist(invert_index(
purrr::set_names(pkg$topics$alias, rdname)))

withr::local_options(
list(
downlit.package = pkg$package,
downlit.article_index = article_index,
downlit.topic_index = topic_index,
downlit.article_path = paste0(up_path(depth), "articles/"),
downlit.topic_path = paste0(up_path(depth), "reference/")
),
.local_envir = .frame
)
}

article_index <- function(pkg) {
purrr::set_names(
fs::path_rel(pkg$vignettes$file_out, "articles"),
fs::path_file(pkg$vignettes$name)
)
}

get_rdname <- function(topics) {
gsub("\\.[Rr]d$", "", topics$file_in)
}

invert_index <- function(x) {
stopifnot(is.list(x))

if (length(x) == 0)
return(list())

key <- rep(names(x), purrr::map_int(x, length))
val <- unlist(x, use.names = FALSE)

split(key, val)
}

up_path <- function(depth) {
paste(rep.int("../", depth), collapse = "")
}
joelnitta marked this conversation as resolved.
Show resolved Hide resolved
Binary file added inst/po/ja/LC_MESSAGES/R-sandpaper.mo
Binary file not shown.
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.

52 changes: 52 additions & 0 deletions po/R-ja.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
msgid ""
msgstr ""
"Project-Id-Version: sandpaper 0.14.1.9000\n"
"POT-Creation-Date: 2023-11-22 15:19+0900\n"
"PO-Revision-Date: 2023-11-22 09:14+0900\n"
"Last-Translator: Automatically generated\n"
"Language-Team: none\n"
"Language: ja\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=1; plural=0;\n"

#: build_404.R:57
msgid "Page not found"
msgstr "ページが見つかりません"

#: build_aio.R:6
msgid "All in One View"
msgstr "一つにまとまったバージョン"

#: build_episode.R:134
msgid "Home"
msgstr "ホーム"

#: build_home.R:49
msgid "Summary and Schedule"
msgstr "概要とスケジュール"

#: build_home.R:58
msgid "Summary and Setup"
msgstr "概要とセットアップ"

#: build_home.R:100
msgid "Finish"
msgstr "終わり"

#: build_images.R:6
msgid "All Images"
msgstr "全ての画像"

#: build_instructor_notes.R:27
msgid "Instructor Notes"
msgstr "インストラクター用のメモ"

#: build_keypoints.R:6
msgid "Key Points"
msgstr "まとめ"

#: build_profiles.R:16
msgid "Learner Profiles"
msgstr "学習者プロフィール"
51 changes: 51 additions & 0 deletions po/R-sandpaper.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
msgid ""
msgstr ""
"Project-Id-Version: sandpaper 0.14.1.9000\n"
"POT-Creation-Date: 2023-11-22 15:19+0900\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: build_404.R:57
msgid "Page not found"
msgstr ""

#: build_aio.R:6
msgid "All in One View"
msgstr ""

#: build_episode.R:134
msgid "Home"
msgstr ""

#: build_home.R:49
msgid "Summary and Schedule"
msgstr ""

#: build_home.R:58
msgid "Summary and Setup"
msgstr ""

#: build_home.R:100
msgid "Finish"
msgstr ""

#: build_images.R:6
msgid "All Images"
msgstr ""

#: build_instructor_notes.R:27
msgid "Instructor Notes"
msgstr ""

#: build_keypoints.R:6
msgid "Key Points"
msgstr ""

#: build_profiles.R:16
msgid "Learner Profiles"
msgstr ""
38 changes: 38 additions & 0 deletions tests/testthat/test-translate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generate temporary lesson and set `lang: ja` in config.yaml
tmp <- res <- restore_fixture()
config_path <- fs::path(tmp, "config.yaml")
config <- yaml::read_yaml(config_path)
config$lang <- "ja"
yaml::write_yaml(config, config_path)
metadata_json <- trimws(create_metadata_jsonld(tmp))
joelnitta marked this conversation as resolved.
Show resolved Hide resolved
sitepath <- fs::path(tmp, "site", "docs")

test_that("Lessons can be translated with lang setting", {

skip_if_not(rmarkdown::pandoc_available("2.11"))

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

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

# Header should be translated to Japanese
expect_true(
identical(
xml2::xml_text(h1_header),
withr::with_language("ja", tr_("Summary and Setup"))
)
)

# Header should no longer be in English
expect_false(
identical(
xml2::xml_text(h1_header),
withr::with_language("en", tr_("Summary and Setup"))
)
)
joelnitta marked this conversation as resolved.
Show resolved Hide resolved

})