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

Catalog bare words in templates that should be used for translation #104

Closed
zkamvar opened this issue Nov 23, 2023 · 1 comment
Closed
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@zkamvar
Copy link
Contributor

zkamvar commented Nov 23, 2023

to support carpentries/sandpaper#546, {varnish} needs to be able to support translations. One of the ways we do this is via the $translate field in the pkgdown data.

based on carpentries/sandpaper#205 (comment), hard-coded English text needs to be replaced with mustache templating:

For example, this link to key points from the header has "Key Points" hard-coded:

<a class="nav-link" href="{{#site}}{{root}}{{/site}}key-points.html">
Key Points
</a>

To use translations, we should do the following:

  1. in {sandpaper} add a function that will add $translate field to the {pkgdown} object in build_site() that will include a translation for keypoints:
    add_varnish_translations <- function(pkg) {
      pkg$translate <- list(
        keypoints = tr_("Key Points")
      )
      return(pkg)
    }
  2. in {varnish}, use the mustache template to replace the hard-coded text:
    <a class="nav-link" href="{{#site}}{{root}}{{/site}}key-points.html">
    {{#translate}}{{keypoints}}{{/translate}}
    </a>

Once we come up with a catalog, then we can pass that to {sandpaper} and have the translations available.

@zkamvar zkamvar added enhancement New feature or request help wanted Extra attention is needed labels Nov 23, 2023
@zkamvar
Copy link
Contributor Author

zkamvar commented Nov 27, 2023

This has been implemented in carpentries/sandpaper#546 (comment), but it's a bit incomplete because we need to consider phrases that include variables so that more rich translations can be implemented.

Here is the code to extract the text to be translated:

get_bare_text <- function(path) {
  html <- xml2::read_html(path)
  no_brace <- "not(starts-with(normalize-space(text()), '{'))"
  no_blank <- "not(normalize-space(text())='')"
  no_mathjax <- "not(starts-with(normalize-space(text()), 'MathJax.Hub'))"
  no_docsearch <- "not(starts-with(normalize-space(text()), 'docsearch'))"

  xpath <- sprintf(".//*[%s and %s and %s and %s]", 
    no_brace, no_blank, no_mathjax, no_docsearch)
  nodes <- xml2::xml_find_all(html, xpath)
  unique(trimws(xml2::xml_text(nodes)))
}

get_varnish_text <- function() {
  templates <- system.file("pkgdown", "templates", package = "varnish")
  res <- lapply(list.files(templates, pattern = "*html", full.names = TRUE),
    get_bare_text)
  reslist <- unique(unlist(res, use.names = FALSE))
  resvar <- abbreviate(reslist, 6)
  clipr::write_clip(paste0(resvar, " = _tr(", sQuote(reslist, q = 2), "),"))
}

@zkamvar zkamvar closed this as completed Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant