Skip to content

Commit

Permalink
Adds localization to cite_r().
Browse files Browse the repository at this point in the history
  • Loading branch information
crsh committed Oct 11, 2024
1 parent c045234 commit cfcf51e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ example/example.tex
inst/doc

.vscode
.luarc.json
32 changes: 21 additions & 11 deletions R/cite_r.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,28 @@
#' @examples cite_r()
#' @export

cite_r <- function(file = NULL, prefix = "R-", footnote = FALSE, pkgs = NULL, omit = TRUE, ...) {
cite_r <- function(file = NULL, prefix = "R-", footnote = FALSE, pkgs = NULL, omit = TRUE, lang = NULL, ...) {
if(!is.null(file)) validate(file, check_class = "character", check_length = 1)
validate(prefix, check_class = "character", check_length = 1)
validate(footnote, check_class = "logical", check_length = 1)
if(!is.null(pkgs)) validate(pkgs, check_class = "character")

if(is.null(lang)) {
apa_terms <- getOption("papaja.terms")
} else {
validate(lang, check_class = "character", check_length = 1)
lang <- parse_bcp47(lang)
apa_terms <- localize(lang)
}

ellipsis <- list(...)
if(!is.null(ellipsis$withhold)) {
omit <- ellipsis$withhold
}
validate(omit, check_class = "logical", check_length = 1)

r_version <- as.character(utils::packageVersion("base"))
cite_just_r <- paste0("R [Version ", r_version, "; @", prefix, "base]")
cite_just_r <- paste0("R [", apa_terms$version, " ", r_version, "; @", prefix, "base]")

if(is.null(file) || !utils::file_test("-f", file)) { # Print R-reference if there is no .bib-file
if(!is.null(file)) warning("File ", file, " not found. Cannot cite R-packages. If knitting again does not solve the problem, please check file path.")
Expand Down Expand Up @@ -100,7 +108,7 @@ cite_r <- function(file = NULL, prefix = "R-", footnote = FALSE, pkgs = NULL, om
pkg_names <- names(pkg_citations)
pkg_names <- unique(gsub("\\_\\D", "", pkg_names))
pkg_names <- gsub("survival-book", "survival", pkg_names)
pkg_versions <- sapply(pkg_names, function(x) if(package_available(x)) paste0("Version ", as.character(utils::packageVersion(x)), "\\; ") else "")
pkg_versions <- sapply(pkg_names, function(x) if(package_available(x)) paste0(apa_terms$version, " ", as.character(utils::packageVersion(x)), "\\; ") else "")
pkg_keys <- sapply(pkg_names, function(x){
keys <- pkg_citations[grepl(x, names(pkg_citations))]
paste0("@", keys, collapse = "; ")
Expand All @@ -113,23 +121,25 @@ cite_r <- function(file = NULL, prefix = "R-", footnote = FALSE, pkgs = NULL, om

if(length(pkg_texts) > 1) {
pkg_info <- paste(pkg_texts[1:(length(pkg_texts) - 1)], collapse = ", ")
pkg_info <- paste0(pkg_info, ", and ", utils::tail(pkg_texts, 1))
pkg_info <- paste0(pkg_info, " ", apa_terms$and, " ", utils::tail(pkg_texts, 1))
} else {
pkg_info <- pkg_texts
}

complete_r_citaiton <- paste0("R [Version ", r_version, "\\; @", r_citation, "]")
complete_r_citation <- paste0("R [", apa_terms$version, " ", r_version, "\\; @", r_citation, "]")

if(footnote) {
res <- list()
res$r <- paste0(complete_r_citaiton, "[^papaja_pkg_citations]")
res$r <- paste0(complete_r_citation, "[^papaja_pkg_citations]")

res$pkgs <- paste0("\n\n[^papaja_pkg_citations]: We, furthermore, used the R-packages ", pkg_info, ".\n\n")
res$pkgs <- paste0("\n\n[^papaja_pkg_citations]: ", apa_terms$cite_r_footnote, " ", pkg_info, ".\n\n")
} else {
res <- paste0(
complete_r_citaiton, " and the R-package"
, if(length(pkg_texts) > 1) "s", " " , pkg_info
)
if(length(pkg_texts) > 1) {
pkg_phrase <- apa_terms$cite_r_packages_pl
} else {
pkg_phrase <- apa_terms$cite_r_packages_s
}
res <- paste0(complete_r_citation, " ", pkg_phrase, " " , pkg_info)
}

res
Expand Down
15 changes: 15 additions & 0 deletions R/lookup_tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ localize <- function(x) {
, note = "Note"
, correspondence = "Correspondence concerning this article should be addressed to "
, email = "E-mail"
, version = "Version"
, and = "and"
, cite_r_packages_s = "and the R-package"
, cite_r_packages_pl = "and the R-packages"
, cite_r_footnote = "We, furthermore, used the R-packages"
)
, german = list(
author_note = "Anmerkung des Autors"
Expand All @@ -333,6 +338,11 @@ localize <- function(x) {
, note = "Anmerkung"
, correspondence = "Schriftverkehr diesen Artikel betreffend sollte adressiert sein an "
, email = "E-Mail"
, version = "Version"
, and = "und"
, cite_r_packages_s = "und das R-Paket"
, cite_r_packages_pl = "und die R-Pakete"
, cite_r_footnote = "Wir verwendeten zudem die R-Pakete"
)
, dutch = list(
author_note = "Over de auteur"
Expand All @@ -344,6 +354,11 @@ localize <- function(x) {
, note = "Opmerking"
, correspondence = "Correspondentie betreffende dit artikel wordt geadresseerd aan "
, email = "E-mail"
, verion = "Versie"
, and = "en"
, cite_r_packages_s = "en het R-pakket"
, cite_r_packages_pl = "en de R-pakketten"
, cite_r_footnote = "We gebruikten bovendien de R-pakketten"
)
)
}
12 changes: 7 additions & 5 deletions R/onload.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
.onLoad <- function(libname, pkgname) { # nocov start
lang <- if(length(knitr::opts_knit$get("rmarkdown.pandoc.to")) > 0 && !is.null(rmarkdown::metadata$lang)) {
rmarkdown::metadata$lang
} else "english"
if(length(knitr::opts_knit$get("rmarkdown.pandoc.to")) > 0 && !is.null(rmarkdown::metadata$lang)) {
lang <- parse_bcp47(rmarkdown::metadata$lang)
} else {
lang <- "english"
}

op <- options()
op_papaja <- list(
papaja.language = lang
, papaja.terms = localize(lang)
# papaja.language = lang
papaja.terms = localize(lang)
, papaja.na_string = "NA"
, papaja.plot_colors = "greyscale"
, papaja.mse = !package_available("effectsize")
Expand Down
20 changes: 20 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,23 @@ no_method <- function(x) {
, call. = FALSE
)
}

#' Simple language names from BCP 47 tags
#'
#' Internal function to translate BCP 47 tags to simple language names.

#' @param x Character. BCP 47 tag.
#' @return Character. Simple language name.
#' @keywords internal

parse_bcp47 <- function(x) {
lang <- gsub("-[A-Z]{2}$", "", x)

switch(
lang
, en = "english"
, de = "german"
, nl = "dutch"
, "english"
)
}

0 comments on commit cfcf51e

Please sign in to comment.