R/lang.R

Defines functions quetzio_txt

Documented in quetzio_txt

quetzio_texts <- new.env()

######     for everyone who wants to add another language support:   ####
# - add new language in 'language_registered'

quetzio_texts$.languages_registered <- c("en", "pl")


# - update 'lang_warning_call' with information about new language

quetzio_texts$.lang_error_call <- "Only supported languages for now are: 'en' and 'pl'"

# add new labels in 'quetzio_texts' environment
# remember to escape any non-standard characters using /uXXXX with their unicode

quetzio_texts$en = list(
  # submit button labels
  submit_enabled = "Submit",
  submit_disabled = "Can't submit",
  submit_done = "Submitted!",
  submit_error = "Error!",
  # modal texts
  modal_title = "Answers can't be saved",
  modal_content = "Some mandatory inputs aren't filled and/or filled inputs aren't valid:",
  modal_button = "Close the window"
)

quetzio_texts$pl = list(
  # submit button labels
  submit_enabled = "Prze\u015blij",
  submit_disabled = "Nie mo\u017cna przes\u0142a\u0107",
  submit_done = "Przes\u0142ano!",
  submit_error = "B\u0142\u0105d!",
  # modal texts
  modal_title = "Odpowiedzi nie mog\u0105 by\u0107 zapisane",
  modal_content = "Niekt\u00f3re pola wymagane nie s\u0105 wype\u0142nione i/lub pola s\u0105 nieprawid\u0142owo wype\u0142nione:",
  modal_button = "Zamknij okno"
)

# also - modify documentation in quetzio_server argument `lang`

#' Get texts for given language and supports getting them in context of
#' quetzio_server
#'
#' @param lang character to identify the language
#' @param x character to identify the txt to get. If NULL, all labels are
#' recovered
#' @param private private element of R6 class in which context it is used to get
#' the specific element. Used only internally. Recommended to leave at NULL default.
#'
#' @details
#' 'quetzio_txt' outside of internal usage should be used only for getting the
#' structure of all texts generated by 'quetzio_server'. You then call it in
#' your console without specifying the 'private' argument.
#'
#' To customize texts used by questionnaire module, provide within its
#' init call named list to the 'custom_txts' argument. Its elements should
#' be named in the same way as the default text you are willing to replace: so
#' to replace only the Submit button label for the state when the questionnaire
#' is valid for submission and not, the list should look like that:
#'
#' `custom_txts = list(submit_enabled = "Do it!", submit_disabled = "Can't do!)`
#'
#' @seealso quetzio_server
#' @example inst/examples/quetzio_custom_txts.R
#' @export
#'

quetzio_txt <- function(
  lang,
  x = NULL,
  private = NULL
) {

  # check if the lang is in registered languages
  if (!lang %in% quetzio_texts$.languages_registered) {
    stop(quetzio_texts$.lang_error_call, call.= F)   # nocov
  }

  # if x is null, print all labels in specified language
  if (is.null(x)) {
    return(quetzio_texts[[as.character(lang)]])
    # if private is null, print specified label to the console
  } else if (is.null(private)){
    return(quetzio_texts[[as.character(lang)]][[as.character(x)]])
    # internal usage inside 'quetzio_server'
  } else {

    texts <- quetzio_texts[[as.character(lang)]]

    # recover and change 'texts' if any 'custom_txts' are specified
    if (!is.null(private$custom_txts)) {
      # only if value is provided
      custom_txt <- private$custom_txts[[as.character(x)]]

      if (!is.null(custom_txt)) {
        texts[[as.character(x)]] <- as.character(custom_txt)
      }
    }
    return(texts[[x]])
  }

}
StatisMike/shiny.survey documentation built on April 4, 2022, 5:03 a.m.