R/header-language.R

Defines functions content_language

#' Specify content language
#'
#' Implements \href{https://datatracker.ietf.org/doc/html/rfc3282}{RFC 3282}.
#'
#' @noRd
content_language <- function(language, content) {
  if (is.na(language)) language <- FALSE

  if (is.logical(language)) {
    if (language) {
      # Auto-detect language.
      if (requireNamespace("cld3", quietly = TRUE)) {
        # nocov start
        detect_language <- cld3::detect_language
      } else if (requireNamespace("cld2", quietly = TRUE)) {
        detect_language <- cld2::detect_language
      } else {
        stop("Unable to auto-detect language. Install {cld3} or {cld2}.")
      } # nocov end

      language <- detect_language(content)
      log_debug("Auto-detected language: {language}")
    } else {
      # Don't include Content-Language.
      return(NULL)
    }
  } else {
    if (!is.character(language)) {
      stop("Language must either be a string or TRUE/FALSE.") # nocov
    }
  }
  header("Content-Language", paste(language, collapse = ", "))
}

Try the emayili package in your browser

Any scripts or data that you put into this service are public.

emayili documentation built on June 24, 2024, 5:12 p.m.