Class: BetterTranslate::Configuration

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

Overview

Configuration class for BetterTranslate

Manages all configuration options with type safety and validation.

Examples:

Basic configuration

config = Configuration.new
config.provider = :chatgpt
config.openai_key = ENV['OPENAI_API_KEY']
config.source_language = "en"
config.target_languages = [{ short_name: "it", name: "Italian" }]
config.validate!

Provider-specific options

config.model = "gpt-5-nano"           # Specify model (optional, provider-specific)
config.temperature = 0.7              # Control creativity (0.0-2.0, default: 0.3)
config.max_tokens = 1500              # Limit response length (default: 2000)

Backup configuration

config.create_backup = true           # Enable automatic backups (default: true)
config.max_backups = 5                # Keep up to 5 backup files (default: 3)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Initialize a new configuration with defaults



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/better_translate/configuration.rb', line 122

def initialize
  @translation_mode = :override
  @max_concurrent_requests = 3
  @request_timeout = 30
  @max_retries = 3
  @retry_delay = 2.0
  @cache_enabled = true
  @cache_size = 1000
  @cache_ttl = nil
  @verbose = false
  @dry_run = false
  @global_exclusions = []
  @exclusions_per_language = {}
  @target_languages = []
  @preserve_variables = true
  @model = nil
  @temperature = 0.3
  @max_tokens = 2000
  @create_backup = true
  @max_backups = 3
end

Instance Attribute Details

#cache_enabledBoolean

Returns Enable/disable caching.

Returns:

  • (Boolean)

    Enable/disable caching



83
84
85
# File 'lib/better_translate/configuration.rb', line 83

def cache_enabled
  @cache_enabled
end

#cache_sizeInteger

Returns Cache size (LRU capacity).

Returns:

  • (Integer)

    Cache size (LRU capacity)



86
87
88
# File 'lib/better_translate/configuration.rb', line 86

def cache_size
  @cache_size
end

#cache_ttlInteger?

Returns Cache TTL in seconds (nil = no expiration).

Returns:

  • (Integer, nil)

    Cache TTL in seconds (nil = no expiration)



89
90
91
# File 'lib/better_translate/configuration.rb', line 89

def cache_ttl
  @cache_ttl
end

#claude_keyString? Also known as: anthropic_key

Returns Anthropic API key (Claude).

Returns:

  • (String, nil)

    Anthropic API key (Claude)



43
44
45
# File 'lib/better_translate/configuration.rb', line 43

def claude_key
  @claude_key
end

#create_backupBoolean

Returns Create backup files before overwriting (default: true).

Returns:

  • (Boolean)

    Create backup files before overwriting (default: true)



116
117
118
# File 'lib/better_translate/configuration.rb', line 116

def create_backup
  @create_backup
end

#dry_runBoolean

Returns Dry run mode (no files written).

Returns:

  • (Boolean)

    Dry run mode (no files written)



95
96
97
# File 'lib/better_translate/configuration.rb', line 95

def dry_run
  @dry_run
end

#exclusions_per_languageHash

Returns Language-specific exclusions.

Returns:

  • (Hash)

    Language-specific exclusions



101
102
103
# File 'lib/better_translate/configuration.rb', line 101

def exclusions_per_language
  @exclusions_per_language
end

#global_exclusionsArray<String>

Returns Global exclusions (apply to all languages).

Returns:

  • (Array<String>)

    Global exclusions (apply to all languages)



98
99
100
# File 'lib/better_translate/configuration.rb', line 98

def global_exclusions
  @global_exclusions
end

#google_gemini_keyString? Also known as: gemini_key

Returns Google Gemini API key.

Returns:

  • (String, nil)

    Google Gemini API key



36
37
38
# File 'lib/better_translate/configuration.rb', line 36

def google_gemini_key
  @google_gemini_key
end

#input_fileString

Returns Path to input YAML file (for backward compatibility, use input_files for multiple files).

Returns:

  • (String)

    Path to input YAML file (for backward compatibility, use input_files for multiple files)



56
57
58
# File 'lib/better_translate/configuration.rb', line 56

def input_file
  @input_file
end

#input_filesArray<String>, String

Returns Multiple input files (array or glob pattern).

Returns:

  • (Array<String>, String)

    Multiple input files (array or glob pattern)



59
60
61
# File 'lib/better_translate/configuration.rb', line 59

def input_files
  @input_files
end

#max_backupsInteger

Returns Maximum number of backup files to keep (default: 3).

Returns:

  • (Integer)

    Maximum number of backup files to keep (default: 3)



119
120
121
# File 'lib/better_translate/configuration.rb', line 119

def max_backups
  @max_backups
end

#max_concurrent_requestsInteger

Returns Maximum concurrent requests.

Returns:

  • (Integer)

    Maximum concurrent requests



71
72
73
# File 'lib/better_translate/configuration.rb', line 71

def max_concurrent_requests
  @max_concurrent_requests
end

#max_retriesInteger

Returns Maximum number of retries.

Returns:

  • (Integer)

    Maximum number of retries



77
78
79
# File 'lib/better_translate/configuration.rb', line 77

def max_retries
  @max_retries
end

#max_tokensInteger

Returns Maximum tokens for AI response.

Returns:

  • (Integer)

    Maximum tokens for AI response



113
114
115
# File 'lib/better_translate/configuration.rb', line 113

def max_tokens
  @max_tokens
end

#modelString?

Returns AI model to use (provider-specific, e.g., "gpt-5-nano", "gemini-2.0-flash-exp").

Returns:

  • (String, nil)

    AI model to use (provider-specific, e.g., "gpt-5-nano", "gemini-2.0-flash-exp")



107
108
109
# File 'lib/better_translate/configuration.rb', line 107

def model
  @model
end

#openai_keyString?

Returns OpenAI API key.

Returns:

  • (String, nil)

    OpenAI API key



33
34
35
# File 'lib/better_translate/configuration.rb', line 33

def openai_key
  @openai_key
end

#output_folderString

Returns Output folder for translated files.

Returns:

  • (String)

    Output folder for translated files



62
63
64
# File 'lib/better_translate/configuration.rb', line 62

def output_folder
  @output_folder
end

#preserve_variablesBoolean

Returns Preserve interpolation variables during translation (default: true).

Returns:

  • (Boolean)

    Preserve interpolation variables during translation (default: true)



104
105
106
# File 'lib/better_translate/configuration.rb', line 104

def preserve_variables
  @preserve_variables
end

#providerSymbol

Returns The translation provider (:chatgpt, :gemini, :anthropic).

Returns:

  • (Symbol)

    The translation provider (:chatgpt, :gemini, :anthropic)



30
31
32
# File 'lib/better_translate/configuration.rb', line 30

def provider
  @provider
end

#request_timeoutInteger

Returns Request timeout in seconds.

Returns:

  • (Integer)

    Request timeout in seconds



74
75
76
# File 'lib/better_translate/configuration.rb', line 74

def request_timeout
  @request_timeout
end

#retry_delayFloat

Returns Retry delay in seconds.

Returns:

  • (Float)

    Retry delay in seconds



80
81
82
# File 'lib/better_translate/configuration.rb', line 80

def retry_delay
  @retry_delay
end

#source_languageString

Returns Source language code (e.g., "en").

Returns:

  • (String)

    Source language code (e.g., "en")



50
51
52
# File 'lib/better_translate/configuration.rb', line 50

def source_language
  @source_language
end

#target_languagesArray<Hash>

Returns Target languages with :short_name and :name.

Returns:

  • (Array<Hash>)

    Target languages with :short_name and :name



53
54
55
# File 'lib/better_translate/configuration.rb', line 53

def target_languages
  @target_languages
end

#temperatureFloat

Returns Temperature for AI generation (0.0-2.0, higher = more creative).

Returns:

  • (Float)

    Temperature for AI generation (0.0-2.0, higher = more creative)



110
111
112
# File 'lib/better_translate/configuration.rb', line 110

def temperature
  @temperature
end

#translation_contextString?

Returns Translation context for domain-specific terminology.

Returns:

  • (String, nil)

    Translation context for domain-specific terminology



68
69
70
# File 'lib/better_translate/configuration.rb', line 68

def translation_context
  @translation_context
end

#translation_modeSymbol

Returns Translation mode (:override or :incremental).

Returns:

  • (Symbol)

    Translation mode (:override or :incremental)



65
66
67
# File 'lib/better_translate/configuration.rb', line 65

def translation_mode
  @translation_mode
end

#verboseBoolean

Returns Verbose logging.

Returns:

  • (Boolean)

    Verbose logging



92
93
94
# File 'lib/better_translate/configuration.rb', line 92

def verbose
  @verbose
end

Instance Method Details

#create_default_input_file!(file_path) ⇒ void (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Create a default input file with root language key

Parameters:

  • file_path (String)

    Path to the input file



260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/better_translate/configuration.rb', line 260

def create_default_input_file!(file_path)
  # Create directory if needed
  FileUtils.mkdir_p(File.dirname(file_path))

  # Determine file format (YAML or JSON)
  content = if file_path.end_with?(".json")
              JSON.pretty_generate({ source_language => {} })
            else
              { source_language => {} }.to_yaml
            end

  File.write(file_path, content)
end

#validate!true

Validate the configuration

Returns:

  • (true)

    if configuration is valid

Raises:



148
149
150
151
152
153
154
155
# File 'lib/better_translate/configuration.rb', line 148

def validate!
  validate_provider!
  validate_api_keys!
  validate_languages!
  validate_files!
  validate_optional_settings!
  true
end

#validate_api_keys!void (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Validate API keys based on selected provider

Raises:



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/better_translate/configuration.rb', line 174

def validate_api_keys!
  case provider
  when :chatgpt
    if openai_key.nil? || openai_key.empty?
      raise ConfigurationError, "OpenAI API key is required for ChatGPT provider"
    end
  when :gemini
    if google_gemini_key.nil? || google_gemini_key.empty?
      raise ConfigurationError, "Google Gemini API key is required for Gemini provider"
    end
  when :anthropic
    if anthropic_key.nil? || anthropic_key.empty?
      raise ConfigurationError, "Anthropic API key is required for Anthropic provider"
    end
  end
end

#validate_files!void (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Validate file paths

Raises:



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/better_translate/configuration.rb', line 213

def validate_files!
  # Check if either input_file or input_files is set
  has_input = (input_file && !input_file.empty?) || input_files

  raise ConfigurationError, "Input file or input_files must be set" unless has_input
  raise ConfigurationError, "Output folder must be set" if output_folder.nil? || output_folder.empty?

  # Only validate input_file exists if using single file mode (not glob pattern or array)
  return unless input_file && !input_file.empty? && !input_files

  # Create input file if it doesn't exist
  return if File.exist?(input_file)

  create_default_input_file!(input_file)
  puts "Created empty input file: #{input_file}" if verbose
end

#validate_languages!void (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Validate language configuration

Raises:



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/better_translate/configuration.rb', line 196

def validate_languages!
  raise ConfigurationError, "Source language must be set" if source_language.nil? || source_language.empty?
  raise ConfigurationError, "Target languages must be an array" unless target_languages.is_a?(Array)
  raise ConfigurationError, "At least one target language is required" if target_languages.empty?

  target_languages.each do |lang|
    raise ConfigurationError, "Each target language must be a Hash" unless lang.is_a?(Hash)
    raise ConfigurationError, "Target language must have :short_name" unless lang.key?(:short_name)
    raise ConfigurationError, "Target language must have :name" unless lang.key?(:name)
  end
end

#validate_optional_settings!void (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Validate optional settings (timeouts, retries, cache, etc.)

Raises:



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/better_translate/configuration.rb', line 235

def validate_optional_settings!
  valid_modes = i[override incremental]
  unless valid_modes.include?(translation_mode)
    raise ConfigurationError, "Translation mode must be :override or :incremental"
  end

  raise ConfigurationError, "Max concurrent requests must be positive" if max_concurrent_requests <= 0
  raise ConfigurationError, "Request timeout must be positive" if request_timeout <= 0
  raise ConfigurationError, "Max retries must be non-negative" if max_retries.negative?
  raise ConfigurationError, "Cache size must be positive" if cache_size <= 0

  # Validate temperature range (AI providers typically accept 0.0-2.0)
  if temperature && (temperature < 0.0 || temperature > 2.0)
    raise ConfigurationError, "Temperature must be between 0.0 and 2.0"
  end

  # Validate max_tokens is positive
  raise ConfigurationError, "Max tokens must be positive" if max_tokens && max_tokens <= 0
end

#validate_provider!void (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Validate provider configuration

Raises:



164
165
166
167
# File 'lib/better_translate/configuration.rb', line 164

def validate_provider!
  raise ConfigurationError, "Provider must be set" if provider.nil?
  raise ConfigurationError, "Provider must be a Symbol" unless provider.is_a?(Symbol)
end