Skip to content

Commit

Permalink
fix selection of correct langStrings depending on navigator.languages
Browse files Browse the repository at this point in the history
  • Loading branch information
s-tittel committed Oct 8, 2024
1 parent f283c2c commit f715be8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
10 changes: 5 additions & 5 deletions demo/complex-example.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ example:Dataset
sh:property [ sh:datatype rdf:langString ;
sh:languageIn ( "en" "de" ) ;
sh:uniqueLang true ;
sh:description "Name of the dataset" , "Der Name des Datensatzes"@de ;
sh:description "Name of the dataset"@en , "Name des Datensatzes"@de ;
sh:minCount 1 ;
sh:maxCount 2 ;
sh:name "Name" ;
Expand All @@ -76,16 +76,16 @@ example:Dataset
sh:datatype rdf:langString ;
sh:languageIn ( "en" "de" ) ;
sh:uniqueLang true ;
sh:description "Description of the dataset" ;
sh:description "Description of the dataset"@en, "Beschreibung des Datensatzes"@de ;
sh:minCount 1 ;
sh:maxCount 2 ;
sh:name "Description" ;
sh:name "Description"@en, "Beschreibung"@de ;
sh:path dcterms:description
] ;
sh:property [ sh:description "License of the dataset" ;
sh:property [ sh:description "License of the dataset"@en, "Lizenz des Datensatzes"@de ;
sh:maxCount 1 ;
sh:minCount 1 ;
sh:name "License" ;
sh:name "License"@en, "Lizenz"@de ;
sh:nodeKind sh:IRI ;
sh:path dcterms:license ;
sh:in (
Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ export class Config {
this.languages = [...new Set(navigator.languages.flatMap(lang => {
if (lang.length > 2) {
// for each 5 letter lang code (e.g. de-DE) append its corresponding 2 letter code (e.g. de) directly afterwards
return [lang, lang.substring(0, 2)]
return [lang.toLocaleLowerCase(), lang.substring(0, 2)]
}
return lang
}))]
})), ''] // <-- append empty string to accept RDF literals with no language
}

updateAttributes(elem: HTMLElement) {
Expand Down
6 changes: 3 additions & 3 deletions src/property-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Literal, NamedNode, Quad, DataFactory } from 'n3'
import { Term } from '@rdfjs/types'
import { OWL_PREDICATE_IMPORTS, PREFIX_DASH, PREFIX_OA, PREFIX_RDF, PREFIX_SHACL, SHACL_PREDICATE_CLASS, SHACL_PREDICATE_TARGET_CLASS } from './constants'
import { Config } from './config'
import { findLabel, removePrefixes } from './util'
import { findLabel, prioritizeByLanguage, removePrefixes } from './util'
import { ShaclNode } from './node'

const mappers: Record<string, (template: ShaclPropertyTemplate, term: Term) => void> = {
[`${PREFIX_SHACL}name`]: (template, term) => { const literal = term as Literal; if (!template.name || literal.language === template.config.attributes.language) { template.name = literal } },
[`${PREFIX_SHACL}description`]: (template, term) => { const literal = term as Literal; if (!template.description || literal.language === template.config.attributes.language) { template.description = literal } },
[`${PREFIX_SHACL}name`]: (template, term) => { const literal = term as Literal; template.name = prioritizeByLanguage(template.config.languages, template.name, literal) },
[`${PREFIX_SHACL}description`]: (template, term) => { const literal = term as Literal; template.description = prioritizeByLanguage(template.config.languages, template.description, literal) },
[`${PREFIX_SHACL}path`]: (template, term) => { template.path = term.value },
[`${PREFIX_SHACL}node`]: (template, term) => { template.node = term as NamedNode },
[`${PREFIX_SHACL}datatype`]: (template, term) => { template.datatype = term as NamedNode },
Expand Down
20 changes: 19 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NamedNode, Prefixes, Quad, Store } from 'n3'
import { Literal, NamedNode, Prefixes, Quad, Store } from 'n3'
import { OWL_OBJECT_NAMED_INDIVIDUAL, PREFIX_RDFS, PREFIX_SHACL, PREFIX_SKOS, RDFS_PREDICATE_SUBCLASS_OF, RDF_PREDICATE_TYPE, SHAPES_GRAPH, SKOS_PREDICATE_BROADER } from './constants'
import { Term } from '@rdfjs/types'
import { InputListEntry } from './theme'
Expand Down Expand Up @@ -114,3 +114,21 @@ export function isURL(input: string): boolean {
}
return url.protocol === 'http:' || url.protocol === 'https:'
}

export function prioritizeByLanguage(languages: string[], text1?: Literal, text2?: Literal): Literal | undefined {
if (text1 === undefined) {
return text2
}
if (text2 === undefined) {
return text1
}
const index1 = languages.indexOf(text1.language)
if (index1 < 0) {
return text2
}
const index2 = languages.indexOf(text2.language)
if (index2 < 0) {
return text1
}
return index2 > index1 ? text1 : text2
}

0 comments on commit f715be8

Please sign in to comment.