Skip to content

Commit

Permalink
Load vocab metadata using specific locale
Browse files Browse the repository at this point in the history
  • Loading branch information
inukshuk committed Mar 30, 2018
1 parent d31d954 commit eaac742
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/commands/ontology.js
Expand Up @@ -39,7 +39,7 @@ class Import extends Command {
let file = files[i]

try {
let data = (yield call(Ontology.open, file, false)).toJSON()
let data = (yield call(Ontology.open, file, false)).toJSON(ARGS.locale)

yield call(db.transaction, async tx => {
for (let id in data) {
Expand Down
34 changes: 21 additions & 13 deletions src/common/ontology.js
Expand Up @@ -104,7 +104,7 @@ class Ontology extends Resource {
this.name = name
}

toJSON() {
toJSON(locale = 'en') {
let json = {}
let seq = 0

Expand All @@ -117,7 +117,7 @@ class Ontology extends Resource {
if (empty(data)) return []

let ns = isDefinedBy(id, data)
let vocab = json[ns] || this.getVocabulary(ns, prefix())
let vocab = json[ns] || this.getVocabulary(ns, prefix(), locale)

if (vocab == null) return []
if (json[ns] == null) json[ns] = vocab
Expand All @@ -142,42 +142,42 @@ class Ontology extends Resource {
let range = get(data, [RDFS.range, 0, '@id'])

vocab.properties.push({
id, data, vocabulary: vocab.id, domain, range, ...info(data)
id, data, vocabulary: vocab.id, domain, range, ...info(data, locale)
})
}

for (let id of this.getByType(...Ontology.CLASSES)) {
let [vocab, data] = collect(id)
if (vocab == null) continue
vocab.classes.push({
id, data, vocabulary: vocab.id, ...info(data)
id, data, vocabulary: vocab.id, ...info(data, locale)
})
}

for (let id of this.getByType(...Ontology.DATATYPES)) {
let [vocab, data] = collect(id)
if (vocab == null) continue
vocab.datatypes.push({
id, data, vocabulary: vocab.id, ...info(data)
id, data, vocabulary: vocab.id, ...info(data, locale)
})
}

return json
}

getVocabulary(id, prefix, title = id) {
getVocabulary(id, prefix, title = id, locale = 'en') {
let data = this.getData(id)

if (empty(data) && (/[#/]$/).test(id)) {
data = this.getData(id.slice(0, id.length - 1))
}

title = get(any(data, DC.title, TERMS.title), [0, '@value'], title)
title = getValue(any(data, DC.title, TERMS.title), locale) || title
prefix = get(data, [VANN.preferredNamespacePrefix, 0, '@value'], prefix)

const seeAlso = get(data, [RDFS.seeAlso, 0, '@id'])
const description = getValue(
any(data, DC.description, TERMS.description, RDFS.comment)
any(data, DC.description, TERMS.description, RDFS.comment), locale
)

return {
Expand Down Expand Up @@ -212,17 +212,25 @@ class Ontology extends Resource {
}


function info(data) {
function info(data, locale = 'en') {
return {
comment: getValue(data, RDFS.comment),
comment: getValue(any(data, RDFS.comment), locale),
description: getValue(
any(data, SKOS.defintion, DC.description, TERMS.description)
any(data, SKOS.defintion, DC.description, TERMS.description), locale
)
}
}

function getValue(data, ...path) {
return get(data, [...path, 0, '@value'])
function getValue(data, locale = 'en') {
if (data == null || data.length === 0) {
return null
}
for (let value of data) {
if (value['@language'] === locale) {
return value['@value']
}
}
return data[0]['@value']
}

function literal(value) {
Expand Down

0 comments on commit eaac742

Please sign in to comment.