diff --git a/DESCRIPTION b/DESCRIPTION index 60e7191..9736c78 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,8 @@ Package: googleLanguageR Title: Call Google's 'Natural Language' API, 'Cloud Translation' API, 'Cloud Speech' API and 'Cloud Text-to-Speech' API Version: 0.3.0.9000 -Authors@R: c(person("Mark", "Edmondson", email = "r@sunholo.com", role = c("aut", "cre")), +Authors@R: c(person("Aleksander", "Dietrichson",email = "dietrichson@gmail.com", role=c("ctb")), + person("Mark", "Edmondson", email = "r@sunholo.com", role = c("aut", "cre")), person("John", "Muschelli", email = "muschellij2@gmail.com", role = c("ctb")), person("Neal", "Richardson", email = "neal.p.richardson@gmail.com", role = "rev", comment = "Neal reviewed the package for ropensci, @@ -24,7 +25,7 @@ Depends: R (>= 3.3) License: MIT + file LICENSE Encoding: UTF-8 LazyData: true -RoxygenNote: 7.1.0 +RoxygenNote: 7.2.3 VignetteBuilder: knitr Imports: assertthat, @@ -37,6 +38,7 @@ Imports: tibble, utils Suggests: + pdftools, cld2, testthat, knitr, diff --git a/NAMESPACE b/NAMESPACE index 2c6b54e..8050384 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -13,9 +13,12 @@ export(gl_talk_shiny) export(gl_talk_shinyUI) export(gl_translate) export(gl_translate_detect) +export(gl_translate_document) export(gl_translate_languages) import(assertthat) import(base64enc) +importFrom(base64enc,base64decode) +importFrom(base64enc,base64encode) importFrom(googleAuthR,gar_api_generator) importFrom(googleAuthR,gar_attach_auto_auth) importFrom(googleAuthR,gar_auth_service) diff --git a/R/translate-document.R b/R/translate-document.R new file mode 100644 index 0000000..a6c08a8 --- /dev/null +++ b/R/translate-document.R @@ -0,0 +1,83 @@ +#' Translate document +#' +#' Translate a document via the Google Translate API +#' + +#' +#' @param d_path path of the document to be translated +#' @param output_path where to save the translated document +#' @param format currently only pdf-files are supported +#' +#' @return output filename +#' @family translations +#' @import assertthat +#' @importFrom base64enc base64encode +#' @importFrom base64enc base64decode +#' @importFrom utils URLencode +#' @importFrom googleAuthR gar_api_generator +#' @importFrom tibble as_tibble +#' @importFrom stats setNames +#' @export +#' + #' @examples +#' +#' \dontrun{ +#' gl_translate_document(system.file(package = "googleLanguageR","test-doc.pdf"), "no") +#' +#' } +gl_translate_document <- function(d_path, + target = "es-ES", + output_path = "out.pdf", + format = c("pdf"), + source = 'en-UK', + model = c("nmt", "base"), + + location = "global"){ + + ## Checks + assert_that(is.character(d_path), + is.character(output_path), + is.string(target), + is.string(source)) + + format <- match.arg(format) + model <- match.arg(model) + + format <- paste0("application/",format) + + if(file.exists(output_path)) stop("Output file already exists.") + + payload <- + list( + target_language_code = target, + source_language_code = source, + document_input_config = list( + mimeType = format, + content = base64encode(d_path) + ) + ) + + + project_id <- gar_token()$auth_token$secrets$project_id + LOCATION <- location + + my_URI <- paste0( + "https://translation.googleapis.com/v3beta1/projects/", + project_id, + "/locations/", + location,":translateDocument") + + + call_api <- gar_api_generator(my_URI, "POST" ) + + me <- tryCatch(call_api(the_body = payload), error=function(e){print(e)}) + + writeBin( + base64decode( + me$content$documentTranslation[[1]] + ), output_path) + + path.expand(output_path) + +} + diff --git a/inst/test-doc-no.pdf b/inst/test-doc-no.pdf new file mode 100644 index 0000000..8a458a8 Binary files /dev/null and b/inst/test-doc-no.pdf differ diff --git a/inst/test-doc.pdf b/inst/test-doc.pdf new file mode 100644 index 0000000..c14729d Binary files /dev/null and b/inst/test-doc.pdf differ diff --git a/man/gl_translate_document.Rd b/man/gl_translate_document.Rd new file mode 100644 index 0000000..46b6fa4 --- /dev/null +++ b/man/gl_translate_document.Rd @@ -0,0 +1,43 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/translate-document.R +\name{gl_translate_document} +\alias{gl_translate_document} +\title{Translate document} +\usage{ +gl_translate_document( + d_path, + target = "es-ES", + output_path = "out.pdf", + format = c("pdf"), + source = "en-UK", + model = c("nmt", "base"), + location = "global" +) +} +\arguments{ +\item{d_path}{path of the document to be translated} + +\item{output_path}{where to save the translated document} + +\item{format}{currently only pdf-files are supported} +} +\value{ +output filename +} +\description{ +Translate a document via the Google Translate API +} +\examples{ + +\dontrun{ +gl_translate_document(system.file(package = "googleLanguageR","test-doc.pdf"), "no") + +} +} +\seealso{ +Other translations: +\code{\link{gl_translate_detect}()}, +\code{\link{gl_translate_languages}()}, +\code{\link{gl_translate}()} +} +\concept{translations} diff --git a/man/gl_translate_languages.Rd b/man/gl_translate_languages.Rd index 00dd10b..0651593 100644 --- a/man/gl_translate_languages.Rd +++ b/man/gl_translate_languages.Rd @@ -36,6 +36,7 @@ gl_translate_languages("da") Other translations: \code{\link{gl_translate_detect}()}, +\code{\link{gl_translate_document}()}, \code{\link{gl_translate}()} } \concept{translations} diff --git a/tests/testthat/test-translate-document.R b/tests/testthat/test-translate-document.R new file mode 100644 index 0000000..f5e2735 --- /dev/null +++ b/tests/testthat/test-translate-document.R @@ -0,0 +1,17 @@ +source("prep_tests.R") +test_that("document tranlation works", { + skip_on_cran() + skip_on_travis() + + my_out <- tempfile(fileext = ".pdf") + + gl_translate_document(system.file(package = "googleLanguageR","test-doc.pdf"), target = "no",output_path = my_out) + + my_pdf1 <-pdftools::pdf_data(my_out) + + my_pdf2 <- pdftools::pdf_data( + system.file(package = "googleLanguageR","test-doc-no.pdf") + ) + + expect_equal(my_pdf1[[1]]$text, my_pdf2[[1]]$text) +})