Class: LlmTranslate::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_translate/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path, cli_options = {}) ⇒ Config

Returns a new instance of Config.



13
14
15
16
17
18
# File 'lib/llm_translate/config.rb', line 13

def initialize(config_path, cli_options = {})
  @cli_options = cli_options
  @data = load_config_file(config_path)
  apply_cli_overrides
  validate_config
end

Instance Attribute Details

#cli_optionsObject (readonly)

Returns the value of attribute cli_options.



7
8
9
# File 'lib/llm_translate/config.rb', line 7

def cli_options
  @cli_options
end

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/llm_translate/config.rb', line 7

def data
  @data
end

Class Method Details

.load(config_path, cli_options = {}) ⇒ Object



9
10
11
# File 'lib/llm_translate/config.rb', line 9

def self.load(config_path, cli_options = {})
  new(config_path, cli_options)
end

Instance Method Details

#ai_hostObject



25
26
27
# File 'lib/llm_translate/config.rb', line 25

def ai_host
  resolve_env_var(data.dig('ai', 'host')) || 'https://aihubmix.com'
end

#ai_modelObject



33
34
35
# File 'lib/llm_translate/config.rb', line 33

def ai_model
  data.dig('ai', 'model') || 'claude-3-7-sonnet-20250219'
end

#ai_providerObject



29
30
31
# File 'lib/llm_translate/config.rb', line 29

def ai_provider
  data.dig('ai', 'provider') || 'openai'
end

#api_keyObject

AI Configuration



21
22
23
# File 'lib/llm_translate/config.rb', line 21

def api_key
  resolve_env_var(data.dig('ai', 'api_key'))
end

#concurrent_chunksObject



91
92
93
# File 'lib/llm_translate/config.rb', line 91

def concurrent_chunks
  data.dig('translation', 'concurrent_chunks') || 3
end

#concurrent_filesObject

Performance Configuration



184
185
186
# File 'lib/llm_translate/config.rb', line 184

def concurrent_files
  data.dig('performance', 'concurrent_files') || 3
end

#default_promptObject

Translation Configuration



58
59
60
# File 'lib/llm_translate/config.rb', line 58

def default_prompt
  cli_options[:prompt] || data.dig('translation', 'default_prompt') || default_translation_prompt
end

#enable_document_splitting?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/llm_translate/config.rb', line 87

def enable_document_splitting?
  data.dig('translation', 'enable_splitting') != false
end

#exclude_patternsObject



137
138
139
# File 'lib/llm_translate/config.rb', line 137

def exclude_patterns
  data.dig('files', 'exclude_patterns') || []
end

#filename_strategyObject



125
126
127
# File 'lib/llm_translate/config.rb', line 125

def filename_strategy
  data.dig('files', 'filename_strategy') || 'suffix'
end

#filename_suffixObject



129
130
131
# File 'lib/llm_translate/config.rb', line 129

def filename_suffix
  data.dig('files', 'filename_suffix') || '.zh'
end

#generate_error_report?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/llm_translate/config.rb', line 175

def generate_error_report?
  data.dig('error_handling', 'generate_error_report') != false
end

#generate_report?Boolean

Output Configuration

Returns:

  • (Boolean)


193
194
195
# File 'lib/llm_translate/config.rb', line 193

def generate_report?
  data.dig('output', 'generate_report') != false
end

#include_patternsObject



133
134
135
# File 'lib/llm_translate/config.rb', line 133

def include_patterns
  data.dig('files', 'include_patterns') || ['**/*.md', '**/*.markdown']
end

#input_directoryObject

File Configuration



96
97
98
# File 'lib/llm_translate/config.rb', line 96

def input_directory
  cli_options[:input] || data.dig('files', 'input_directory') || './docs'
end

#input_fileObject



104
105
106
107
108
# File 'lib/llm_translate/config.rb', line 104

def input_file
  return cli_options[:input] if cli_options[:input]

  data.dig('files', 'input_file')
end

#log_levelObject

Logging Configuration



150
151
152
# File 'lib/llm_translate/config.rb', line 150

def log_level
  cli_options[:verbose] ? 'debug' : (data.dig('logging', 'level') || 'info')
end

#log_outputObject



154
155
156
# File 'lib/llm_translate/config.rb', line 154

def log_output
  data.dig('logging', 'output') || 'console'
end

#max_chars_for_splittingObject

Document Splitting Configuration



79
80
81
# File 'lib/llm_translate/config.rb', line 79

def max_chars_for_splitting
  data.dig('translation', 'max_chars') || 20_000
end

#max_consecutive_errorsObject



167
168
169
# File 'lib/llm_translate/config.rb', line 167

def max_consecutive_errors
  data.dig('error_handling', 'max_consecutive_errors') || 5
end

#max_tokensObject



41
42
43
# File 'lib/llm_translate/config.rb', line 41

def max_tokens
  data.dig('ai', 'max_tokens') || 40_000
end

#on_errorObject

Error Handling Configuration



163
164
165
# File 'lib/llm_translate/config.rb', line 163

def on_error
  data.dig('error_handling', 'on_error') || 'log_and_continue'
end

#output_directoryObject



100
101
102
# File 'lib/llm_translate/config.rb', line 100

def output_directory
  cli_options[:output] || data.dig('files', 'output_directory') || './docs-translated'
end

#output_fileObject



110
111
112
113
114
# File 'lib/llm_translate/config.rb', line 110

def output_file
  return cli_options[:output] if cli_options[:input] && cli_options[:output]

  data.dig('files', 'output_file')
end

#overwrite_policyObject



145
146
147
# File 'lib/llm_translate/config.rb', line 145

def overwrite_policy
  data.dig('files', 'overwrite_policy') || 'ask'
end

#preserve_directory_structure?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/llm_translate/config.rb', line 141

def preserve_directory_structure?
  data.dig('files', 'preserve_directory_structure') != false
end

#preserve_formatting?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/llm_translate/config.rb', line 70

def preserve_formatting?
  data.dig('translation', 'preserve_formatting') != false
end

#report_pathObject



197
198
199
# File 'lib/llm_translate/config.rb', line 197

def report_path
  data.dig('output', 'report_path') || './reports/translation_report.md'
end

#request_intervalObject



188
189
190
# File 'lib/llm_translate/config.rb', line 188

def request_interval
  data.dig('performance', 'request_interval') || 1
end

#retry_attemptsObject



45
46
47
# File 'lib/llm_translate/config.rb', line 45

def retry_attempts
  data.dig('ai', 'retry_attempts') || 3
end

#retry_delayObject



49
50
51
# File 'lib/llm_translate/config.rb', line 49

def retry_delay
  data.dig('ai', 'retry_delay') || 2
end

#retry_on_failureObject



171
172
173
# File 'lib/llm_translate/config.rb', line 171

def retry_on_failure
  data.dig('error_handling', 'retry_on_failure') || 2
end

#should_stop_on_error?(error_count) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/llm_translate/config.rb', line 179

def should_stop_on_error?(error_count)
  on_error == 'stop' || error_count >= max_consecutive_errors
end

#single_file_mode?Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
122
123
# File 'lib/llm_translate/config.rb', line 116

def single_file_mode?
  input_file_path = input_file
  output_file_path = output_file

  # Both must be present and input must be a file (not directory) for single file mode
  !input_file_path.nil? && !output_file_path.nil? &&
    File.exist?(input_file_path) && File.file?(input_file_path)
end

#source_languageObject



66
67
68
# File 'lib/llm_translate/config.rb', line 66

def source_language
  data.dig('translation', 'source_language') || 'auto'
end

#split_every_charsObject



83
84
85
# File 'lib/llm_translate/config.rb', line 83

def split_every_chars
  data.dig('translation', 'every_chars') || 20_000
end

#target_languageObject



62
63
64
# File 'lib/llm_translate/config.rb', line 62

def target_language
  data.dig('translation', 'target_language') || 'zh-CN'
end

#temperatureObject



37
38
39
# File 'lib/llm_translate/config.rb', line 37

def temperature
  data.dig('ai', 'temperature') || 0.3
end

#timeoutObject



53
54
55
# File 'lib/llm_translate/config.rb', line 53

def timeout
  data.dig('ai', 'timeout') || 60
end

#translate_code_comments?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/llm_translate/config.rb', line 74

def translate_code_comments?
  data.dig('translation', 'translate_code_comments') == true
end

#verbose_translation?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/llm_translate/config.rb', line 158

def verbose_translation?
  cli_options[:verbose] || data.dig('logging', 'verbose_translation') == true
end