Skip to content

Commit

Permalink
add: pons translation function
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomeriko96 committed Jan 26, 2024
1 parent af2ffc9 commit c22f8f9
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export(linguee_external_sources)
export(linguee_translation_examples)
export(linguee_word_translation)
export(mymemory_translate)
export(pons_translate)
export(translate_file)
export(wikimedia_detect_language)
export(wikipedia_get_language_names)
Expand Down
69 changes: 69 additions & 0 deletions R/pons_translate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#' Translate text using PONS
#'
#' @param text This is the text that you want to translate. Can be a single string or a vector of strings.
#' @param target_language This is the language that you want to translate the text into.
#' The default value for this argument is "pt" for Portuguese.
#' @param source_language This is the language of the text that you want to translate.
#' The default value for this argument is "en" for English.
#'
#' @return Translated text. If the input is a vector, it returns a character vector of translated strings.
#' @export
#'
#' @examples
#' \dontrun{
#' pons_translate("I love languages!", target_language = "pt", source_language = "en")
#' text_to_translate <- c("The", "Greatest", "Language")
#' pons_translate(text_to_translate, "pt", "en")
#' }
pons_translate <- function(text, target_language = "pt", source_language = "en") {
# Create the data payload and headers once
payload <- list(
targetLanguage = target_language,
text = text,
sourceLanguage = source_language
)

headers <- httr::add_headers(
`authority` = "api.pons.com",
`accept` = "*/*",
`cache-control` = "no-cache",
`content-type` = "application/json",
`origin` = "https://en.pons.com",
`pragma` = "no-cache",
`referer` = "https://en.pons.com/",
`sec-ch-ua-mobile` = "?0",
`sec-fetch-dest` = "empty",
`sec-fetch-mode` = "cors",
`sec-fetch-site` = "same-site"
)

# Check if the input is a vector
if (is.vector(text)) {
# If it is, translate each string separately
purrr::map_chr(text, function(single_text) {
# Update the payload with the current single text
payload$text <- single_text
# Make the request
response <- httr::POST(
url = "https://api.pons.com/text-translation-web/v4/translate?locale=en",
body = payload,
encode = "json",
headers = headers
)
# Parse the response
content <- httr::content(response, "parsed")
return(content$text)
})
} else {
# If it's not a vector, translate the single string
response <- httr::POST(
url = "https://api.pons.com/text-translation-web/v4/translate?locale=en",
body = payload,
encode = "json",
headers = headers
)
# Parse the response
content <- httr::content(response, "parsed")
return(content$text)
}
}
5 changes: 5 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ reference:
contents:
- mymemory_translate

- title: Pons Methods
desc: Methods using Pons Translation services
contents:
- pons_translate

- title: Wikimedia Foundation Methods
desc: Methods using Wikimedia Foundation Translation services
contents:
Expand Down
30 changes: 30 additions & 0 deletions man/pons_translate.Rd

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

0 comments on commit c22f8f9

Please sign in to comment.