Class: Traductor::Glossary

Inherits:
Object
  • Object
show all
Defined in:
lib/traductor/glossary.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Glossary

Returns a new instance of Glossary.



9
10
11
# File 'lib/traductor/glossary.rb', line 9

def initialize(path = nil)
  @terms = path && File.exist?(path) ? load_terms(path) : {}
end

Instance Attribute Details

#terms ⇒ Object (readonly)

Returns the value of attribute terms.



7
8
9
# File 'lib/traductor/glossary.rb', line 7

def terms
  @terms
end

Instance Method Details

#for_locale(locale) ⇒ Object

Returns terms relevant to a specific target locale



14
15
16
17
# File 'lib/traductor/glossary.rb', line 14

def for_locale(locale)
  locale = locale.to_s
  @terms.select { |_term, translations| translations.key?(locale) }
end

#to_prompt_text(locale) ⇒ Object

Formats glossary entries for inclusion in prompts



20
21
22
23
24
25
26
27
# File 'lib/traductor/glossary.rb', line 20

def to_prompt_text(locale)
  entries = for_locale(locale)
  return nil if entries.empty?

  entries.map { |term, translations|
    "- \"#{term}\" -> \"#{translations[locale.to_s]}\""
  }.join("\n")
end