2018-11-05 13:20:32 +01:00
# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
2019-01-02 23:24:07 +01:00
# SOURCE #
# https://gitlab.com/msberends/AMR #
2018-11-05 13:20:32 +01:00
# #
# LICENCE #
2019-01-02 23:24:07 +01:00
# (c) 2019 Berends MS (m.s.berends@umcg.nl), Luz CF (c.f.luz@umcg.nl) #
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. #
# #
# This R package was created for academic research and was publicly #
# released in the hope that it will be useful, but it comes WITHOUT #
# ANY WARRANTY OR LIABILITY. #
2019-04-05 18:47:39 +02:00
# Visit our website for more info: https://msberends.gitlab.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
#'
2019-08-11 19:07:26 +02:00
#' For language-dependent output of AMR functions, like \code{\link{mo_name}}, \code{\link{mo_type}} and \code{\link{ab_name}}.
2019-06-01 20:40:49 +02: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: \url{https://gitlab.com/msberends/AMR/blob/master/data-raw/translations.tsv}.
2019-05-10 16:44:59 +02:00
#'
2019-08-11 19:07:26 +02:00
#' Currently supported languages can be found if running: \code{unique(AMR:::translations_file$lang)}.
#'
2019-06-01 20:40:49 +02:00
#' Please suggest your own translations \href{https://gitlab.com/msberends/AMR/issues/new?issue[title]=Translation\%20suggestion}{by creating a new issue on our repository}.
2019-05-10 16:44:59 +02:00
#'
2019-06-01 20:40:49 +02:00
#' This file will be read by all functions where a translated output can be desired, like all \code{\link{mo_property}} functions (\code{\link{mo_fullname}}, \code{\link{mo_type}}, etc.).
2019-05-10 16:44:59 +02:00
#'
2019-08-11 19:07:26 +02:00
#' The system language will be used at default, if that language is supported. The system language can be overwritten with \code{\link{getOption}("AMR_locale")}.
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
#' # The 'language' parameter of below functions
#' # 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 ( ) {
2019-09-12 15:08:53 +02:00
if ( ! is.null ( getOption ( " AMR_locale" , default = NULL ) ) ) {
2019-05-10 16:44:59 +02:00
return ( getOption ( " AMR_locale" ) )
2018-11-05 13:20:32 +01:00
}
2019-05-10 16:44:59 +02:00
2018-11-05 13:20:32 +01:00
lang <- Sys.getlocale ( " LC_COLLATE" )
2019-08-04 10:48:41 +02:00
# Check the locale settings for a start with one of these languages:
# grepl() with ignore.case = FALSE is faster than %like%
2019-09-12 15:08:53 +02:00
2018-11-12 15:07:43 +01:00
if ( grepl ( " ^(English|en_|EN_)" , lang , ignore.case = FALSE ) ) {
# as first option to optimise speed
2018-11-05 13:20:32 +01:00
" en"
2018-11-12 15:07:43 +01:00
} else if ( grepl ( " ^(German|Deutsch|de_|DE_)" , lang , ignore.case = FALSE ) ) {
2018-11-05 13:20:32 +01:00
" de"
2018-11-12 15:07:43 +01:00
} else if ( grepl ( " ^(Dutch|Nederlands|nl_|NL_)" , lang , ignore.case = FALSE ) ) {
2018-11-05 13:20:32 +01:00
" nl"
2018-11-12 15:07:43 +01:00
} else if ( grepl ( " ^(Spanish|Espa.ol|es_|ES_)" , lang , ignore.case = FALSE ) ) {
2018-11-05 13:20:32 +01:00
" es"
2018-11-12 15:07:43 +01:00
} else if ( grepl ( " ^(Italian|Italiano|it_|IT_)" , lang , ignore.case = FALSE ) ) {
2018-11-05 13:20:32 +01:00
" it"
2018-11-12 15:07:43 +01:00
} else if ( grepl ( " ^(French|Fran.ais|fr_|FR_)" , lang , ignore.case = FALSE ) ) {
2018-11-05 13:20:32 +01:00
" fr"
2018-11-12 15:07:43 +01:00
} else if ( grepl ( " ^(Portuguese|Portugu.s|pt_|PT_)" , lang , ignore.case = FALSE ) ) {
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
#' @importFrom dplyr %>% filter
translate_AMR <- function ( from , language = get_locale ( ) , only_unknown = FALSE ) {
if ( is.null ( language ) ) {
return ( from )
}
if ( language %in% c ( " en" , " " , NA ) ) {
return ( from )
}
df_trans <- translations_file # internal data file
if ( ! language %in% df_trans $ lang ) {
stop ( " Unsupported language: '" , language , " ' - use one of: " ,
paste0 ( " '" , sort ( unique ( df_trans $ lang ) ) , " '" , collapse = " , " ) ,
call. = FALSE )
}
df_trans <- df_trans %>% filter ( lang == language )
if ( only_unknown == TRUE ) {
df_trans <- df_trans %>% filter ( pattern %like% " unknown" )
}
# 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
any_form_in_patterns <- tryCatch ( any ( from %like% paste0 ( " (" , paste ( df_trans $ pattern , collapse = " |" ) , " )" ) ) ,
error = function ( e ) {
warning ( " Translation not possible. Please open an issue on GitLab (https://gitlab.com/msberends/AMR/issues) or GitHub (https://github.com/msberends/AMR/issues)." , call. = FALSE )
return ( FALSE )
} )
if ( NROW ( df_trans ) == 0 | ! any_form_in_patterns ) {
return ( from )
}
for ( i in 1 : nrow ( df_trans ) ) {
from <- gsub ( x = from ,
pattern = df_trans $ pattern [i ] ,
replacement = df_trans $ replacement [i ] ,
fixed = df_trans $ fixed [i ] ,
ignore.case = df_trans $ ignore.case [i ] )
}
# force UTF-8 for diacritics
base :: enc2utf8 ( from )
}