diff --git a/lib/rdf2marc/rdf2model/mappers/control_fields.rb b/lib/rdf2marc/rdf2model/mappers/control_fields.rb index 0280bc6..b723dae 100644 --- a/lib/rdf2marc/rdf2model/mappers/control_fields.rb +++ b/lib/rdf2marc/rdf2model/mappers/control_fields.rb @@ -45,8 +45,22 @@ def parse_date(date_literal) raise BadRequestError, "#{date_literal} is an invalid date." end + LANGUAGE_PARTS = ['text', 'sung or spoken text'].freeze + def language - language_uri = item.work.query.path_first_uri([BF.language]) + language_nodes = item.work.query.path_all([BF.language]) + preferred_nodes = language_nodes.select do |node| + LANGUAGE_PARTS.include?(item.work.query.path_first_literal([BF.part], subject_term: node)&.downcase) + end + + language_blank_node = preferred_nodes.first || language_nodes.first + return nil if language_blank_node.nil? + + language_node = item.work.query.path_first([RDF::RDFS.label], subject_term: language_blank_node) + return nil unless language_node.is_a?(RDF::URI) + + language_uri = language_node.value + return nil if language_uri.nil? || !language_uri.start_with?(%r{https?://id.loc.gov/vocabulary/languages/}) language_uri.sub(%r{^https?://id.loc.gov/vocabulary/languages/}, '') diff --git a/spec/rdf2marc/rdf2model/mappers/control_fields_spec.rb b/spec/rdf2marc/rdf2model/mappers/control_fields_spec.rb index 80f0457..457871b 100644 --- a/spec/rdf2marc/rdf2model/mappers/control_fields_spec.rb +++ b/spec/rdf2marc/rdf2model/mappers/control_fields_spec.rb @@ -98,25 +98,58 @@ end describe 'general info language' do - let(:ttl) do - <<~TTL - <#{work_term}> . - "Achinese". - <> . - "Bugis". - TTL + context 'when language part is text' do + let(:ttl) do + <<~TTL + <#{work_term}> _:b29, _:b30. + _:b29 a ; + ; + "Original"@en. + "English". + _:b30 a ; + ; + "Text"@en. + "Ukrainian". + TTL + end + + let(:model) do + { + general_info: { + language: 'ukr', + place: 'xx' + } + } + end + + include_examples 'mapper', described_class end - let(:model) do - { - general_info: { - language: 'ace', - place: 'xx' + context 'when no language part' do + let(:ttl) do + <<~TTL + <#{work_term}> _:b29, _:b30. + _:b29 a ; + ; + "Original"@en. + "English". + _:b30 a ; + . + "Ukrainian". + TTL + end + + let(:model) do + { + general_info: { + language: 'eng', + place: 'xx' + } } - } - end + end - include_examples 'mapper', described_class + include_examples 'mapper', described_class + end end describe 'general info book illustrative content' do