Skip to content

Commit 0891de4

Browse files
committed
docs: Enhance README with language tag management and examples; update LanguagePreferences documentation
1 parent fcf169f commit 0891de4

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,102 @@ console.log(person2.mum.mum.name)
169169
// outputs "Joanne"
170170
```
171171

172+
### Managing Language Tags
173+
174+
RDF includes a special attribute for string literals called a [language tag](https://www.w3.org/TR/rdf11-concepts/#section-Graph-Literal). Language tags let developers provide string values for many different translations. This library provides `LanguagePreferences` to declaratively control which translations are read and written.
175+
176+
A language preference is an ordered list of [IETF language tags](https://en.wikipedia.org/wiki/IETF_language_tag) plus the special tags `@none` (matches strings without a language tag) and `@other` (matches any language not explicitly listed).
177+
178+
- **Read** operations return the value matching the highest-priority preference.
179+
- **Write** operations use the first non-`@other` preference as the language tag.
180+
181+
#### Singular properties
182+
183+
```javascript
184+
import { LanguagePreferences, RequiredFrom, RequiredAs, TermWrapper } from "@rdfjs/wrapper"
185+
186+
class Hospital extends TermWrapper {
187+
languages = new LanguagePreferences("es", "ko", "@none")
188+
189+
get label() {
190+
return RequiredFrom.subjectPredicateByLanguage(this, "http://www.w3.org/2000/01/rdf-schema#label", this.languages)
191+
}
192+
193+
set label(value) {
194+
RequiredAs.objectByLanguage(this, "http://www.w3.org/2000/01/rdf-schema#label", value, this.languages)
195+
}
196+
}
197+
```
198+
199+
Assuming the following RDF has been loaded in a dataset `dataset`:
200+
201+
```turtle
202+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
203+
204+
<hospital>
205+
rdfs:label "Hospital" ;
206+
rdfs:label "Hôpital"@fr ;
207+
rdfs:label "병원"@ko .
208+
```
209+
210+
Class usage:
211+
212+
```javascript
213+
const hospital = new Hospital("hospital", dataset, DataFactory)
214+
215+
// No Spanish label, so falls through to Korean
216+
console.log(hospital.label)
217+
// outputs "병원"
218+
219+
// Writing adds a Spanish label (first preference)
220+
hospital.label = "Hospital Español"
221+
console.log(hospital.label)
222+
// outputs "Hospital Español"
223+
```
224+
225+
#### Set properties
226+
227+
```javascript
228+
import { LanguagePreferences, SetFrom, TermWrapper } from "@rdfjs/wrapper"
229+
230+
class Hospital extends TermWrapper {
231+
languages = new LanguagePreferences("fr", "ko", "@none")
232+
233+
get descriptions() {
234+
return SetFrom.subjectPredicateByLanguage(this, "http://www.w3.org/2000/01/rdf-schema#description", this.languages)
235+
}
236+
}
237+
```
238+
239+
```javascript
240+
const hospital = new Hospital("hospital", dataset, DataFactory)
241+
242+
// Returns French descriptions (highest-priority match)
243+
console.log(hospital.descriptions.size)
244+
// outputs 2
245+
246+
for (const desc of hospital.descriptions) {
247+
console.log(desc)
248+
}
249+
// outputs "Guérit les malades"
250+
// outputs "A des médecins"
251+
```
252+
253+
#### Inspecting all translations
254+
255+
The `languagesOf` function returns all language-tagged string values for a predicate, grouped by language tag:
256+
257+
```javascript
258+
import { languagesOf } from "@rdfjs/wrapper"
259+
260+
const langs = languagesOf(hospital, "http://www.w3.org/2000/01/rdf-schema#label")
261+
// Map { "@none" => ["Hospital"], "fr" => ["Hôpital"], "ko" => ["병원"] }
262+
```
263+
264+
#### Optional properties
265+
266+
`OptionalFrom.subjectPredicateByLanguage` and `OptionalAs.objectByLanguage` work the same way but return `undefined` when no match is found and remove all language-tagged quads when set to `undefined`.
267+
172268

173269
## Background
174270

src/LanguagePreferences.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { XSD } from "./vocabulary/XSD.js"
1212
*
1313
* Valid preference values include:
1414
* - Any {@link https://en.wikipedia.org/wiki/IETF_language_tag | IETF language tag} (e.g., `"en"`, `"fr"`, `"ko"`)
15-
* - `"@none"` — matches literals with `rdf:langString` datatype but no language tag
15+
* - `"@none"` — matches string literals that have no language tag (plain `xsd:string` or `rdf:langString` without a tag)
1616
* - `"@other"` — matches any language not explicitly listed in the preferences
1717
*
1818
* For read operations, literals are searched in preference order and the first

0 commit comments

Comments
 (0)