Skip to content

Commit

Permalink
Allow Non UTF-8 source
Browse files Browse the repository at this point in the history
- Add unit test to check_translation_csv()
- closes #61
  • Loading branch information
statnmap committed Nov 23, 2021
1 parent f667107 commit 5248856
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 30 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import(shiny)
import(tmap)
importFrom(DBI,dbConnect)
importFrom(RPostgres,Postgres)
importFrom(dplyr,mutate_all)
importFrom(golem,activate_js)
importFrom(golem,add_resource_path)
importFrom(golem,bundle_resources)
Expand Down
70 changes: 44 additions & 26 deletions R/utils_csv_check.R
Original file line number Diff line number Diff line change
@@ -1,32 +1,50 @@
#' csv_check
#'
#'
#' @description Check the CSV written by hand
#'
#' @param path path to file to check
#' @param source_encoding Encoding of the source file
#' @param new_path Where to save the modified file
#' @return The return value, if any, from executing the utility.
#'
#' @importFrom dplyr mutate_all
#' @export
check_translation_csv <- function(path) {
if (!file.exists(path)) {
stop("File doesn't exists")
}
if (tools::file_ext(path) != "csv") {
stop("File should be a csv file")
}
fls <- read.csv(path)
if (
names(fls)[1] != "entry"
) {
stop("The first column should be named 'entry'")
}
if (
!"en" %in% names(fls)
) {
stop("Their is no 'en' column")
}
if (
!"fr" %in% names(fls)
) {
stop("Their is no 'fr' column")
}
cli::cli_alert_success("CSV seems correct")
check_translation_csv <- function(path, source_encoding = "latin1", new_path = path) {
if (!file.exists(path)) {
stop("File doesn't exists")
}
if (tools::file_ext(path) != "csv") {
stop("path should be a csv file")
}
if (tools::file_ext(new_path) != "csv") {
stop("new_path should be a csv file")
}
fls <- read.csv(path, encoding = source_encoding)
if (
names(fls)[1] != "entry"
) {
stop("The first column should be named 'entry'")
}
if (
!"en" %in% names(fls)
) {
stop("Their is no 'en' column")
}
if (
!"fr" %in% names(fls)
) {
stop("Their is no 'fr' column")
}
if (source_encoding != "UTF-8") {
fls <- fls %>% mutate_all(enc2utf8)
write.csv(fls, file = new_path,
fileEncoding = "UTF-8",
# col.names = TRUE,
row.names = FALSE)
cli::cli_alert_success("CSV was re-encoded to UTF-8")
} else {
file.copy(path, new_path)
}
cli::cli_alert_success("CSV seems correct")

return(fls)
}
13 changes: 10 additions & 3 deletions dev/translation.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,24 @@ DBI::dbGetQuery(
+ `inst/translation.csv` est le fichier à compléter à la main.
Il peut être ouvert avec Excel et __doit__ être sauvegardé en `.csv`.

Vous pouvez vérifier l'intégriter du csv via :
Vous devez vérifier l'intégrité du csv via et indiquer avec quel encodage vous avez créé le fichier :

- `source_encoding = "UTF-8"` a priori, si créé sous Linux/MacOS
- `source_encoding = "latin1"` a priori, si créé sur Excel/LibreOffice avec Windows

Ainsi, si vous rencontrez des problèmes d'encodage sur l'application, vous pouvez modifier de nouveau le csv et changer le paramètre ci-dessous.
Vous pouvez aussi le vérifier en regardant le fichier lu avec `output` ci-dessous.

```{r}
check_translation_csv("inst/translation.csv")
output <- check_translation_csv("inst/translation.csv", source_encoding = "latin1")
head(output)
```

## Notes pour les dévelopeurs

Comment créer un nouvel élément "traductible" dans l'UI?

+ La traduction est assurée par le module JS `i18n` et la fonction `with_i18` dans l'app
+ La traduction est assurée par le module JS `i18n` et la fonction `with_i18()` dans l'app

```{r}
with_i18(
Expand Down
2 changes: 2 additions & 0 deletions inst/reprex/trad_latin1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
entry,en,fr
toto,t�t�,t�t�
2 changes: 2 additions & 0 deletions inst/reprex/trad_utf8.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
entry,en,fr
toto,tùtù,tàtà
9 changes: 8 additions & 1 deletion man/check_translation_csv.Rd

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

42 changes: 42 additions & 0 deletions tests/testthat/test-check_translation_csv.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
trad_latin <- system.file("reprex", "trad_latin1.csv", package = "diades.atlas")

new_output_test <- tempfile(fileext = ".csv")

test_that("check_translation_csv works with encoding Latin1", {
output <- check_translation_csv(
path = trad_latin,
source_encoding = "latin1", new_path = new_output_test)
expect_equal(names(output), c("entry", "en", "fr"))
expect_equal(as.character(output[1,]), c("toto", "t\u00f9t\u00f9", "t\u00e0t\u00e0"))
expect_equal(
readLines(new_output_test)[1],
c("\"entry\",\"en\",\"fr\"")
)

# Read re-encoded one: new_output_test
output <- check_translation_csv(
path = new_output_test,
source_encoding = "UTF-8", new_path = new_output_test)
expect_equal(names(output), c("entry", "en", "fr"))
expect_equal(as.character(output[1,]), c("toto", "t\u00f9t\u00f9", "t\u00e0t\u00e0"))
expect_equal(
readLines(new_output_test)[1],
c("\"entry\",\"en\",\"fr\"") # Copied with write.csv
)
})

# with uf8 ----
trad_utf8 <- system.file("reprex", "trad_utf8.csv", package = "diades.atlas")
new_output_test <- tempfile(fileext = ".csv")

test_that("check_translation_csv works with encoding UTF8 directly", {
output <- check_translation_csv(
path = trad_utf8,
source_encoding = "UTF-8", new_path = new_output_test)
expect_equal(names(output), c("entry", "en", "fr"))
expect_equal(as.character(output[1,]), c("toto", "t\u00f9t\u00f9", "t\u00e0t\u00e0"))
expect_equal(
readLines(new_output_test)[1],
c("entry,en,fr") # Copied as is
)
})

0 comments on commit 5248856

Please sign in to comment.