Class: LcpRuby::I18nCheck::Configuration

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

Overview

Configuration for the i18n_check lint. Hosts override defaults via

LcpRuby.configuration.i18n_check = { locales: [:en, :cs], severity: :error, ... }

in ‘config/initializers/lcp_ruby.rb`.

Constant Summary collapse

DEFAULT_PATHS =
[ "config/lcp_ruby" ].freeze
DEFAULT_EXCLUDES =
[ "**/fixtures/**", "**/tmp/**" ].freeze
VALID_SEVERITIES =
%i[error warning info off].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(severity: :warning, severity_per_kind: {}, paths: nil, exclude: [], format: :text, flag_pure_duplicates: false, literal_as_default_locale: false, locales: nil) ⇒ Configuration

Returns a new instance of Configuration.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lcp_ruby/i18n_check/configuration.rb', line 14

def initialize(
  severity: :warning,
  severity_per_kind: {},
  paths: nil,
  exclude: [],
  format: :text,
  flag_pure_duplicates: false,
  literal_as_default_locale: false,
  locales: nil
)
  @severity             = normalize_severity(severity)
  @severity_per_kind    = severity_per_kind.each_with_object({}) do |(k, v), h|
    h[k.to_sym] = normalize_severity(v)
  end
  @paths                = Array(paths || DEFAULT_PATHS)
  @exclude              = (DEFAULT_EXCLUDES + Array(exclude)).uniq
  @format               = format.to_sym
  # When false (default), a literal label that matches the runtime
  # humanize fallback AND is missing from every locale is reported
  # as `:missing_translation` (more informative). When true, a
  # literal that pairs with all-locales-present is also flagged
  # — useful when the host wants to remove redundant DSL literals
  # in favour of locale-only definitions.
  @flag_pure_duplicates = !!flag_pure_duplicates
  # When true, treat any non-empty DSL literal as the implicit
  # `I18n.default_locale` value: the lint excludes the default
  # locale from "missing" before deciding offense severity. Net
  # effect: `label "Stage"` without any locale entries is silent
  # in a single-locale app (literal IS the en value the runtime
  # would render); a missing `:cs` entry still fires
  # `:missing_translation`. Trades the strict
  # "locale files = source of truth" principle for ergonomics on
  # single-locale apps; multi-locale apps with translator workflows
  # should keep this `false`. See `CLAUDE.md` § "i18n Principle".
  @literal_as_default_locale = !!literal_as_default_locale
  @locales              = locales.nil? ? nil : Array(locales).map(&:to_sym)
end

Instance Attribute Details

#excludeObject (readonly)

Returns the value of attribute exclude.



11
12
13
# File 'lib/lcp_ruby/i18n_check/configuration.rb', line 11

def exclude
  @exclude
end

#flag_pure_duplicatesObject (readonly)

Returns the value of attribute flag_pure_duplicates.



11
12
13
# File 'lib/lcp_ruby/i18n_check/configuration.rb', line 11

def flag_pure_duplicates
  @flag_pure_duplicates
end

#formatObject (readonly)

Returns the value of attribute format.



11
12
13
# File 'lib/lcp_ruby/i18n_check/configuration.rb', line 11

def format
  @format
end

#literal_as_default_localeObject (readonly)

Returns the value of attribute literal_as_default_locale.



11
12
13
# File 'lib/lcp_ruby/i18n_check/configuration.rb', line 11

def literal_as_default_locale
  @literal_as_default_locale
end

#pathsObject (readonly)

Returns the value of attribute paths.



11
12
13
# File 'lib/lcp_ruby/i18n_check/configuration.rb', line 11

def paths
  @paths
end

#severityObject (readonly)

Returns the value of attribute severity.



11
12
13
# File 'lib/lcp_ruby/i18n_check/configuration.rb', line 11

def severity
  @severity
end

#severity_per_kindObject (readonly)

Returns the value of attribute severity_per_kind.



11
12
13
# File 'lib/lcp_ruby/i18n_check/configuration.rb', line 11

def severity_per_kind
  @severity_per_kind
end

Instance Method Details

#localesObject

Resolved locales for the lint. Defaults to ‘I18n.available_locales` at call time so the value reflects the host app’s runtime config rather than freezing whatever was set when this Configuration was built.

Emits a one-time stderr warning when falling back to ‘available_locales` because a real host (with rails-i18n loaded) usually has 30+ locales — the lint output becomes noise (`missing in: en,ar,de,es,…,zh-TW`) unless the host explicitly narrows it down to what they actually ship.



62
63
64
65
66
67
68
69
70
# File 'lib/lcp_ruby/i18n_check/configuration.rb', line 62

def locales
  return @locales if @locales
  warn_unconfigured_locales!
  if defined?(I18n) && I18n.respond_to?(:available_locales)
    I18n.available_locales.map(&:to_sym)
  else
    [ :en ]
  end
end

#severity_for(kind) ⇒ Object

Per-kind severity falls back to the top-level :severity.



73
74
75
# File 'lib/lcp_ruby/i18n_check/configuration.rb', line 73

def severity_for(kind)
  @severity_per_kind[kind] || @severity
end