gl_translate: Translate the language of text within a request

Description Usage Arguments Details Value See Also Examples

View source: R/translate.R

Description

Translate character vectors via the Google Translate API

Usage

1
2
3
4
5
6
7
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

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

Value

A tibble of translatedText and detectedSourceLanguage and text of length equal to the vector of text you passed in.

See Also

https://cloud.google.com/translate/docs/reference/translate

Other translations: gl_translate_detect(), gl_translate_languages()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## Not run: 

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")


## End(Not run)

googleLanguageR documentation built on April 19, 2020, 3:58 p.m.