Skip to content

Support for translating PDF files #85

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

Merged
merged 12 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -37,6 +38,7 @@ Imports:
tibble,
utils
Suggests:
pdftools,
cld2,
testthat,
knitr,
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
83 changes: 83 additions & 0 deletions R/translate-document.R
Original file line number Diff line number Diff line change
@@ -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)

}

Binary file added inst/test-doc-no.pdf
Binary file not shown.
Binary file added inst/test-doc.pdf
Binary file not shown.
43 changes: 43 additions & 0 deletions man/gl_translate_document.Rd

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

1 change: 1 addition & 0 deletions man/gl_translate_languages.Rd

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

17 changes: 17 additions & 0 deletions tests/testthat/test-translate-document.R
Original file line number Diff line number Diff line change
@@ -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)
})