Skip to content

Commit

Permalink
feat: translate file
Browse files Browse the repository at this point in the history
  • Loading branch information
tin900 committed Jun 16, 2023
1 parent 00f1893 commit b23e16f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
export("%>%")
export(google_translate)
export(mymemory_translate)
export(translate_file)
importFrom(magrittr,"%>%")
18 changes: 17 additions & 1 deletion R/google_translate_file.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
#' Translate File
#'
#' Translates the content of a file using Google Translate API.
#'
#' @param file_path The path to the file to be translated.
#' @param target_language The target language to translate the file content to. Default is "en".
#' @param source_language The source language of the file content. Default is "auto".
#' @param overwrite Logical indicating whether to overwrite the original file with the translated content. Default is FALSE.
#'
#' @return NULL
#'
#' @examples
#' \dontrun{
#' translate_file("path/to/file.txt", target_language = "fr", source_language = "en", overwrite = TRUE)
#' }
#' @export
translate_file <- function(file_path, target_language = "en", source_language = "auto", overwrite = FALSE) {

lines <- readLines(file_path, warn = FALSE, encoding = "UTF-8")
Expand All @@ -10,7 +26,7 @@ translate_file <- function(file_path, target_language = "en", source_language =
}
}

translated_lines <- sapply(lines, translate_line)
translated_lines <- vapply(lines, translate_line, character(1))

combined_lines <- mapply(function(original, translated) {
return(paste0(substr(original, 1, regexpr("[^ ]", original) - 1), translated))
Expand Down
30 changes: 30 additions & 0 deletions man/translate_file.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b23e16f

Please sign in to comment.