Skip to content

Commit

Permalink
#298 test multi-lingual support + apply to title / ISO 19115 citation…
Browse files Browse the repository at this point in the history
…/title
  • Loading branch information
eblondel committed Dec 20, 2022
1 parent 43aa346 commit 10bdc95
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 10 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export(executeWorkflow)
export(executeWorkflowJob)
export(extract_cell_components)
export(extract_kvp)
export(extract_kvps)
export(filter_sf_by_cqlfilter)
export(geoflowLogger)
export(geoflow_action)
Expand Down Expand Up @@ -54,6 +55,7 @@ export(geoflow_validator_entity_TemporalCoverage)
export(geoflow_validator_entity_Title)
export(geoflow_validator_entity_Type)
export(get_line_separator)
export(get_locales_from)
export(get_union_bbox)
export(initWorkflow)
export(initWorkflowJob)
Expand Down
10 changes: 10 additions & 0 deletions R/geoflow_entity.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ geoflow_entity <- R6Class("geoflow_entity",
status = list(),
#'@field resources entity resources
resources = list(),
#'@field locales entity locales
locales = list(),

#'@description Initializes an object of class \link{geoflow_entity}
initialize = function(){
Expand All @@ -79,6 +81,13 @@ geoflow_entity <- R6Class("geoflow_entity",
return(clazz_obj$getValidKeys())
},

#'@description Adds locales to entity from kvp values
#'@param values values
addLocalesFromValues = function(values){
locales <- unlist(lapply(names(attributes(values)), function(x){unlist(strsplit(x,"locale#"))[2]}))
self$locales <- unique(c(self$locales, locales))
},

#'@description Set an identifier given a key. Default key is "id", but others can be specified, eg "doi".
#'@param key identifier key. Default is "id"
#'@param id identifier value
Expand Down Expand Up @@ -141,6 +150,7 @@ geoflow_entity <- R6Class("geoflow_entity",
paste0(self$getAllowedKeyValuesFor("Title"), collapse=",")))
}
self$titles[[key]] <- title
self$addLocalesFromValues(title)
},

#'@description Sets description
Expand Down
74 changes: 73 additions & 1 deletion R/geoflow_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,79 @@ extract_kvp <- function(str){
return(value)
})

return(list(key = key, values = values))
#locale management
locale = NULL
key_parts <- unlist(strsplit(key, "#"))
if(length(key_parts)>1){
key <- key_parts[1]
locale <- key_parts[2]
}

return(list(key = key, values = values, locale = locale))
}

#' @name extract_kvp
#' @aliases extract_kvp
#' @title extract_kvp
#' @description \code{extract_kvp} parses a string into a key value pair represented by
#' a \code{geoflow_kvp} object.
#'
#' @usage extract_kvps(strs)
#'
#' @param str a string as object of class \code{character}
#' @param collapse collapse by. Default is \code{NULL}
#'
#' @author Emmanuel Blondel, \email{emmanuel.blondel1@@gmail.com}
#' @export
#'
extract_kvps <- function(strs, collapse = NULL){
kvps <- lapply(strs, function(str){
kvp <- extract_kvp(str)
if(!is.null(collapse)) kvp$values <- list(paste0(kvp$values, collapse = collapse))
return(kvp)
})
keys <- unique(sapply(kvps, "[[", "key"))
kvps <- lapply(keys, function(key){
kvps_for_key <- kvps[sapply(kvps, function(kvp){kvp$key == key})]
with_null_locale <- any(sapply(kvps_for_key, function(x){is.null(x$locale)}))
kvp_with_default_locale <- kvps_for_key[sapply(kvps_for_key, function(x){is.null(x$locale)})]
if(length(kvp_with_default_locale)>0){
kvp_with_default_locale <- kvp_with_default_locale[[1]]
}else{
#TODO support default language in geoflow
}
locale_values <- kvp_with_default_locale$values
if(length(locale_values)==1) locale_values <- locale_values[[1]]
kvp_with_locale <- kvps_for_key[sapply(kvps_for_key, function(x){!is.null(x$locale)})]
for(item in kvp_with_locale){
attr(locale_values, paste0("locale#", toupper(item$locale))) <- item$values
}

kvp_with_locales <- list(key = key, values = locale_values)
return(kvp_with_locales)
})

return(kvps)
}

#'@name get_locales_from
#'@aliases get_locales_from
#'@title get_locales_from
#'@description Get locales from a property values set
#'
#'@usage get_locales_from(values)
#'
#'@param values values
#'
#'@export
get_locales_from <- function(values){
if(is.null(attributes(values))) return(NULL)
locales <- lapply(attributes(values), function(x){
if(is.list(x)) x <- x[[1]]
return(x)
})
names(locales) <- sapply(names(attributes(values)), function(x){unlist(strsplit(x, "locale#"))[2]})
return(locales)
}

#' @name str_to_posix
Expand Down
13 changes: 12 additions & 1 deletion inst/actions/geometa_create_iso_19115.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ function(action, entity, config){
md$setLanguage(entity$language)
md$setDateStamp(Sys.time())

#locales (i18n/i10n support)
if(length(entity$locales)){
for(locale in entity$locales){
a_locale <- ISOLocale$new()
a_locale$setId(locale)
a_locale$setLanguage(locale)
a_locale$setCharacterSet("utf8")
md$addLocale(a_locale)
}
}

if(!is.null(entity$data)) {
md$setMetadataStandardName(switch(entity$data$spatialRepresentationType,
"vector" = "ISO 19115:2003 Geographic information - Metadata",
Expand Down Expand Up @@ -253,7 +264,7 @@ function(action, entity, config){
#citation
now <- Sys.time()
ct <- ISOCitation$new()
ct$setTitle(entity$titles[["title"]])
ct$setTitle(entity$titles[["title"]], locales = geoflow::get_locales_from(entity$titles[["title"]]))
if("alternative" %in% names(entity$titles)){
ct$setAlternateTitle(entity$titles[["alternative"]])
}
Expand Down
11 changes: 3 additions & 8 deletions inst/metadata/entity/entity_handler_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,9 @@ handle_entities_df <- function(config, source){
}
titles <- if(!is.na(src_title)) extract_cell_components(src_title) else list()
if(length(titles)>0){
if(length(titles)==1){
kvp <- extract_kvp(titles)
entity$setTitle("title", paste(kvp$values, collapse=","))
}else{
for(title in titles){
kvp <- extract_kvp(title)
entity$setTitle(kvp$key, paste(kvp$values, collapse=","))
}
kvps <- extract_kvps(titles, collapse=",")
for(kvp in kvps){
entity$setTitle(kvp$key, kvp$values)
}
}

Expand Down
8 changes: 8 additions & 0 deletions man/extract_kvp.Rd

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

20 changes: 20 additions & 0 deletions man/geoflow_entity.Rd

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

14 changes: 14 additions & 0 deletions man/get_locales_from.Rd

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

0 comments on commit 10bdc95

Please sign in to comment.