diff --git a/DESCRIPTION b/DESCRIPTION index 6e5a8ca..2e34e77 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,5 +22,6 @@ Imports: yaml, RCurl, digest, - tools + tools, + whisker Roxygen: list(wrap = FALSE) diff --git a/NAMESPACE b/NAMESPACE index 3e459d3..d10ddd8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -45,3 +45,4 @@ importFrom(tools,file_ext) importFrom(tools,file_path_sans_ext) importFrom(yaml,yaml.load) importFrom(yaml,yaml.load_file) +importFrom(whisker,whisker.render) diff --git a/R/menu.R b/R/menu.R index 6127a4e..f6fa213 100644 --- a/R/menu.R +++ b/R/menu.R @@ -293,6 +293,12 @@ loadLesson.default <- function(e, courseU, lesson){ clearCustomTests() loadCustomTests(lesPath) + # Check if there is a localization file + localization <- file.path(lesPath, "locale.yaml") + if(file.exists(localization)){ + dataName <- localize_lesson(localization, lesPath, shortname) + } + # Attached class to content based on file extension class(dataName) <- get_content_class(dataName) diff --git a/R/parse_content.R b/R/parse_content.R index e060c74..6db6eea 100644 --- a/R/parse_content.R +++ b/R/parse_content.R @@ -54,4 +54,33 @@ parse_content.yaml <- function(file, e){ lesson(df, lesson_name=meta$Lesson, course_name=meta$Course, author=meta$Author, type=meta$Type, organization=meta$Organization, version=meta$Version) +} + + +#' @importFrom tools file_ext +#' @importFrom tools file_path_sans_ext +#' @importFrom yaml yaml.load_file +#' @importFrom whisker whisker.render +localize_lesson <- function(file, lesPath, shortname){ + + # Load localization file + locale_yaml <- yaml.load_file(file) + + # TO DO: Process localization. Placeholder loads first element. + locale_yaml <- locale_yaml[[1]] + + # Load lesson file + lesson <- readLines(con = file.path(lesPath, shortname)) + + # Render localized lesson + lesson <- whisker.render(template = lesson, data = locale_yaml) + + # Save localized lesson as a temporary file + file <- file_path_sans_ext(shortname) + extension <- paste0(".", file_ext(shortname)) + localized_lesson <- tempfile(pattern = file, fileext = extension) + writeLines(lesson, con = localized_lesson) + + # Return file path for localized lesson + return(localized_lesson) } \ No newline at end of file