@@ -50,6 +50,19 @@ class Format < RDF::Format
5050 # range: "xsd:float",
5151 # type: "owl:DatatypeProperty",
5252 # "vs:term_status": "testing"
53+ #
54+ # @example term definition with language-tagged strings
55+ # @example A term definition with tagged values
56+ # property :actor,
57+ # comment: {en: "Subproperty of as:attributedTo that identifies the primary actor"},
58+ # domain: "https://www.w3.org/ns/activitystreams#Activity",
59+ # label: {en: "actor"},
60+ # range: term(
61+ # type: "http://www.w3.org/2002/07/owl#Class",
62+ # unionOf: list("https://www.w3.org/ns/activitystreams#Object", "https://www.w3.org/ns/activitystreams#Link")
63+ # ),
64+ # subPropertyOf: "https://www.w3.org/ns/activitystreams#attributedTo",
65+ # type: "http://www.w3.org/2002/07/owl#ObjectProperty"
5366 class Writer < RDF ::Writer
5467 include RDF ::Util ::Logger
5568 format RDF ::Vocabulary ::Format
@@ -272,9 +285,17 @@ def from_node(name, attributes, term_type)
272285 attributes . keys . sort_by ( &:to_s ) . map ( &:to_sym ) . each do |key |
273286 value = Array ( attributes [ key ] )
274287 component = key . inspect . start_with? ( ':"' ) ? "#{ key . to_s . inspect } : " : "#{ key } : "
275- value = value . first if value . length == 1
288+ value = value . first if value . length == 1 && value . none? { | v | v . is_a? ( RDF :: Literal ) && v . language? }
276289 component << if value . is_a? ( Array )
277- '[' + value . map { |v | serialize_value ( v , key , indent : " " ) } . sort . join ( ", " ) + "]"
290+ # Represent language-tagged literals as a hash
291+ lang_vals , vals = value . partition { |v | v . literal? && v . language? }
292+ hash_val = lang_vals . inject ( { } ) { |memo , obj | memo . merge ( obj . language => obj . to_s ) }
293+ vals << hash_val unless hash_val . empty?
294+ if vals . length > 1
295+ '[' + vals . map { |v | serialize_value ( v , key , indent : " " ) } . sort . join ( ", " ) + "]"
296+ else
297+ serialize_value ( vals . first , key , indent : " " )
298+ end
278299 else
279300 serialize_value ( value , key , indent : " " )
280301 end
@@ -284,7 +305,14 @@ def from_node(name, attributes, term_type)
284305 end
285306
286307 def serialize_value ( value , key , indent : "" )
287- if value . is_a? ( Literal ) && %w( : comment definition notation note editorialNote ) . include? ( key . to_s )
308+ if value . is_a? ( Hash )
309+ # Favor English
310+ keys = value . keys
311+ keys = keys . include? ( :en ) ? ( keys - [ :en ] ) . sort . unshift ( :en ) : keys . sort
312+ '{' + keys . map do |k |
313+ "#{ k . inspect [ 1 ..-1 ] } : #{ value [ k ] . inspect } "
314+ end . join ( ', ' ) + '}'
315+ elsif value . is_a? ( Literal ) && %w( : comment definition notation note editorialNote ) . include? ( key . to_s )
288316 "#{ value . to_s . inspect } "
289317 elsif value . is_a? ( RDF ::URI )
290318 "#{ value . to_s . inspect } "
0 commit comments