Class: Locallingo::Configuration
- Inherits:
-
Object
- Object
- Locallingo::Configuration
- 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.("../../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
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#package ⇒ Object
readonly
Returns the value of attribute package.
-
#root_path ⇒ Object
readonly
Returns the value of attribute root_path.
Class Method Summary collapse
-
.load(root_path: Dir.pwd, package: nil) ⇒ Object
Resolve config for
root_path, optionally scoped to apackagepath from thepackages:list.
Instance Method Summary collapse
-
#after_translate ⇒ Object
--- hooks ---------------------------------------------------------------.
- #all_locales ⇒ Object
-
#base_path ⇒ Object
The directory config paths resolve against — the package path when scoped, else the app root.
- #batch_size ⇒ Object
- #british_spellings? ⇒ Boolean
-
#context ⇒ Object
--- prompt scaffolding --------------------------------------------------.
- #exceptions_dir ⇒ Object
- #glossary ⇒ Object
-
#initialize(root_path: Dir.pwd, package: nil, data: nil) ⇒ Configuration
constructor
A new instance of Configuration.
-
#language_guide(locale) ⇒ Object
Extra per-locale style text appended to the translation prompt.
-
#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.
- #locales_dir ⇒ Object
- #log_file ⇒ Object
- #placeholder_style ⇒ Object
-
#provider ⇒ Object
--- provider / models ---------------------------------------------------.
- #quality_model ⇒ Object
- #source_locale ⇒ Object
- #state_dir ⇒ Object
-
#strict_types(tier) ⇒ Object
Violation types (symbols) a strict tier fails on.
- #target_locales ⇒ Object
- #terminology_setting ⇒ Object
- #translate_model ⇒ Object
-
#validator_enabled?(name) ⇒ Boolean
--- validators / strictness ---------------------------------------------.
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.(root_path) @package = package @data = data || resolve end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
21 22 23 |
# File 'lib/locallingo/configuration.rb', line 21 def data @data end |
#package ⇒ Object (readonly)
Returns the value of attribute package.
21 22 23 |
# File 'lib/locallingo/configuration.rb', line 21 def package @package end |
#root_path ⇒ Object (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_translate ⇒ Object
--- hooks ---------------------------------------------------------------
99 |
# File 'lib/locallingo/configuration.rb', line 99 def after_translate = Array(data.fetch("after_translate", [])) |
#all_locales ⇒ Object
45 |
# File 'lib/locallingo/configuration.rb', line 45 def all_locales = [source_locale, *target_locales].uniq |
#base_path ⇒ Object
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_size ⇒ Object
56 |
# File 'lib/locallingo/configuration.rb', line 56 def batch_size = dig("translate", "batch_size") |
#british_spellings? ⇒ Boolean
58 |
# File 'lib/locallingo/configuration.rb', line 58 def british_spellings? = !!dig("quality", "british_spellings") |
#context ⇒ Object
--- prompt scaffolding --------------------------------------------------
63 |
# File 'lib/locallingo/configuration.rb', line 63 def context = data.fetch("context", "a business application") |
#exceptions_dir ⇒ Object
49 |
# File 'lib/locallingo/configuration.rb', line 49 def exceptions_dir = File.join(state_dir, "exceptions") |
#glossary ⇒ Object
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.(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_dir ⇒ Object
47 |
# File 'lib/locallingo/configuration.rb', line 47 def locales_dir = File.join(base_path, data.fetch("locales_dir")) |
#log_file ⇒ Object
50 |
# File 'lib/locallingo/configuration.rb', line 50 def log_file = File.join(state_dir, "translation.log") |
#placeholder_style ⇒ Object
64 |
# File 'lib/locallingo/configuration.rb', line 64 def placeholder_style = data.fetch("placeholder_style", "%<name>s, %<count>s") |
#provider ⇒ Object
--- provider / models ---------------------------------------------------
54 |
# File 'lib/locallingo/configuration.rb', line 54 def provider = data.fetch("provider").to_sym |
#quality_model ⇒ Object
57 |
# File 'lib/locallingo/configuration.rb', line 57 def quality_model = dig("quality", "model") |
#source_locale ⇒ Object
43 |
# File 'lib/locallingo/configuration.rb', line 43 def source_locale = data.fetch("source_locale") |
#state_dir ⇒ Object
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_locales ⇒ Object
44 |
# File 'lib/locallingo/configuration.rb', line 44 def target_locales = Array(data.fetch("target_locales")) |
#terminology_setting ⇒ Object
59 |
# File 'lib/locallingo/configuration.rb', line 59 def terminology_setting = dig("quality", "terminology") |
#translate_model ⇒ Object
55 |
# File 'lib/locallingo/configuration.rb', line 55 def translate_model = dig("translate", "model") |
#validator_enabled?(name) ⇒ Boolean
--- validators / strictness ---------------------------------------------
87 88 89 |
# File 'lib/locallingo/configuration.rb', line 87 def validator_enabled?(name) !!dig("validators", name.to_s) end |