Skip to content

Commit 22ee73c

Browse files
committed
feat: improve language button
1 parent d2c37b1 commit 22ee73c

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

R/render.R

+32-2
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,11 @@ add_links <- function(path, main_language, # nolint: cyclocomp_linter
481481
"//div[contains(@class,'sidebar-menu-container')]"
482482
)
483483

484+
# case where the page is just a redirection
485+
if (inherits(sidebar_menu, "xml_missing")) {
486+
return()
487+
}
488+
484489
xml2::xml_add_sibling(
485490
sidebar_menu,
486491
"div",
@@ -502,10 +507,17 @@ add_links <- function(path, main_language, # nolint: cyclocomp_linter
502507
id = "languages-button"
503508
)
504509

510+
button <- xml2::xml_find_first(html, "//button[@id='languages-button']")
511+
505512
xml2::xml_add_child(
506-
xml2::xml_find_first(html, "//button[@id='languages-button']"),
513+
button,
507514
"i",
508-
class = "bi bi-globe2"
515+
class = config[["babelquarto"]][["icon"]] %||% "bi bi-globe2"
516+
)
517+
518+
xml2::xml_text(button) <- sprintf(
519+
" %s",
520+
find_language_name(path_language, config)
509521
)
510522

511523
xml2::xml_add_child(
@@ -595,3 +607,21 @@ path_rel <- function(path, output_folder, lang, main_language) {
595607
fs::path_rel(path, start = file.path(output_folder, lang))
596608
}
597609
}
610+
611+
find_language_name <- function(language_code, config) {
612+
codes <- config[["babelquarto"]][["languagecodes"]]
613+
614+
if (is.null(codes)) {
615+
return(toupper(language_code))
616+
}
617+
618+
language_names <- purrr::map_chr(codes, "name")
619+
language_texts <- purrr::map_chr(codes, "text")
620+
621+
if (!language_code %in% language_names) {
622+
return(toupper(language_code))
623+
}
624+
625+
language_texts[language_names == language_code][1]
626+
627+
}

vignettes/configuration.qmd

+11
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ website:
7474

7575
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.
7676

77+
## Configure the icon of the language switch button
78+
79+
Any [Bootstrap icon](https://icons.getbootstrap.com/) can be used instead of the globe button.
80+
For getting a backpack icon you'd use:
81+
82+
```yaml
83+
babelquarto:
84+
icon: "bi bi-backpack"
85+
86+
```
87+
7788
## Translate parts in multilingual books
7889

7990
In multilingual books, if you use parts to structure your book, you can translate the part titles like so:

0 commit comments

Comments
 (0)