Class: Opencdd::Languages
- Inherits:
-
Object
- Object
- Opencdd::Languages
- Defined in:
- lib/opencdd/languages.rb
Overview
Value object representing the language configuration of a CDD
dictionary. The source language is the one the data was originally
authored in; translation languages are additional languages that
appear in the .xls property keys (e.g. MDC_P004.de).
The exporter scans each entity's @properties hash for
<property_id>.<lang> keys to build per-field language maps
(TODO.work/08). This class is the model object that aggregates
"what languages does this dictionary have?" — used by the browser
to render a language switcher and by the data pipeline to report
language coverage.
This is a model object. It does not normalize language codes:
source-format quirks (e.g. IEC CDD .xls files using "jp" for
Japanese) are normalized at the SheetSchema ingestion boundary
via Opencdd::Parcel::LanguageAliases, so every consumer of
this class can assume ISO 639-1 codes unconditionally.
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#translations ⇒ Object
readonly
Returns the value of attribute translations.
Class Method Summary collapse
-
.from_properties(properties, default_source: "en") ⇒ Object
Scan a properties hash for
<property_id>.<lang>keys and return a Languages object covering every language seen.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #all ⇒ Object
- #empty? ⇒ Boolean
- #hash ⇒ Object
- #include?(lang) ⇒ Boolean
-
#initialize(source: "en", translations: []) ⇒ Languages
constructor
A new instance of Languages.
- #size ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(source: "en", translations: []) ⇒ Languages
Returns a new instance of Languages.
24 25 26 27 28 |
# File 'lib/opencdd/languages.rb', line 24 def initialize(source: "en", translations: []) @source = source.to_s @translations = Array(translations).map(&:to_s).uniq - [@source] freeze end |
Instance Attribute Details
#source ⇒ Object (readonly)
Returns the value of attribute source.
22 23 24 |
# File 'lib/opencdd/languages.rb', line 22 def source @source end |
#translations ⇒ Object (readonly)
Returns the value of attribute translations.
22 23 24 |
# File 'lib/opencdd/languages.rb', line 22 def translations @translations end |
Class Method Details
.from_properties(properties, default_source: "en") ⇒ Object
Scan a properties hash for <property_id>.<lang> keys and
return a Languages object covering every language seen. The
source language defaults to default_source when no explicit
source-language key is present.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/opencdd/languages.rb', line 64 def self.from_properties(properties, default_source: "en") langs = properties.keys.each_with_object(Set.new) do |key, acc| next unless key.include?(".") _, lang = key.split(".", 2) next unless lang =~ /\A[a-z]{2}(-[a-z0-9]+)?\z/i acc << lang end source = langs.include?(default_source) ? default_source : (langs.first || default_source) translations = langs.to_a - [source] new(source: source, translations: translations) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
50 51 52 53 |
# File 'lib/opencdd/languages.rb', line 50 def ==(other) other.is_a?(Opencdd::Languages) && source == other.source && translations == other.translations end |
#all ⇒ Object
30 31 32 |
# File 'lib/opencdd/languages.rb', line 30 def all [@source] + @translations end |
#empty? ⇒ Boolean
38 39 40 |
# File 'lib/opencdd/languages.rb', line 38 def empty? @source.nil? || @source.empty? end |
#hash ⇒ Object
56 57 58 |
# File 'lib/opencdd/languages.rb', line 56 def hash [source, translations].hash end |
#include?(lang) ⇒ Boolean
34 35 36 |
# File 'lib/opencdd/languages.rb', line 34 def include?(lang) all.include?(lang.to_s) end |
#size ⇒ Object
42 43 44 |
# File 'lib/opencdd/languages.rb', line 42 def size all.size end |
#to_a ⇒ Object
46 47 48 |
# File 'lib/opencdd/languages.rb', line 46 def to_a all end |