From 08ee200575e2cd8ef4a4d0193441f0147d26be61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Thu, 7 Dec 2023 10:18:14 +0100 Subject: [PATCH] refactor: extract examine_glossary() --- R/glossary.R | 27 +++++++++++++++++++++++++++ R/translate.R | 22 +++++----------------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/R/glossary.R b/R/glossary.R index 3f50155..98bb824 100644 --- a/R/glossary.R +++ b/R/glossary.R @@ -124,3 +124,30 @@ get_glossary_id <- function(glossary_name, glossary <- glossaries[glossary_names == glossary_name] return(glossary[[1]][["glossary_id"]]) } + +examine_glossary <- function(glossary_name, + source_lang_code, + target_lang_code, + call = rlang::caller_env()) { + if (is.null(glossary_name)) { + return(NULL) + } + + glossary_id <- get_glossary_id( + glossary_name, + source_lang = source_lang_code, + target_lang = target_lang_code + ) + if (is.null(glossary_id)) { + cli::cli_abort( + c( + "Can't find {.field glossary_name} {.val {glossary_name}}.", + i = "Check the spelling, or create it with {.fun upsert_glossary}." + ), + call = call + ) + } + + glossary_id + +} diff --git a/R/translate.R b/R/translate.R index 2a94146..55ac5cf 100644 --- a/R/translate.R +++ b/R/translate.R @@ -50,23 +50,11 @@ deepl_translate <- function(path, source_lang_code <- examine_source_lang(source_lang) target_lang_code <- examine_target_lang(target_lang) - if (!is.null(glossary_name)) { - glossary_id <- get_glossary_id( - glossary_name, - source_lang = source_lang_code, - target_lang = target_lang_code - ) - if (is.null(glossary_id)) { - cli::cli_abort( - c( - "Can't find {.field glossary_name} {.val {glossary_name}}.", - i = "Check the spelling, or create it with {.fun upsert_glossary}." - ) - ) - } - } else { - glossary_id <- NULL - } + glossary_id <- examine_glossary( + glossary_name = glossary_name, + source_lang_code = source_lang_code, + target_lang_code = target_lang_code + ) formality <- rlang::arg_match( formality,