From 0a6deb2d35b4a2efa4edc53c8adb01e2bf9cf9ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Thu, 30 Jan 2025 16:01:35 +0100 Subject: [PATCH 1/3] feat: improve language button --- R/render.R | 34 ++++++++++++++++++++++++++++++++-- vignettes/configuration.qmd | 11 +++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/R/render.R b/R/render.R index bb08b73..5e96059 100644 --- a/R/render.R +++ b/R/render.R @@ -481,6 +481,11 @@ add_links <- function(path, main_language, # nolint: cyclocomp_linter "//div[contains(@class,'sidebar-menu-container')]" ) + # case where the page is just a redirection + if (inherits(sidebar_menu, "xml_missing")) { + return() + } + xml2::xml_add_sibling( sidebar_menu, "div", @@ -502,10 +507,17 @@ add_links <- function(path, main_language, # nolint: cyclocomp_linter id = "languages-button" ) + button <- xml2::xml_find_first(html, "//button[@id='languages-button']") + xml2::xml_add_child( - xml2::xml_find_first(html, "//button[@id='languages-button']"), + button, "i", - class = "bi bi-globe2" + class = config[["babelquarto"]][["icon"]] %||% "bi bi-globe2" + ) + + xml2::xml_text(button) <- sprintf( + " %s", + find_language_name(path_language, config) ) xml2::xml_add_child( @@ -595,3 +607,21 @@ path_rel <- function(path, output_folder, lang, main_language) { fs::path_rel(path, start = file.path(output_folder, lang)) } } + +find_language_name <- function(language_code, config) { + codes <- config[["babelquarto"]][["languagecodes"]] + + if (is.null(codes)) { + return(toupper(language_code)) + } + + language_names <- purrr::map_chr(codes, "name") + language_texts <- purrr::map_chr(codes, "text") + + if (!language_code %in% language_names) { + return(toupper(language_code)) + } + + language_texts[language_names == language_code][1] + +} diff --git a/vignettes/configuration.qmd b/vignettes/configuration.qmd index c5b25b8..73e5841 100644 --- a/vignettes/configuration.qmd +++ b/vignettes/configuration.qmd @@ -74,6 +74,17 @@ website: If you render your multilingual book or website in a CI context, you need will need to set the `BABELQUARTO_CI_URL` environment variable. See `vignette("render-with-ci")` for more information. +## Configure the icon of the language switch button + +Any [Bootstrap icon](https://icons.getbootstrap.com/) can be used instead of the globe button. +For getting a backpack icon you'd use: + +```yaml +babelquarto: + icon: "bi bi-backpack" + +``` + ## Translate parts in multilingual books In multilingual books, if you use parts to structure your book, you can translate the part titles like so: From 8060b915d167115e460ba80598b1d8e830dabaf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Thu, 30 Jan 2025 16:06:17 +0100 Subject: [PATCH 2/3] test: update snapshots (newer Quarto I guess) --- tests/testthat/_snaps/register/_quarto.yml | 4 +++- tests/testthat/_snaps/register/_quarto2.yml | 4 +++- tests/testthat/_snaps/register/_quarto3.yml | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/testthat/_snaps/register/_quarto.yml b/tests/testthat/_snaps/register/_quarto.yml index 0a74055..2b2f01b 100644 --- a/tests/testthat/_snaps/register/_quarto.yml +++ b/tests/testthat/_snaps/register/_quarto.yml @@ -16,7 +16,9 @@ bibliography: references.bib format: html: - theme: cosmo + theme: + - cosmo + - brand babelquarto: languagelinks: sidebar diff --git a/tests/testthat/_snaps/register/_quarto2.yml b/tests/testthat/_snaps/register/_quarto2.yml index 4f6e7d6..12cf401 100644 --- a/tests/testthat/_snaps/register/_quarto2.yml +++ b/tests/testthat/_snaps/register/_quarto2.yml @@ -16,7 +16,9 @@ bibliography: references.bib format: html: - theme: cosmo + theme: + - cosmo + - brand babelquarto: languagelinks: sidebar diff --git a/tests/testthat/_snaps/register/_quarto3.yml b/tests/testthat/_snaps/register/_quarto3.yml index 4f6e7d6..12cf401 100644 --- a/tests/testthat/_snaps/register/_quarto3.yml +++ b/tests/testthat/_snaps/register/_quarto3.yml @@ -16,7 +16,9 @@ bibliography: references.bib format: html: - theme: cosmo + theme: + - cosmo + - brand babelquarto: languagelinks: sidebar From 690350b9bbf5bef66efbae86281e46f1b68b1809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Thu, 30 Jan 2025 16:10:15 +0100 Subject: [PATCH 3/3] test: make it more robust to changes in default hopefully --- tests/testthat/test-render.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-render.R b/tests/testthat/test-render.R index 1ba532c..ad843cc 100644 --- a/tests/testthat/test-render.R +++ b/tests/testthat/test-render.R @@ -144,7 +144,7 @@ test_that("render_book() works -- partial template", { config <- append( config, c(" template-partials:", " - metadata.html"), - after = grep("theme: cosmo", config, fixed = TRUE) + after = grep("html:", config, fixed = TRUE) ) brio::write_lines(config, config_path)