Skip to content

Commit

Permalink
refactor: extract examine_glossary()
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Dec 7, 2023
1 parent aafa4df commit 08ee200
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
27 changes: 27 additions & 0 deletions R/glossary.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
22 changes: 5 additions & 17 deletions R/translate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 08ee200

Please sign in to comment.