| Title: | Translate Text |
|---|---|
| Description: | Provide easy methods to translate pieces of text. Functions send requests to translation services online. |
| Authors: | Tomer Iwan [aut, cre, cph] |
| Maintainer: | Tomer Iwan <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.7.4 |
| Built: | 2026-06-08 10:30:14 UTC |
| Source: | https://github.com/tomeriko96/polyglotr |
This function retrieves the supported language pairs from the Apertium API.
apertium_get_language_pairs(host = "https://apertium.org/apy")apertium_get_language_pairs(host = "https://apertium.org/apy")
host |
Host URL for the Apertium API (default is "https://apertium.org/apy"). |
A list of language pairs. Each element contains sourceLanguage and targetLanguage.
pairs <- apertium_get_language_pairs() head(pairs, 5)pairs <- apertium_get_language_pairs() head(pairs, 5)
Translate text using Apertium
apertium_translate( text, target_language, source_language, host = "https://apertium.org/apy" )apertium_translate( text, target_language, source_language, host = "https://apertium.org/apy" )
text |
Text to translate. Can be a single string or a vector of strings. |
target_language |
Language to translate text to. |
source_language |
Language to translate text from. |
host |
Host URL for the Apertium API (default is "https://apertium.org/apy"). |
Translated text. Returns a vector if input is a vector.
apertium_translate("Hello World", target_language = "es", source_language = "en") apertium_translate("Hola mundo", target_language = "en", source_language = "es") # Translate multiple texts texts <- c("Hello", "Good morning", "Thank you") apertium_translate(texts, target_language = "es", source_language = "en")apertium_translate("Hello World", target_language = "es", source_language = "en") apertium_translate("Hola mundo", target_language = "en", source_language = "es") # Translate multiple texts texts <- c("Hello", "Good morning", "Thank you") apertium_translate(texts, target_language = "es", source_language = "en")
This function translates a file into each target language using the polyglotr package's translate_file function, and saves the translated files.
batch_translate(input_file, source_language, target_languages)batch_translate(input_file, source_language, target_languages)
input_file |
A character string indicating the path to the input file. |
source_language |
A character string indicating the source language. |
target_languages |
A character vector indicating the target languages. |
Nothing is returned.
## Not run: batch_translate("README.md", "nl", c("fr", "es", "de")) ## End(Not run)## Not run: batch_translate("README.md", "nl", c("fr", "es", "de")) ## End(Not run)
This function generates a translation table by translating a list of words into multiple languages.
create_translation_table(words, languages)create_translation_table(words, languages)
words |
A character vector containing the words to be translated. |
languages |
A character vector specifying the target languages for translation. |
A data frame representing the translation table with original words and translations in each language.
## Not run: words <- c("Hello", "Translate", "Table", "Script") languages <- c("es", "fr", "de", "nl") translations <- create_translation_table(words, languages) print(translations) ## End(Not run)## Not run: words <- c("Hello", "Translate", "Table", "Script") languages <- c("es", "fr", "de", "nl") translations <- create_translation_table(words, languages) print(translations) ## End(Not run)
This function generates a transliteration table by transliterating a list of words into multiple languages.
create_transliteration_table(words, languages)create_transliteration_table(words, languages)
words |
A character vector containing the words to be transliterated. |
languages |
A character vector specifying the target languages for transliteration. |
A data frame representing the transliteration table with original words and transliterations in each language.
## Not run: words <- c("Hello world", "Goodbye", "Thank you", "Please") languages <- c("ar", "he", "el", "ru", "fa") transliterations <- create_transliteration_table(words, languages) print(transliterations) ## End(Not run)## Not run: words <- c("Hello world", "Goodbye", "Thank you", "Please") languages <- c("ar", "he", "el", "ru", "fa") transliterations <- create_transliteration_table(words, languages) print(transliterations) ## End(Not run)
This function fetches the supported languages from the Google Cloud Translate documentation page.
google_get_supported_languages()google_get_supported_languages()
A data frame containing the supported languages and their corresponding ISO 639-1 codes.
This function checks if a given language code is in the google_supported_languages dataset.
google_is_valid_language_code(language_code)google_is_valid_language_code(language_code)
language_code |
The language code to check. |
A logical value indicating if the language code is valid.
## Not run: google_is_valid_language_code("en") # TRUE google_is_valid_language_code("fr") # TRUE google_is_valid_language_code("xx") # FALSE ## End(Not run)## Not run: google_is_valid_language_code("en") # TRUE google_is_valid_language_code("fr") # TRUE google_is_valid_language_code("xx") # FALSE ## End(Not run)
This dataset contains the language names and iso codes of languages supported by Google Translate API.
google_supported_languagesgoogle_supported_languages
A data frame with two variables: language_name and iso_code
Google Translate API
Translates input text to a specified language using the Google Translate mobile web interface. Automatically detects and preserves URLs by temporarily replacing them with placeholders. Long texts are split on word boundaries and translated in chunks, then reassembled.
google_translate(text, target_language = "en", source_language = "auto")google_translate(text, target_language = "en", source_language = "auto")
text |
This is the text that you want to translate. Can be a single string or a vector of strings. |
target_language |
This is the language that you want to translate the text into. The default value is "en" (English). |
source_language |
This is the language of the text to be translated. The default value is "auto", which attempts automatic language detection. |
A translated string or vector of translated strings, matching the length of the input.
# Translate a simple sentence google_translate("I love languages", target_language = "es") # Translate a vector of words text_to_translate <- c("the", "quick", "brown") google_translate(text_to_translate, "fr", "en") # Translate text containing a URL google_translate("Visit http://example.com for more info.", target_language = "de")# Translate a simple sentence google_translate("I love languages", target_language = "es") # Translate a vector of words text_to_translate <- c("the", "quick", "brown") google_translate(text_to_translate, "fr", "en") # Translate text containing a URL google_translate("Visit http://example.com for more info.", target_language = "de")
Translates long text from one language to another using Google Translate by splitting the input into manageable chunks if necessary.
google_translate_long_text( text, target_language = "en", source_language = "auto", chunk_size = 1000, preserve_newlines = FALSE )google_translate_long_text( text, target_language = "en", source_language = "auto", chunk_size = 1000, preserve_newlines = FALSE )
text |
A single character string with the text to translate. |
target_language |
The language code to translate the text into (default: "en" for English). |
source_language |
The language code of the input text (default: "auto" for automatic detection). |
chunk_size |
Maximum number of characters per translation request (default: 1000). |
preserve_newlines |
Logical; if TRUE, preserves newlines between chunks in the output. If FALSE (default), newlines are replaced with spaces. |
A single character string containing the translated text.
## Not run: long_text <- paste(rep("This is a long text to translate.", 100), collapse = " ") google_translate_long_text( long_text, target_language = "de", source_language = "en", chunk_size = 500, preserve_newlines = TRUE ) ## End(Not run)## Not run: long_text <- paste(rep("This is a long text to translate.", 100), collapse = " ") google_translate_long_text( long_text, target_language = "de", source_language = "en", chunk_size = 500, preserve_newlines = TRUE ) ## End(Not run)
Transliterate a single word or a sentence to the required language.
google_transliterate(text, language_tag = "el", num = 5)google_transliterate(text, language_tag = "el", num = 5)
text |
The word or sentence to transliterate from Latin/Roman (English) script. |
language_tag |
The target language's ISO639 code. The default value for this argument is "el" for Greek. |
num |
The maximum number of suggestions to fetch. The default value for this argument is 5. |
Character vector of transliterated sentences or larger pieces of text.
## Not run: google_transliterate("Hello world", "fr", 10) google_transliterate("hello", "el", 10) ## End(Not run)## Not run: google_transliterate("Hello world", "fr", 10) google_transliterate("hello", "el", 10) ## End(Not run)
This function detects the language of a given text using the Google Translate API.
language_detect(text)language_detect(text)
text |
The text for which the language needs to be detected. |
A character string representing the detected language.
This function launches the Shiny web application for polyglotr, providing a user-friendly interface to access multiple translation services.
launch_polyglotr_app(launch.browser = TRUE, port = NULL, host = "127.0.0.1")launch_polyglotr_app(launch.browser = TRUE, port = NULL, host = "127.0.0.1")
launch.browser |
Logical. Should the app be launched in the default browser? Default is TRUE. |
port |
The port number for the Shiny app. Default is NULL (auto-assign). |
host |
The host IP address. Default is "127.0.0.1". |
Starts the Shiny application. No return value.
## Not run: # Launch the app in default browser launch_polyglotr_app() # Launch on specific port launch_polyglotr_app(port = 3838) # Launch without opening browser launch_polyglotr_app(launch.browser = FALSE) ## End(Not run)## Not run: # Launch the app in default browser launch_polyglotr_app() # Launch on specific port launch_polyglotr_app(port = 3838) # Launch without opening browser launch_polyglotr_app(launch.browser = FALSE) ## End(Not run)
Get the set of languages currently supported by the Microsoft Translator API
microsoft_supported_languages(scope = NULL)microsoft_supported_languages(scope = NULL)
scope |
(optional) A comma-separated list of names defining the group of languages to return. Allowed group names are: translation, transliteration, and dictionary. If no scope is given, then all groups are returned. |
A list of supported languages for the specified groups.
## Not run: microsoft_supported_languages(scope = "translation,transliteration,dictionary") ## End(Not run)## Not run: microsoft_supported_languages(scope = "translation,transliteration,dictionary") ## End(Not run)
Translate text using mymemory translate
mymemory_translate(text, target_language = "en", source_language = "auto")mymemory_translate(text, target_language = "en", source_language = "auto")
text |
Text to translate. |
target_language |
Language to translate text to. |
source_language |
Language to translate text from |
Translated text.
mymemory_translate("Hello World", target_language = "es", source_language = "en")mymemory_translate("Hello World", target_language = "es", source_language = "en")
Get the list of available dictionaries from PONS API
pons_dictionaries(language = "en")pons_dictionaries(language = "en")
language |
The language of the output (ISO 639-1 - two-letter codes). Supported languages are de, el, en, es, fr, it, pl, pt, ru, sl, tr, zh. |
A list of available dictionaries in the specified language.
## Not run: pons_dictionaries(language = "es") ## End(Not run)## Not run: pons_dictionaries(language = "es") ## End(Not run)
Translate text using PONS
pons_translate(text, target_language = "pt", source_language = "en")pons_translate(text, target_language = "pt", source_language = "en")
text |
This is the text that you want to translate. Can be a single string or a vector of strings. |
target_language |
This is the language that you want to translate the text into. The default value for this argument is "pt" for Portuguese. |
source_language |
This is the language of the text that you want to translate. The default value for this argument is "en" for English. |
Translated text. If the input is a vector, it returns a character vector of translated strings.
## Not run: 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") ## End(Not run)## Not run: 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") ## End(Not run)
Get the QCRI API key from the environment variable
qcri_api_key()qcri_api_key()
The QCRI API key stored in the QCRI_API_KEY environment variable.
This function retrieves the supported domains from the QCRI Multiterm API.
qcri_get_domains(api_key = qcri_api_key())qcri_get_domains(api_key = qcri_api_key())
api_key |
The API key associated with the user account being used. If not provided, the function will attempt to retrieve it from the QCRI_API_KEY environment variable. |
A list with keys:
success: Boolean indicating whether the request succeeded.
domains: Array of supported domains, such as news, tedtalks etc. Only present if success is true.
error: Error message in case success is false.
## Not run: qcri_get_domains(api_key = "YourApiKey") qcri_get_domains() ## End(Not run)## Not run: qcri_get_domains(api_key = "YourApiKey") qcri_get_domains() ## End(Not run)
This function retrieves the supported language pairs from the QCRI Multiterm API.
qcri_get_language_pairs(api_key = qcri_api_key())qcri_get_language_pairs(api_key = qcri_api_key())
api_key |
The API key associated with the user account being used. If not provided, the function will attempt to retrieve it from the QCRI_API_KEY environment variable. You can register for an API key at https://mt.qcri.org/api/register |
Language pairs.
## Not run: qcri_get_language_pairs(api_key = "YourApiKey") qcri_get_language_pairs() ## End(Not run)## Not run: qcri_get_language_pairs(api_key = "YourApiKey") qcri_get_language_pairs() ## End(Not run)
This function translates a text from the source language to the target language using the QCRI Multiterm API.
qcri_translate_text(text, langpair, domain, api_key = qcri_api_key())qcri_translate_text(text, langpair, domain, api_key = qcri_api_key())
text |
The text to be translated. This must be URL encoded. |
langpair |
The source-target language pair, where source is language of the provided text and target is the language into which the text has to be translated. |
domain |
The domain over which the translation is tuned. |
api_key |
The API key associated with the user account being used. If not provided, the function will attempt to retrieve it from the QCRI_API_KEY environment variable. |
Translated text.
## Not run: qcri_translate_text(text = "Hello, world!", langpair = "en-ar", domain = "general", api_key = "YourApiKey") qcri_translate_text(text = "Hello, world!", langpair = "en-ar", domain = "general") ## End(Not run)## Not run: qcri_translate_text(text = "Hello, world!", langpair = "en-ar", domain = "general", api_key = "YourApiKey") qcri_translate_text(text = "Hello, world!", langpair = "en-ar", domain = "general") ## End(Not run)
Detects and replaces protocol-style links (e.g., http://, https://, ftp://) in a given sentence
with unique placeholders like __URL1__, __URL2__, etc. This is useful for preparing text for
translation or further processing while preserving original URLs.
replace_urls_with_placeholders(sentence)replace_urls_with_placeholders(sentence)
sentence |
A character string potentially containing one or more URLs. |
A list with two elements:
The input sentence with URLs replaced by placeholders.
A character vector of the extracted URLs.
Replaces placeholders like __URL1__, __URL2__, etc. in a translated sentence back with their original URLs.
Handles both original and lowercased versions of the placeholder (to account for translation artifacts).
restore_urls_from_placeholders(translated, urls)restore_urls_from_placeholders(translated, urls)
translated |
A character string where placeholders should be replaced with original URLs. |
urls |
A character vector of the original URLs to restore. |
A character string with the placeholders replaced by the corresponding URLs.
Translates the content of a file using Google Translate API.
translate_file( file_path, target_language = "en", source_language = "auto", overwrite = FALSE )translate_file( file_path, target_language = "en", source_language = "auto", overwrite = FALSE )
file_path |
The path to the file to be translated. |
target_language |
The target language to translate the file content to. Default is "en". |
source_language |
The source language of the file content. Default is "auto". |
overwrite |
Logical indicating whether to overwrite the original file with the translated content. Default is FALSE. |
## Not run: translate_file("path/to/file.txt", target_language = "fr", source_language = "en", overwrite = TRUE) ## End(Not run)## Not run: translate_file("path/to/file.txt", target_language = "fr", source_language = "en", overwrite = TRUE) ## End(Not run)
This function takes a string of text as input and translates it to Morse code using the FunTranslations API.
translate_to_morse(text, api_key = NULL)translate_to_morse(text, api_key = NULL)
text |
A character string containing the text to be translated to Morse code. |
api_key |
(optional) Your FunTranslations API key, if you have a paid subscription. |
A list containing the translated Morse code text and other metadata.
This function takes an English text string as input and translates it to Morse code with an audio output using the FunTranslations API.
translate_to_morse_audio(text, api_key = NULL)translate_to_morse_audio(text, api_key = NULL)
text |
A character string containing the English text to be translated. |
api_key |
(optional) Your FunTranslations API key, if you have a paid subscription. |
A list containing the translated Morse code text, the Morse code audio as a base64-encoded string, and other metadata.
This function sends a POST request to the Wikimedia Language ID API with the specified text, parses the JSON response, and returns the detected language.
wikimedia_detect_language(text)wikimedia_detect_language(text)
text |
The text whose language is to be detected. |
The detected language.
{ # Detect the language of a text wikimedia_detect_language("Hallo, wereld") }{ # Detect the language of a text wikimedia_detect_language("Hallo, wereld") }
This function sends a GET request to the Wikipedia API and returns the language names as a dataframe.
wikipedia_get_language_names()wikipedia_get_language_names()
A dataframe of language names.
wikipedia_get_language_names()wikipedia_get_language_names()
This function sends a POST request to the WMCloud translation API with the specified parameters, parses the JSON response, and returns the translated content.
wmcloud_translate( content, target_language = "en", source_language = "en", format = "text", model = "nllb200-600M" )wmcloud_translate( content, target_language = "en", source_language = "en", format = "text", model = "nllb200-600M" )
content |
The content to translate. Can be plain text, a URL (for a webpage), a JSON string, or a Markdown string. |
target_language |
The target language for the translation (default is "en"). |
source_language |
The source language of the content (default is "en"). |
format |
The format of the content ("json", "markdown", "text", "webpage"). |
model |
The model to use for the translation (only "nllb200-600M" is currently known to work). |
The translated content.
## Not run: # Translate plain text wmcloud_translate("rijst", target_language = "es", source_language = "nl", format = "text") # Translate a webpage wmcloud_translate("https://en.m.wikivoyage.org/wiki/Goes", target_language = "es", source_language = "en", format = "webpage") # Translate JSON content wmcloud_translate('{ "id": 1, "title": "Chicken Biryani", "description": "Chicken Biryani is a savory chicken and rice dish", "ingredients": [ "Vegetable oil", "Garlic", "Ginger" ,"Rice"] } ', target_language = "es", source_language = "en", format = "json") # Translate Markdown content wmcloud_translate('# Heading This is a [link to Wikipedia](https://wikipedia.org) ', target_language = "es", source_language = "en", format = "markdown") ## End(Not run)## Not run: # Translate plain text wmcloud_translate("rijst", target_language = "es", source_language = "nl", format = "text") # Translate a webpage wmcloud_translate("https://en.m.wikivoyage.org/wiki/Goes", target_language = "es", source_language = "en", format = "webpage") # Translate JSON content wmcloud_translate('{ "id": 1, "title": "Chicken Biryani", "description": "Chicken Biryani is a savory chicken and rice dish", "ingredients": [ "Vegetable oil", "Garlic", "Ginger" ,"Rice"] } ', target_language = "es", source_language = "en", format = "json") # Translate Markdown content wmcloud_translate('# Heading This is a [link to Wikipedia](https://wikipedia.org) ', target_language = "es", source_language = "en", format = "markdown") ## End(Not run)