Class: Locallingo::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/locallingo/configuration.rb

Overview

Loads and resolves .locallingo.yml.

The file has a defaults: block that applies to the whole app, plus an optional packages: list. Each package entry deep-merges onto defaults, so a package only lists the keys it changes. With no packages: (the common case), the resolved config is just defaults merged over the shipped config/locallingo.default.yml.

Typed readers expose everything the Manager/QualityChecker/CLI need, so no other class parses raw config hashes.

Constant Summary collapse

CONFIG_FILENAMES =
%w[.locallingo.yml .locallingo.yaml].freeze
DEFAULT_CONFIG_PATH =
File.expand_path("../../config/locallingo.default.yml", __dir__)
BUILTIN_LANGUAGE_NAMES =
{
  "de" => "German", "sv" => "Swedish", "fr" => "French",
  "af" => "Afrikaans", "es" => "Spanish", "it" => "Italian",
  "nl" => "Dutch", "pt" => "Portuguese", "da" => "Danish",
  "nb" => "Norwegian", "no" => "Norwegian", "fi" => "Finnish"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path: Dir.pwd, package: nil, data: nil) ⇒ Configuration

Returns a new instance of Configuration.



29
30
31
32
33
# File 'lib/locallingo/configuration.rb', line 29

def initialize(root_path: Dir.pwd, package: nil, data: nil)
  @root_path = File.expand_path(root_path)
  @package = package
  @data = data || resolve
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



21
22
23
# File 'lib/locallingo/configuration.rb', line 21

def data
  @data
end

#packageObject (readonly)

Returns the value of attribute package.



21
22
23
# File 'lib/locallingo/configuration.rb', line 21

def package
  @package
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



21
22
23
# File 'lib/locallingo/configuration.rb', line 21

def root_path
  @root_path
end

Class Method Details

.load(root_path: Dir.pwd, package: nil) ⇒ Object

Resolve config for root_path, optionally scoped to a package path from the packages: list.



25
26
27
# File 'lib/locallingo/configuration.rb', line 25

def self.load(root_path: Dir.pwd, package: nil)
  new(root_path:, package:)
end

Instance Method Details

#after_translateObject

--- hooks ---------------------------------------------------------------



99
# File 'lib/locallingo/configuration.rb', line 99

def after_translate = Array(data.fetch("after_translate", []))

#all_localesObject



45
# File 'lib/locallingo/configuration.rb', line 45

def all_locales = [source_locale, *target_locales].uniq

#base_pathObject

The directory config paths resolve against — the package path when scoped, else the app root.



39
40
41
# File 'lib/locallingo/configuration.rb', line 39

def base_path
  package ? File.join(root_path, package) : root_path
end

#batch_sizeObject



56
# File 'lib/locallingo/configuration.rb', line 56

def batch_size = dig("translate", "batch_size")

#british_spellings?Boolean

Returns:

  • (Boolean)


58
# File 'lib/locallingo/configuration.rb', line 58

def british_spellings? = !!dig("quality", "british_spellings")

#contextObject

--- prompt scaffolding --------------------------------------------------



63
# File 'lib/locallingo/configuration.rb', line 63

def context = data.fetch("context", "a business application")

#exceptions_dirObject



49
# File 'lib/locallingo/configuration.rb', line 49

def exceptions_dir = File.join(state_dir, "exceptions")

#glossaryObject



65
# File 'lib/locallingo/configuration.rb', line 65

def glossary = data.fetch("glossary", {}) || {}

#language_guide(locale) ⇒ Object

Extra per-locale style text appended to the translation prompt. A value of { "file" => path } is read from disk (relative to base_path); a String is used verbatim. Returns "" when none is configured.



70
71
72
73
74
75
76
77
# File 'lib/locallingo/configuration.rb', line 70

def language_guide(locale)
  guide = (data.fetch("language_guides", {}) || {})[locale.to_s]
  return "" if guide.nil?
  return guide.to_s unless guide.is_a?(Hash) && guide["file"]

  path = File.expand_path(guide["file"], base_path)
  File.exist?(path) ? File.read(path) : ""
end

#language_name(locale) ⇒ Object

Human-readable language name for a locale, from the guide config or a small built-in map, falling back to the locale code itself.



81
82
83
# File 'lib/locallingo/configuration.rb', line 81

def language_name(locale)
  BUILTIN_LANGUAGE_NAMES[locale.to_s] || locale.to_s
end

#locales_dirObject



47
# File 'lib/locallingo/configuration.rb', line 47

def locales_dir = File.join(base_path, data.fetch("locales_dir"))

#log_fileObject



50
# File 'lib/locallingo/configuration.rb', line 50

def log_file = File.join(state_dir, "translation.log")

#placeholder_styleObject



64
# File 'lib/locallingo/configuration.rb', line 64

def placeholder_style = data.fetch("placeholder_style", "%<name>s, %<count>s")

#providerObject

--- provider / models ---------------------------------------------------



54
# File 'lib/locallingo/configuration.rb', line 54

def provider = data.fetch("provider").to_sym

#quality_modelObject



57
# File 'lib/locallingo/configuration.rb', line 57

def quality_model = dig("quality", "model")

#source_localeObject



43
# File 'lib/locallingo/configuration.rb', line 43

def source_locale = data.fetch("source_locale")

#state_dirObject



48
# File 'lib/locallingo/configuration.rb', line 48

def state_dir = File.join(base_path, data.fetch("state_dir"))

#strict_types(tier) ⇒ Object

Violation types (symbols) a strict tier fails on. tier is :strict or :strict_all.



93
94
95
# File 'lib/locallingo/configuration.rb', line 93

def strict_types(tier)
  Array(dig("strict", tier.to_s)).map(&:to_sym)
end

#target_localesObject



44
# File 'lib/locallingo/configuration.rb', line 44

def target_locales = Array(data.fetch("target_locales"))

#terminology_settingObject



59
# File 'lib/locallingo/configuration.rb', line 59

def terminology_setting = dig("quality", "terminology")

#translate_modelObject



55
# File 'lib/locallingo/configuration.rb', line 55

def translate_model = dig("translate", "model")

#validator_enabled?(name) ⇒ Boolean

--- validators / strictness ---------------------------------------------

Returns:

  • (Boolean)


87
88
89
# File 'lib/locallingo/configuration.rb', line 87

def validator_enabled?(name)
  !!dig("validators", name.to_s)
end