Skip to content

Commit

Permalink
Initial try [ropensci#84]
Browse files Browse the repository at this point in the history
  • Loading branch information
dietrichson committed Jun 1, 2023
1 parent 27e1e0e commit 904fbea
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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 Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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)
Expand Down
105 changes: 105 additions & 0 deletions R/translate-document.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#' Translate document
#'
#' Translate a document via the Google Translate API
#'

#'
#' @param d_path path of the document to be translated
#' @param out_path where to save the translated document
#' @param format currently only pdf-files are supported
#'
#' @return invisible() on success
#' @family translations
#' @import assertthat
#' @importFrom utils URLencode
#' @importFrom googleAuthR gar_api_generator
#' @importFrom tibble as_tibble
#' @importFrom stats setNames
#' @export
gl_translate_document <- function(d_path,
target = "es-ES",
out_path = "out.pdf",
format = c("pdf"),
source = 'en-UK',
model = c("nmt", "base"),

location = "global"){

## Checks
assert_that(is.character(d_path),
is.character(out_path),
is.string(target),
is.string(source))

format <- match.arg(format)
model <- match.arg(model)

format <- paste0("application/",format)

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))
# me <- tryCatch(call_api(the_body = pars),
# error = function(ex){
# if(grepl(catch_errors,
# ex$message)){
# my_message(ex$message, level = 3)
# my_message("Attempting to split into several API calls", level = 3)
# # Reduce(rbind, lapply(t_string, gl_translate,
# # format = format,
# # target = target,
# # source = source,
# # model = model))
# } else if(grepl("User Rate Limit Exceeded",
# ex$message)){
# my_message("Characters per 100 seconds rate limit reached, waiting 10 seconds...",
# level = 3)
# Sys.sleep(10)
# ## try again
# gl_translate(t_string,
# format = format,
# target = target,
# source = source,
# model = model)
# } else {
# stop(ex$message, call. = FALSE)
# }
# })
#
me

}

if(interactive()){
library(googleAuthR)
gl_auth("_api-keys/google_api_key.json") -> my_test
library(dplyr)
library(assertthat)
gl_translate_document("./notes/test-doc.pdf") %>% print
}

28 changes: 28 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.

0 comments on commit 904fbea

Please sign in to comment.