From f715be89dcb66f996bd12ff3e742e3bf8c1245af Mon Sep 17 00:00:00 2001 From: Stephan Tittel Date: Tue, 8 Oct 2024 10:36:42 +0200 Subject: [PATCH] fix selection of correct langStrings depending on navigator.languages --- demo/complex-example.ttl | 10 +++++----- src/config.ts | 4 ++-- src/property-template.ts | 6 +++--- src/util.ts | 20 +++++++++++++++++++- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/demo/complex-example.ttl b/demo/complex-example.ttl index b5cdfe2..b4cd742 100644 --- a/demo/complex-example.ttl +++ b/demo/complex-example.ttl @@ -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" ; @@ -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 ( diff --git a/src/config.ts b/src/config.ts index 3242628..e9f0dd9 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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) { diff --git a/src/property-template.ts b/src/property-template.ts index 6f16216..d1ea85e 100644 --- a/src/property-template.ts +++ b/src/property-template.ts @@ -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 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 }, diff --git a/src/util.ts b/src/util.ts index 4c9b7ae..c548a6c 100644 --- a/src/util.ts +++ b/src/util.ts @@ -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' @@ -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 +} \ No newline at end of file