Skip to content

Commit

Permalink
Merge pull request #454 from mrc-ide/translation-script
Browse files Browse the repository at this point in the history
Add script to collect translations, remove some unused keys
  • Loading branch information
r-ash authored Nov 27, 2024
2 parents 31ba516 + 1d53d23 commit 3d7abee
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
2 changes: 0 additions & 2 deletions inst/traduire/fr-translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@
"ART_CHILD": "Enfants sous TARV",
"ART_ADULT_SEX_RATIO": "Rapport de masculinité des adultes TARV",
"ART_CHILD_ADULT_RATIO": "Rapport enfant-adulte ART sous TARV",
"ANC_STATUS": "Statut connu de la CPN",
"ANC_PREVALENCE": "Prévalence CPN",
"ANC_ART_COVERAGE": "Couverture de TARV au CPN",
"ART_NEW_TOTAL": "TARV (Nouveau)",
Expand Down Expand Up @@ -254,7 +253,6 @@
"AWARE_PLHIV_PROP_RATIO": "Proportion de PVVIH conscients rapport",
"INCIDENCE_RATIO": "Rapport d'incidence du VIH",
"WARNING_PROGRAMME_DATA_NOT_EQUAL_TO_SPECTRUM": "Les données infranationales de Naomi ne correspondent pas aux données nationales de Spectrum. Vérifier le tableau de révision des intrants pour: \n{{years}}",
"WARNING_ADDITIONAL": "et {{count}} de plus",
"PJNZ_INVALID_ZIP": "Le zip ne contient aucun fichier PJNZ",
"ANC_PREVALENCE_AGE_MATCHED": "Prévalence CPN appariés par âge",
"ANC_ART_COVERAGE_AGE_MATCHED": "Couverture de TARV au CPN appariés par âge",
Expand Down
2 changes: 0 additions & 2 deletions inst/traduire/pt-translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@
"ART_CHILD": "crianças (<15) em TARV",
"ART_ADULT_SEX_RATIO": "Relação homem com mulher em adultos no TARV",
"ART_CHILD_ADULT_RATIO": "Relação criança-adulto da TARV",
"ANC_STATUS": "Estatuto conhecido do CPN",
"ANC_PREVALENCE": "Prevalência do CPN",
"ANC_ART_COVERAGE": "Cobertura TARV arte em CPN",
"ART_NEW_TOTAL": "TARV (novo)",
Expand Down Expand Up @@ -254,7 +253,6 @@
"AWARE_PLHIV_PROP_RATIO": "Proporção de PVVIH com conhecimento relação",
"INCIDENCE_RATIO": "Incidência de VIH relação",
"WARNING_PROGRAMME_DATA_NOT_EQUAL_TO_SPECTRUM": "Os dados subnacionais da Naomi não são iguais aos dados nacionais da Spectrum. Verificar a tabela de revisão de entradas para: \n{{years}}",
"WARNING_ADDITIONAL": "e {{count}} mais",
"PJNZ_INVALID_ZIP": "O zip não contém ficheiros PJNZ",
"ANC_PREVALENCE_AGE_MATCHED": "Prevalência do CPN idade compatível",
"ANC_ART_COVERAGE_AGE_MATCHED": "Cobertura TARV em CPN idade compatível",
Expand Down
85 changes: 85 additions & 0 deletions scripts/collect_translations.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env Rscript

## Run as ./collect_translations.R 2021-11-17
## Date format is YYYY-MM-DD
args = commandArgs(trailingOnly=TRUE)
if (!is.null(args[1]) && !is.na(args[1])) {
from_date <- args[1]
} else {
from_date <- NULL
}

files <- file.path("inst/traduire", list.files("inst/traduire"))

## Helpers for reading file from git archive
`%||%` <- function(a, b) {
if (is.null(a)) b else a
}
system3 <- function(command, args) {
res <- suppressWarnings(system2(command, args, stdout = TRUE, stderr = TRUE))
code <- attr(res, "status") %||% 0
attr(res, "status") <- NULL
list(success = code == 0,
code = code,
output = res)
}
git_run <- function(args, root = NULL, check = FALSE) {
if (!is.null(root)) {
args <- c("-C", root, args)
}
res <- system3(git, args)
if (check && !res$success) {
stop(sprintf("Error code %d running command:\n%s",
res$code, paste0(" > ", res$output, collapse = "\n")))
}
res
}
git_show <- function(path, date) {
path <- sprintf("HEAD@{%s}:./%s", date, path)
git <- unname(Sys.which("git"))
res <- system3(git, c("show", path))
if (!res$success) {
stop(sprintf("Error code %d running command:\n%s",
res$code, paste0(" > ", res$output, collapse = "\n")))
}
res$output
}

yml <- lapply(files, function(file) {
lang <- strsplit(basename(file), "-")[[1]][1]
current_strings <- jsonlite::read_json(file)
d <- data.frame(names(current_strings),
unname(unlist(current_strings)))
colnames(d) <- c("key", lang)
d
})

if (!is.null(from_date)) {
old_yml <- lapply(files, function(file) {
lang <- strsplit(basename(file), "-")[[1]][1]
content <- git_show(file, from_date)
strings <- jsonlite::fromJSON(content, simplifyVector = FALSE)
d <- data.frame(names(strings),
unname(unlist(strings)))
colnames(d) <- c("key", lang)
d
})

yml <- Map(function(old, current) {
## Keep rows from current not in old or if the value has changed
row.names(current) <- current$key
row.names(old) <- old$key
keys_out <- vapply(row.names(current), function(key) {
!(key %in% row.names(old)) || any(current[key, ] != old[key, ])
}, logical(1))
row.names(current) <- NULL
current[keys_out, ]
}, old_yml, yml)
}

translations <- Reduce(function(x, y) merge(x, y, by = "key", all = TRUE),
yml, accumulate = FALSE)

out <- sprintf("translations-%s.csv", Sys.Date())
message(sprintf("Saving translations to %s", out))
write.csv(translations, out, row.names = FALSE, na = "")

0 comments on commit 3d7abee

Please sign in to comment.