Class: Traductor::Glossary
- Inherits:
-
Object
- Object
- Traductor::Glossary
- Defined in:
- lib/traductor/glossary.rb
Instance Attribute Summary collapse
-
#terms ⇒ Object
readonly
Returns the value of attribute terms.
Instance Method Summary collapse
-
#for_locale(locale) ⇒ Object
Returns terms relevant to a specific target locale.
-
#initialize(path = nil) ⇒ Glossary
constructor
A new instance of Glossary.
-
#to_prompt_text(locale) ⇒ Object
Formats glossary entries for inclusion in prompts.
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 |