Skip to contents

Translate character vectors via the Google Translate API

Usage

gl_translate(
  t_string,
  target = "en",
  format = c("text", "html"),
  source = "",
  model = c("nmt", "base")
)

Arguments

t_string

A character vector of text to detect language for

target

The target language

format

Whether the text is plain or HTML

source

Specify the language to translate from. Will detect it if left default

model

What translation model to use

Value

A tibble of translatedText and detectedSourceLanguage

and text of length equal to the vector of text you passed in.

Details

You can translate a vector of strings, although if too many for one call then it will be broken up into one API call per element. This is the same cost as charging is per character translated, but will take longer.

If translating HTML set the format = "html". Consider removing anything not needed to be translated first, such as JavaScript and CSS scripts. See example on how to do this with rvest

The API limits in three ways: characters per day, characters per 100 seconds, and API requests per 100 seconds. All can be set in the API manager https://console.developers.google.com/apis/api/translate.googleapis.com/quotas

Examples


if (FALSE) {

text <- "to administer medicine to animals is frequently a very difficult matter,
  and yet sometimes it's necessary to do so"

gl_translate(text, target = "ja")

# translate webpages using rvest to process beforehand
library(rvest)
library(googleLanguageR)

# translate webpages

# dr.dk article
my_url <- "http://bit.ly/2yhrmrH"

## in this case the content to translate is in css selector '.wcms-article-content'
read_html(my_url) %>%
  html_node(css = ".wcms-article-content") %>%
  html_text %>%
  gl_translate(format = "html")

}