translation.R

# NEW LANGUAGE TRANSLATION PROTOCOL ------------------------------------------------------------------------

# 0. Create a new branch for a new language

# 1. First install the babeldown package
#install.packages('babeldown', repos = c('https://ropensci.r-universe.dev', 'https://cloud.r-project.org'))


# 2.Setup the environment variable
Sys.setenv("DEEPL_API_URL" = "https://api.deepl.com")
Sys.setenv(DEEPL_API_KEY = "287d5481-9d96-8500-228c-6f98cfb3c576")



# 3. Indicate modules to be translated
module_list = c(#"intro01"
                "intro02",
                "intro03",
                "intro04",
                "intro05-1",
                "intro05-2"
                #"intro06",
                #"intro07",
                #"intro08-1",
                #"intro08-2",
                #"intro09",
                #"intro10"
                )

base_directory <- here::here("inst", "tutorials")

languages <- c(#"es"
               "fr",
               #"pt"
               )

# 4. Write a loop to run the translation for each module
for (i in 1:length(module_list)) {
  for (j in 1:length(languages)) {
    babeldown::deepl_translate_quarto(
      book_path = paste0(base_directory, "/", module_list[i]),
      chapter = paste0(module_list[i], ".Rmd"),
      force = TRUE,
      render = FALSE, # Whether to run babelquarto::render_bool() after translation.
      source_lang = "EN",
      target_lang = toupper(languages[j]),
      formality = "less")
  }
}


# 5. Create new folder for language version



# Function to process each module
create_LangugeModule <- function(base_directory, module_name, languages) {
  for (lang in languages) {
    # Construct the file and folder names
    file_name <- paste0(module_name, ".", lang, ".Rmd")
    file_name_new <- paste0(module_name, "_", lang, ".Rmd")
    folder_name <- paste0(module_name, "_", lang)
    file_path <- file.path(base_directory, module_name, file_name)
    new_folder_path <- file.path(base_directory, folder_name)
    images_path <- file.path(base_directory, module_name, "images") # Path to the images folder in the English version

    # Check if the file exists
    if (file.exists(file_path)) {
      # Create language-specific folder if it doesn't exist
      if (!dir.exists(new_folder_path)) {
        dir.create(new_folder_path, recursive = TRUE)
      }

      # Move the .Rmd files
      file.rename(from = file_path, to = file.path(new_folder_path, file_name_new))

      # Copy the images folder if it exists in the English version folder
      if (dir.exists(images_path)) {
        # Copy all contents of the images folder directly to the new language version folder
        if (!dir.exists(file.path(new_folder_path, "images"))) {
          dir.create(file.path(new_folder_path, "images"), recursive = TRUE)
        }
        # Use list.files to identify all items in the images directory and copy them individually
        files_to_copy <- list.files(images_path, full.names = TRUE)
        sapply(files_to_copy, function(x) file.copy(from = x, to = file.path(new_folder_path, "images"), recursive = TRUE))
      }
    }
  }
}

# Apply the function to each module
for (module in module_list) {
  create_LangugeModule(base_directory, module, languages)
}

# 6. Review the translation
appliedepi/introexercises documentation built on April 22, 2024, 1:01 a.m.