translate: Translate Texts Using the DeepL API Pro

View source: R/translate.R

translateR Documentation

Translate Texts Using the DeepL API Pro

Description

Translates UTF-8 encoded plain text between supported languages using the DeepL API Pro. A list of supported source and target languages is available at https://developers.deepl.com/docs/getting-started/supported-languages. An authentication key is required to use this service. Charges may apply based on the number of characters translated.

Usage

translate(
  text,
  target_lang = "EN",
  source_lang = NULL,
  split_sentences = TRUE,
  preserve_formatting = FALSE,
  get_detect = FALSE,
  context = NULL,
  model_type = NULL,
  formality = NULL,
  glossary_id = NULL,
  auth_key
)

Arguments

text

Character vector. The text(s) to translate. Each element can contain multiple sentences but must not exceed 30 kB. Only UTF-8 plain text is supported.

target_lang

Character vector. Target language(s) for translation. If length 1, all texts are translated into the same language.

source_lang

Character vector or NULL. Source language(s). If NULL, the language is auto-detected. If of length 1, it is applied to all texts.

split_sentences

Logical. If TRUE (default), DeepL splits input into sentences before translating. Set to FALSE to avoid unintended splits in short texts.

preserve_formatting

Logical. If TRUE, preserves some text formatting (e.g., punctuation and capitalization).

get_detect

Logical. If TRUE, returns a tibble including detected source languages along with translations.

context

Optional. Contextual text to improve translation quality, especially for short or ambiguous inputs. Context is not translated and does not count toward character limits.

model_type

Optional. Specifies the DeepL model to use:

  • "latency_optimized" – Default low-latency model.

  • "quality_optimized" – Higher-quality, higher-latency model (Pro only, limited languages).

  • "prefer_quality_optimized" – Uses quality-optimized when available; otherwise falls back.

formality

Optional. Controls the formality level of the translation (only supported for certain target languages):

  • "default" – Neutral.

  • "more" – More formal.

  • "less" – More informal.

  • "prefer_more" – Prefer formal, fallback to default.

  • "prefer_less" – Prefer informal, fallback to default.

glossary_id

Optional. Glossary ID for translation. Must match the language pair and requires source_lang. Use list_glossaries to retrieve IDs.

auth_key

Character. Your DeepL API authentication key. If missing, the function uses the DEEPL_API_KEY environment variable. You can set it using Sys.setenv(DEEPL_API_KEY = "your_key") or in your .Renviron file.

Details

Register for a DeepL API Pro key at https://www.deepl.com/pro#developer. Only texts passed via the text argument count toward your monthly quota.

Value

If get_detect = FALSE, returns a character vector of translated texts. If get_detect = TRUE, returns a tibble with:

  • translation – Translated text.

  • source_lang – Detected or provided source language.

References

DeepL API Documentation — Translate

See Also

list_glossaries

Examples

## Not run: 
translate("I like to translate texts.", target_lang = "DE")

translate(
  c("I like to translate texts.", "Ich übersetze gerne Texte."), 
  target_lang = "FR"
  )
  
translate("I like to translate texts.", target_lang = c("FR", "DE", "IT"))

translate(
  c("I like to translate texts.", "Ich übersetze gerne Texte."), 
  target_lang = c("FR", "IT")
  )

## End(Not run)


deeplr documentation built on June 8, 2025, 12:47 p.m.