ropensci-review-tools / babeldown

Helpers for Automatic Translation of Markdown-based Content
https://docs.ropensci.org/babeldown/
Other
21 stars 2 forks source link

Translate comments in code #73

Open yabellini opened 1 week ago

yabellini commented 1 week ago

Sometimes, we have code examples with comments explaining the code. It would be nice to be able to translate those comments.

Example:

Original:

# Load packages
library(tidyverse) # for {dplyr} functions and the pipe %>%
library(rio) # for importing data
library(here) # for easy file referencing
library(cleanepi)

Translated to Spanish:

# Cargar paquetes
library(tidyverse) # para las funciones de {dplyr} y el pipe %>%
library(rio) # para importar los datos
library(here) # para referencia sencilla de archivos
library(cleanepi)
maelle commented 3 days ago

Some thoughts.

Currently we do not parse the code at all. To parse it and differentiate comments from code, we'd need to use a code parser (base R + xmlparsedata would allow us to use XPath; treesitter).

In code comments how would we escape code. For instance we don't want to translate variable names, package names, etc.

At the moment because we do not translate code comments, one would obtain better results by extracting/repeating the ideas of the code comments in text around the code chunks.


In the code below we use the packages tidyverse (for dplyr functions and the pipe), rio (for importing data), here (for easy file referencing) and cleanepi.

```r
# Load packages
library(tidyverse) # for {dplyr} functions and the pipe %>%
library(rio) # for importing data
library(here) # for easy file referencing
library(cleanepi)