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

feat: improve language button #94

Merged
merged 3 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
34 changes: 32 additions & 2 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,11 @@
"//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",
Expand All @@ -502,10 +507,17 @@
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(
Expand Down Expand Up @@ -595,3 +607,21 @@
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]

Check warning on line 625 in R/render.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/render.R,line=625,col=50,[extraction_operator_linter] Use `[[` instead of `[` to extract an element.

Check warning on line 625 in R/render.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/render.R,line=625,col=52,[implicit_integer_linter] Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.

}
4 changes: 3 additions & 1 deletion tests/testthat/_snaps/register/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ bibliography: references.bib

format:
html:
theme: cosmo
theme:
- cosmo
- brand

babelquarto:
languagelinks: sidebar
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/_snaps/register/_quarto2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ bibliography: references.bib

format:
html:
theme: cosmo
theme:
- cosmo
- brand

babelquarto:
languagelinks: sidebar
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/_snaps/register/_quarto3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ bibliography: references.bib

format:
html:
theme: cosmo
theme:
- cosmo
- brand

babelquarto:
languagelinks: sidebar
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-render.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
11 changes: 11 additions & 0 deletions vignettes/configuration.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading