Class: Kotoshu::Cli::LanguageResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/cli/language_resolver.rb

Overview

Resolves which language a check should run in.

The CLI’s –language flag accepts:

- omitted / "auto"   → detect from content, fall back if needed
- "default"          → Configuration.default_language (no detection)
- any other code     → used as-is

Pure logic — no IO. Detection delegates to Kotoshu::Language.detect which uses character-set heuristics and needs no model download. A detection only “sticks” if the detected language is set up (Kotoshu.setup? returns true); otherwise the configured default is used and a fallback note is included in the result.

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(flag_value:, default_language:, detector: Kotoshu::Language, setup_predicate: Kotoshu.method(:setup?)) ⇒ LanguageResolver

Returns a new instance of LanguageResolver.

Parameters:

  • flag_value (String, nil)

    Raw –language flag value.

  • default_language (String, nil)

    Configuration.default_language.

  • detector (#detect) (defaults to: Kotoshu::Language)

    Object responding to .detect(text) -> String|nil. Defaults to Kotoshu::Language.

  • setup_predicate (#call) (defaults to: Kotoshu.method(:setup?))

    Callable returning true if a language is set up. Defaults to Kotoshu.method(:setup?).



26
27
28
29
30
31
32
33
# File 'lib/kotoshu/cli/language_resolver.rb', line 26

def initialize(flag_value:, default_language:,
               detector: Kotoshu::Language,
               setup_predicate: Kotoshu.method(:setup?))
  @flag_value = flag_value
  @default_language = default_language
  @detector = detector
  @setup_predicate = setup_predicate
end

Instance Method Details

#resolve(text:) ⇒ Result

Parameters:

  • text (String)

    The document text, used only when flag is “auto”.

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kotoshu/cli/language_resolver.rb', line 37

def resolve(text:)
  case @flag_value
  when nil, "auto"
    resolve_auto(text)
  when "default"
    Result.new(language: @default_language, detected: nil, fallback: nil,
               note: nil)
  else
    Result.new(language: @flag_value, detected: nil, fallback: nil,
               note: nil)
  end
end