diff --git a/NAMESPACE b/NAMESPACE index 3b825e0..35b58e4 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -19,6 +19,7 @@ export(qcri_get_domains) export(qcri_get_language_pairs) export(qcri_translate_text) export(translate_file) +export(translate_to_morse) export(wikimedia_detect_language) export(wikipedia_get_language_names) export(wmcloud_translate) diff --git a/R/translate_to_morse.R b/R/translate_to_morse.R new file mode 100644 index 0000000..eb5dc77 --- /dev/null +++ b/R/translate_to_morse.R @@ -0,0 +1,43 @@ +#' Translate Text to Morse Code using the FunTranslations API +#' +#' This function takes a string of text as input and translates it to Morse code using the FunTranslations API. +#' +#' @param text A character string containing the text to be translated to Morse code. +#' @param api_key (optional) Your FunTranslations API key, if you have a paid subscription. +#' @return A list containing the translated Morse code text and other metadata. +#' @export +translate_to_morse <- function(text, api_key = NULL) { + url <- "https://api.funtranslations.com/translate/morse.json" + payload <- list(text = text) + + # Add API key if provided + if (!is.null(api_key)) { + headers <- c("X-FunTranslations-Api-Secret" = api_key) + } else { + headers <- NULL + } + + # Make the API request + response <- httr::POST(url, body = payload, httr::add_headers(headers)) + + # Check the response status + if (httr::status_code(response) == 200) { + # Parse the JSON response + result <- jsonlite::fromJSON(httr::content(response, "text")) + + # Extract the relevant information + morse_code <- result$contents$translated + text_translated <- result$contents$text + translation_type <- result$contents$translation + + # Return the results as a list + return(list( + morse_code = morse_code, + text_translated = text_translated, + translation_type = translation_type + )) + } else { + # Handle error cases + stop(paste("Error:", httr::status_code(response), httr::content(response, "text"))) + } +} diff --git a/_pkgdown.yml b/_pkgdown.yml index 8a0f783..14fac2e 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -54,3 +54,8 @@ reference: - wikimedia_detect_language - wikipedia_get_language_names - wmcloud_translate + + - title: Funtranslation Methods + desc: Methods using Funtranslation services for morse code + contents: + - translate_to_morse diff --git a/man/translate_to_morse.Rd b/man/translate_to_morse.Rd new file mode 100644 index 0000000..a84166f --- /dev/null +++ b/man/translate_to_morse.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/translate_to_morse.R +\name{translate_to_morse} +\alias{translate_to_morse} +\title{Translate Text to Morse Code using the FunTranslations API} +\usage{ +translate_to_morse(text, api_key = NULL) +} +\arguments{ +\item{text}{A character string containing the text to be translated to Morse code.} + +\item{api_key}{(optional) Your FunTranslations API key, if you have a paid subscription.} +} +\value{ +A list containing the translated Morse code text and other metadata. +} +\description{ +This function takes a string of text as input and translates it to Morse code using the FunTranslations API. +}