Skip to content

Commit

Permalink
feat: translate fig-alt as seen in Quarto docs
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Nov 28, 2023
1 parent b94bfbe commit 4fab5d7
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
36 changes: 34 additions & 2 deletions R/translate.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ deepl_translate <- function(path,
glossary_id = glossary_id,
source_lang = source_lang,
target_lang = target_lang,
formality = formality
formality = formality,
glossary_name = glossary_name
)

# write back, unprotect Hugo shortcodes ----
Expand All @@ -153,7 +154,12 @@ deepl_translate <- function(path,
brio::write_lines(markdown_lines, out_path)
}

translate_part <- function(xml, glossary_id, source_lang, target_lang, formality) {
translate_part <- function(xml,
glossary_id,
source_lang,
target_lang,
formality,
glossary_name) {
temp_file <- withr::local_tempfile()
file.create(temp_file)
woolish <- tinkr::yarn$new(path = temp_file)
Expand Down Expand Up @@ -212,6 +218,14 @@ translate_part <- function(xml, glossary_id, source_lang, target_lang, formality
xml2::xml_name(curly) <- "text"
}
purrr::walk(curlies, replace_curly)
### special case for fig-alt
purrr::walk(
curlies, translate_alt_curly,
glossary_name = glossary_name,
source_lang = source_lang,
target_lang = target_lang,
formality = formality
)

## Make non code blocks code blocks again ----
non_code_blocks <- xml2::xml_find_all(woolish$body, "//d1:non_code_block")
Expand All @@ -220,6 +234,7 @@ translate_part <- function(xml, glossary_id, source_lang, target_lang, formality
}
purrr::walk(non_code_blocks, replace_non_code_block)


woolish[["body"]]
}

Expand Down Expand Up @@ -319,3 +334,20 @@ translate_shortcode <- function(shortcode,
}
shortcode
}

translate_alt_curly <- function(curly, glossary_name, source_lang, target_lang, formality) {
has_alt <- !is.na(xml2::xml_attr(curly, "alt"))
if (has_alt) {
translated_alt <- deepl_translate_markdown_string(
xml2::xml_attr(curly, "alt"),
glossary_name = glossary_name,
source_lang = source_lang,
target_lang = target_lang,
formality = formality
)
xml2::xml_text(curly) <- sprintf(
'{fig-alt="%s"}',
translated_alt
)
}
}
1 change: 1 addition & 0 deletions inst/example-alt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
![rOpenSci Localizacion Process](images/localization_concept_map.png){fig-alt="Localization process cicle"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"translations": [
{
"detected_source_language": "EN",
"text": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n<document xmlns=\"http://commonmark.org/xml/1.0\">\n <paragraph>\n <image destination=\"images/localization_concept_map.png\" title=\"\">\n <text xml:space=\"preserve\">Proceso de Localización de rOpenSci<\/text>\n <\/image>\n <text xml:space=\"preserve\"/>\n <curly curly=\"true\" alt=\"Localization process cicle\">{fig-alt=\"Localization process cicle\"}<\/curly>\n <text/>\n <\/paragraph>\n<\/document>\n"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"translations": [
{
"detected_source_language": "EN",
"text": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n<document xmlns=\"http://commonmark.org/xml/1.0\">\n <paragraph>\n <text xml:space=\"preserve\">Ciclo del proceso de localización<\/text>\n <\/paragraph>\n<\/document>\n"
}
]
}
19 changes: 19 additions & 0 deletions tests/testthat/test-translate.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,22 @@ test_that("deepl_translate() doesn't remove backslashes", {
expect_no_match(lines, " U00B7")
})
})

test_that("deepl_translate() translate fig-alt", {
with_mock_dir("example-alt", {
to_translate <- system.file("example-alt.md", package = "babeldown")
out_path <- withr::local_tempfile()

deepl_translate(
path = to_translate,
out_path = out_path,
source_lang = "EN",
target_lang = "ES",
formality = "less",
yaml_fields = NULL
)

lines <- readLines(out_path)
expect_no_match(lines, "circle")
})
})

0 comments on commit 4fab5d7

Please sign in to comment.