Module: Dcc::I18n::TextLookup

Defined in:
lib/dcc/i18n/text_lookup.rb

Overview

Dcc::I18n::TextLookup extracts the best-matching localized string from a dcc:textType block.

Selection order:

1. Exact match for the requested language
2. First mandatory language declared in the DCC coreData
3. First used language declared in the DCC coreData
4. First content element regardless of language
5. nil if no content at all

Class Method Summary collapse

Class Method Details

.call(text_obj, dcc: nil, lang: nil) ⇒ String?

Parameters:

  • text_obj (Object, nil)

    a parsed dcc:textType block.

  • dcc (Object, nil) (defaults to: nil)

    the parent DCC (used to read declared langs).

  • lang (String, nil) (defaults to: nil)

    requested ISO 639-1 code.

Returns:

  • (String, nil)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dcc/i18n/text_lookup.rb', line 20

def call(text_obj, dcc: nil, lang: nil)
  return nil unless text_obj && Dcc::TypeGuards.has_attribute?(text_obj, :content)

  contents = Array(text_obj.content)
  return nil if contents.empty?

  return content_value(contents, lang) if lang

  dcc_langs = declared_languages(dcc)
  dcc_langs.each do |fallback|
    val = content_value(contents, fallback)
    return val if val
  end

  content_value(contents, nil)
end