Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds combine_translations for combining two translations JSON files as list objects #133

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ vignettes/*.pdf
inst/doc

/docs/

tests/testthat/.DS_Store

tests/.DS_Store
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Generated by roxygen2: do not edit by hand

export(Translator)
export(combine_translations)
export(create_translation_file)
export(init_i18n)
export(translate_with_google_cloud)
export(update_lang)
export(usei18n)
export(write_json_translations)
import(glue)
import(methods)
import(shiny)
Expand Down
48 changes: 47 additions & 1 deletion R/preproc.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,19 @@ save_to_json <- function(key_expressions, output_path = NULL) {
)

if (is.null(output_path)) output_path <- "translation.json"
write_json(list_to_save, output_path)
write_json_translations(list_to_save, output_path)
}

#' Write a list object as a JSON
#'
#' @inheritParams jsonlite::write_json
#' @inheritDotParams jsonlite::toJSON
#'
#' @return None, writes to `path`
#' @export

write_json_translations <- function(x, path, auto_unbox = TRUE, pretty = TRUE, ...) {
jsonlite::write_json(x, path, auto_unbox = auto_unbox, pretty = pretty, ...)
}

#' Save example i18n file to CSV
Expand Down Expand Up @@ -86,6 +98,40 @@ create_translation_file <- function(path, type = "json", handle = "i18n",
)
}

#' Combine two translation json files
#'
#' @param x \code{chr/list} A path to a file or a list via \link[jsonlite]{fromJSON}
#' @param y \code{chr/list} A path to a file or a list via \link[jsonlite]{fromJSON}
#'
#' @return \code{list} combined translation object
#' @export
#'

combine_translations <- function(x, y) {
translations <- lapply(list(x = x, y = y), \(.x) {
if (is.character(.x)) {
if (!file.exists(.x))
stop(paste0(.x, " does not exist. Check the path."))
jsonlite::read_json(.x)
} else
.x
})

if (!do.call(identical, sapply(unname(translations), \(.x) unlist(.x$languages), simplify = FALSE))) {
stop("x & y must have the same languages")
}


combined <- lapply(translations, \(.x) .x$translation)
combined <- append(combined$x, combined$y)
# Remove duplicated entries based on the source string
combined <- combined[!duplicated(sapply(combined, \(.x) .x[[1]]))]
translations$x$translation <- combined
return(translations$x)
}



#' Create translation file addin
#' @keywords internal
create_translation_addin <- function() {
Expand Down
19 changes: 19 additions & 0 deletions man/combine_translations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions man/write_json_translations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/data/translation_additional.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"cultural_date_format": "%d-%m-%Y",
"languages": ["en", "pl"],
"translation": [
{
"en": "Extra translations",
"pl": "Dodatkowe tłumaczenie"
}
]
}
6 changes: 6 additions & 0 deletions tests/testthat/test_preproc.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ test_that("test save_to_csv", {
file.remove("tmp.R")
})

test_that("test combine_translations", {
combined <- combine_translations(jsonlite::read_json("data/translation.json"), jsonlite::read_json("data/translation_additional.json"))
expect_length(combined$translation, 6)
})

test_that("create_translation_addin has proper behavior for rstudio addin", {
temp <- tempfile()
# Mock the behavior of RStudio API calls
Expand Down Expand Up @@ -77,3 +82,4 @@ test_that("create_translation_addin has proper behavior for rstudio addin", {
)

})