2018-11-05 13:20:32 +01:00
# ==================================================================== #
# TITLE #
2020-10-08 11:16:03 +02:00
# Antimicrobial Resistance (AMR) Analysis for R #
2018-11-05 13:20:32 +01:00
# #
2019-01-02 23:24:07 +01:00
# SOURCE #
2020-07-08 14:48:06 +02:00
# https://github.com/msberends/AMR #
2018-11-05 13:20:32 +01:00
# #
# LICENCE #
2020-01-05 17:22:09 +01:00
# (c) 2018-2020 Berends MS, Luz CF et al. #
2020-10-08 11:16:03 +02:00
# Developed at the University of Groningen, the Netherlands, in #
# collaboration with non-profit organisations Certe Medical #
# Diagnostics & Advice, and University Medical Center Groningen. #
2018-11-05 13:20:32 +01:00
# #
2019-01-02 23:24:07 +01:00
# This R package is free software; you can freely use and distribute #
# it for both personal and commercial purposes under the terms of the #
# GNU General Public License version 2.0 (GNU GPL-2), as published by #
# the Free Software Foundation. #
2020-01-05 17:22:09 +01:00
# We created this package for both routine data analysis and academic #
# research and it was publicly released in the hope that it will be #
# useful, but it comes WITHOUT ANY WARRANTY OR LIABILITY. #
2020-10-08 11:16:03 +02:00
# #
# Visit our website for the full manual and a complete tutorial about #
# how to conduct AMR analysis: https://msberends.github.io/AMR/ #
2018-11-05 13:20:32 +01:00
# ==================================================================== #
2019-05-10 16:44:59 +02:00
#' Translate strings from AMR package
2018-11-05 13:20:32 +01:00
#'
2020-05-16 13:05:47 +02:00
#' For language-dependent output of AMR functions, like [mo_name()], [mo_gramstain()], [mo_type()] and [ab_name()].
2020-01-05 17:22:09 +01:00
#' @inheritSection lifecycle Stable lifecycle
2020-12-17 16:22:25 +01:00
#' @details Strings will be translated to foreign languages if they are defined in a local translation file. Additions to this file can be suggested at our repository. The file can be found here: <https://github.com/msberends/AMR/blob/master/data-raw/translations.tsv>. This file will be read by all functions where a translated output can be desired, like all [`mo_*`][mo_property()] functions (such as [mo_name()], [mo_gramstain()], [mo_type()], etc.) and [`ab_*`][ab_property()] functions (such as [ab_name()], [ab_group()], etc.).
2019-05-10 16:44:59 +02:00
#'
2020-09-03 12:31:48 +02:00
#' Currently supported languages are: `r paste(sort(gsub(";.*", "", ISOcodes::ISO_639_2[which(ISOcodes::ISO_639_2$Alpha_2 %in% LANGUAGES_SUPPORTED), "Name"])), collapse = ", ")`. Please note that currently not all these languages have translations available for all antimicrobial agents and colloquial microorganism names.
2019-08-11 19:07:26 +02:00
#'
2020-07-08 14:48:06 +02:00
#' Please suggest your own translations [by creating a new issue on our repository](https://github.com/msberends/AMR/issues/new?title=Translations).
2019-05-10 16:44:59 +02:00
#'
2020-09-14 12:21:23 +02:00
#' ## Changing the default language
2020-09-14 19:41:48 +02:00
#' The system language will be used at default (as returned by `Sys.getenv("LANG")` or, if `LANG` is not set, [Sys.getlocale()]), if that language is supported. But the language to be used can be overwritten in two ways and will be checked in this order:
2020-09-14 12:21:23 +02:00
#'
#' 1. Setting the R option `AMR_locale`, e.g. by running `options(AMR_locale = "de")`
#' 2. Setting the system variable `LANGUAGE` or `LANG`, e.g. by adding `LANGUAGE="de_DE.utf8"` to your `.Renviron` file in your home directory
#'
#' So if the R option `AMR_locale` is set, the system variables `LANGUAGE` and `LANG` will be ignored.
2019-01-02 23:24:07 +01:00
#' @inheritSection AMR Read more on our website!
2019-05-10 16:44:59 +02:00
#' @rdname translate
#' @name translate
2018-11-05 13:20:32 +01:00
#' @export
2019-05-10 16:44:59 +02:00
#' @examples
2020-12-22 00:51:17 +01:00
#' # The 'language' argument of below functions
2019-05-10 16:44:59 +02:00
#' # will be set automatically to your system language
#' # with get_locale()
#'
#' # English
2019-08-11 19:07:26 +02:00
#' mo_name("CoNS", language = "en")
2019-05-10 16:44:59 +02:00
#' #> "Coagulase-negative Staphylococcus (CoNS)"
#'
#' # German
2019-08-11 19:07:26 +02:00
#' mo_name("CoNS", language = "de")
2019-05-10 16:44:59 +02:00
#' #> "Koagulase-negative Staphylococcus (KNS)"
#'
#' # Dutch
2019-08-11 19:07:26 +02:00
#' mo_name("CoNS", language = "nl")
2019-05-10 16:44:59 +02:00
#' #> "Coagulase-negatieve Staphylococcus (CNS)"
#'
#' # Spanish
2019-08-11 19:07:26 +02:00
#' mo_name("CoNS", language = "es")
2019-05-10 16:44:59 +02:00
#' #> "Staphylococcus coagulasa negativo (SCN)"
#'
#' # Italian
2019-08-11 19:07:26 +02:00
#' mo_name("CoNS", language = "it")
2019-05-10 16:44:59 +02:00
#' #> "Staphylococcus negativo coagulasi (CoNS)"
#'
#' # Portuguese
2019-08-11 19:07:26 +02:00
#' mo_name("CoNS", language = "pt")
2019-05-10 16:44:59 +02:00
#' #> "Staphylococcus coagulase negativo (CoNS)"
2018-11-05 13:20:32 +01:00
get_locale <- function ( ) {
2020-10-19 17:09:19 +02:00
# AMR versions 1.3.0 and prior used the environmental variable:
2020-09-12 08:49:01 +02:00
if ( ! identical ( " " , Sys.getenv ( " AMR_locale" ) ) ) {
options ( AMR_locale = Sys.getenv ( " AMR_locale" ) )
}
2019-09-12 15:08:53 +02:00
if ( ! is.null ( getOption ( " AMR_locale" , default = NULL ) ) ) {
2020-09-12 08:49:01 +02:00
lang <- getOption ( " AMR_locale" )
if ( lang %in% LANGUAGES_SUPPORTED ) {
return ( lang )
2020-09-03 12:31:48 +02:00
} else {
2020-09-14 12:21:23 +02:00
stop_ ( " unsupported language set as option 'AMR_locale': '" , lang , " ' - use one of: " ,
2020-09-12 08:49:01 +02:00
paste0 ( " '" , LANGUAGES_SUPPORTED , " '" , collapse = " , " ) )
2020-09-03 12:31:48 +02:00
}
2020-09-14 12:21:23 +02:00
} else {
# we now support the LANGUAGE system variable - return it if set
if ( ! identical ( " " , Sys.getenv ( " LANGUAGE" ) ) ) {
return ( coerce_language_setting ( Sys.getenv ( " LANGUAGE" ) ) )
}
if ( ! identical ( " " , Sys.getenv ( " LANG" ) ) ) {
return ( coerce_language_setting ( Sys.getenv ( " LANG" ) ) )
}
2018-11-05 13:20:32 +01:00
}
2020-05-16 13:05:47 +02:00
2020-12-17 16:22:25 +01:00
coerce_language_setting ( Sys.getlocale ( " LC_COLLATE" ) )
2020-09-14 12:21:23 +02:00
}
coerce_language_setting <- function ( lang ) {
2019-08-04 10:48:41 +02:00
# grepl() with ignore.case = FALSE is faster than %like%
2020-10-19 17:09:19 +02:00
if ( grepl ( " ^(English|en_|EN_)" , lang , ignore.case = FALSE , perl = TRUE ) ) {
2018-11-12 15:07:43 +01:00
# as first option to optimise speed
2018-11-05 13:20:32 +01:00
" en"
2020-10-19 17:09:19 +02:00
} else if ( grepl ( " ^(German|Deutsch|de_|DE_)" , lang , ignore.case = FALSE , perl = TRUE ) ) {
2018-11-05 13:20:32 +01:00
" de"
2020-10-19 17:09:19 +02:00
} else if ( grepl ( " ^(Dutch|Nederlands|nl_|NL_)" , lang , ignore.case = FALSE , perl = TRUE ) ) {
2018-11-05 13:20:32 +01:00
" nl"
2020-10-19 17:09:19 +02:00
} else if ( grepl ( " ^(Spanish|Espa.+ol|es_|ES_)" , lang , ignore.case = FALSE , perl = TRUE ) ) {
2018-11-05 13:20:32 +01:00
" es"
2020-10-19 17:09:19 +02:00
} else if ( grepl ( " ^(Italian|Italiano|it_|IT_)" , lang , ignore.case = FALSE , perl = TRUE ) ) {
2018-11-05 13:20:32 +01:00
" it"
2020-10-19 17:09:19 +02:00
} else if ( grepl ( " ^(French|Fran.+ais|fr_|FR_)" , lang , ignore.case = FALSE , perl = TRUE ) ) {
2018-11-05 13:20:32 +01:00
" fr"
2020-10-19 17:09:19 +02:00
} else if ( grepl ( " ^(Portuguese|Portugu.+s|pt_|PT_)" , lang , ignore.case = FALSE , perl = TRUE ) ) {
2018-11-05 13:20:32 +01:00
" pt"
2018-11-12 15:07:43 +01:00
} else {
2018-11-15 12:42:35 +01:00
# other language -> set to English
2018-11-12 15:07:43 +01:00
" en"
2018-11-05 13:20:32 +01:00
}
}
2019-09-12 15:08:53 +02:00
# translate strings based on inst/translations.tsv
translate_AMR <- function ( from , language = get_locale ( ) , only_unknown = FALSE ) {
2020-05-16 13:05:47 +02:00
2019-09-12 15:08:53 +02:00
if ( is.null ( language ) ) {
return ( from )
}
if ( language %in% c ( " en" , " " , NA ) ) {
return ( from )
}
df_trans <- translations_file # internal data file
2020-09-03 12:31:48 +02:00
from.bak <- from
from_unique <- unique ( from )
from_unique_translated <- from_unique
2019-09-12 15:08:53 +02:00
2020-09-03 12:31:48 +02:00
stop_ifnot ( language %in% LANGUAGES_SUPPORTED ,
2020-06-22 11:18:40 +02:00
" unsupported language: '" , language , " ' - use one of: " ,
2020-09-03 12:31:48 +02:00
paste0 ( " '" , LANGUAGES_SUPPORTED , " '" , collapse = " , " ) ,
2020-06-22 11:18:40 +02:00
call = FALSE )
2019-09-12 15:08:53 +02:00
2020-07-08 14:48:06 +02:00
df_trans <- subset ( df_trans , lang == language )
2019-09-12 15:08:53 +02:00
if ( only_unknown == TRUE ) {
2020-07-08 14:48:06 +02:00
df_trans <- subset ( df_trans , pattern %like% " unknown" )
2019-09-12 15:08:53 +02:00
}
# default case sensitive if value if 'ignore.case' is missing:
df_trans $ ignore.case [is.na ( df_trans $ ignore.case ) ] <- FALSE
# default not using regular expressions (fixed = TRUE) if 'fixed' is missing:
df_trans $ fixed [is.na ( df_trans $ fixed ) ] <- TRUE
# check if text to look for is in one of the patterns
2020-09-03 12:31:48 +02:00
any_form_in_patterns <- tryCatch ( any ( from_unique %like% paste0 ( " (" , paste ( df_trans $ pattern , collapse = " |" ) , " )" ) ) ,
2019-09-12 15:08:53 +02:00
error = function ( e ) {
2020-11-10 16:35:56 +01:00
warning_ ( " Translation not possible. Please open an issue on GitHub (https://github.com/msberends/AMR/issues)." , call = FALSE )
2019-09-12 15:08:53 +02:00
return ( FALSE )
} )
if ( NROW ( df_trans ) == 0 | ! any_form_in_patterns ) {
return ( from )
}
2020-09-03 12:31:48 +02:00
lapply ( seq_len ( nrow ( df_trans ) ) ,
function ( i ) from_unique_translated <<- gsub ( pattern = df_trans $ pattern [i ] ,
replacement = df_trans $ replacement [i ] ,
x = from_unique_translated ,
ignore.case = df_trans $ ignore.case [i ] ,
fixed = df_trans $ fixed [i ] ) )
2019-09-12 15:08:53 +02:00
# force UTF-8 for diacritics
2020-09-03 12:31:48 +02:00
from_unique_translated <- enc2utf8 ( from_unique_translated )
# a kind of left join to get all results back
from_unique_translated [match ( from.bak , from_unique ) ]
2019-09-12 15:08:53 +02:00
}