Class: BetterTranslate::Configuration
- Inherits:
-
Object
- Object
- BetterTranslate::Configuration
- Defined in:
- lib/better_translate/configuration.rb
Overview
Configuration class for BetterTranslate
Manages all configuration options with type safety and validation.
Instance Attribute Summary collapse
-
#cache_enabled ⇒ Boolean
Enable/disable caching.
-
#cache_size ⇒ Integer
Cache size (LRU capacity).
-
#cache_ttl ⇒ Integer?
Cache TTL in seconds (nil = no expiration).
-
#claude_key ⇒ String?
(also: #anthropic_key)
Anthropic API key (Claude).
-
#create_backup ⇒ Boolean
Create backup files before overwriting (default: true).
-
#dry_run ⇒ Boolean
Dry run mode (no files written).
-
#exclusions_per_language ⇒ Hash
Language-specific exclusions.
-
#global_exclusions ⇒ Array<String>
Global exclusions (apply to all languages).
-
#google_gemini_key ⇒ String?
(also: #gemini_key)
Google Gemini API key.
-
#input_file ⇒ String
Path to input YAML file (for backward compatibility, use input_files for multiple files).
-
#input_files ⇒ Array<String>, String
Multiple input files (array or glob pattern).
-
#max_backups ⇒ Integer
Maximum number of backup files to keep (default: 3).
-
#max_concurrent_requests ⇒ Integer
Maximum concurrent requests.
-
#max_retries ⇒ Integer
Maximum number of retries.
-
#max_tokens ⇒ Integer
Maximum tokens for AI response.
-
#model ⇒ String?
AI model to use (provider-specific, e.g., "gpt-5-nano", "gemini-2.0-flash-exp").
-
#openai_key ⇒ String?
OpenAI API key.
-
#output_folder ⇒ String
Output folder for translated files.
-
#preserve_variables ⇒ Boolean
Preserve interpolation variables during translation (default: true).
-
#provider ⇒ Symbol
The translation provider (:chatgpt, :gemini, :anthropic).
-
#request_timeout ⇒ Integer
Request timeout in seconds.
-
#retry_delay ⇒ Float
Retry delay in seconds.
-
#source_language ⇒ String
Source language code (e.g., "en").
-
#target_languages ⇒ Array<Hash>
Target languages with :short_name and :name.
-
#temperature ⇒ Float
Temperature for AI generation (0.0-2.0, higher = more creative).
-
#translation_context ⇒ String?
Translation context for domain-specific terminology.
-
#translation_mode ⇒ Symbol
Translation mode (:override or :incremental).
-
#verbose ⇒ Boolean
Verbose logging.
Instance Method Summary collapse
-
#create_default_input_file!(file_path) ⇒ void
private
private
Create a default input file with root language key.
-
#initialize ⇒ Configuration
constructor
Initialize a new configuration with defaults.
-
#validate! ⇒ true
Validate the configuration.
-
#validate_api_keys! ⇒ void
private
private
Validate API keys based on selected provider.
-
#validate_files! ⇒ void
private
private
Validate file paths.
-
#validate_languages! ⇒ void
private
private
Validate language configuration.
-
#validate_optional_settings! ⇒ void
private
private
Validate optional settings (timeouts, retries, cache, etc.).
-
#validate_provider! ⇒ void
private
private
Validate provider configuration.
Constructor Details
#initialize ⇒ Configuration
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_enabled ⇒ Boolean
Returns Enable/disable caching.
83 84 85 |
# File 'lib/better_translate/configuration.rb', line 83 def cache_enabled @cache_enabled end |
#cache_size ⇒ Integer
Returns Cache size (LRU capacity).
86 87 88 |
# File 'lib/better_translate/configuration.rb', line 86 def cache_size @cache_size end |
#cache_ttl ⇒ Integer?
Returns 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_key ⇒ String? Also known as: anthropic_key
Returns Anthropic API key (Claude).
43 44 45 |
# File 'lib/better_translate/configuration.rb', line 43 def claude_key @claude_key end |
#create_backup ⇒ Boolean
Returns 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_run ⇒ Boolean
Returns 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_language ⇒ Hash
Returns Language-specific exclusions.
101 102 103 |
# File 'lib/better_translate/configuration.rb', line 101 def exclusions_per_language @exclusions_per_language end |
#global_exclusions ⇒ Array<String>
Returns 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_key ⇒ String? Also known as: gemini_key
Returns Google Gemini API key.
36 37 38 |
# File 'lib/better_translate/configuration.rb', line 36 def google_gemini_key @google_gemini_key end |
#input_file ⇒ String
Returns 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_files ⇒ Array<String>, String
Returns 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_backups ⇒ Integer
Returns 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_requests ⇒ Integer
Returns Maximum concurrent requests.
71 72 73 |
# File 'lib/better_translate/configuration.rb', line 71 def max_concurrent_requests @max_concurrent_requests end |
#max_retries ⇒ Integer
Returns Maximum number of retries.
77 78 79 |
# File 'lib/better_translate/configuration.rb', line 77 def max_retries @max_retries end |
#max_tokens ⇒ Integer
Returns Maximum tokens for AI response.
113 114 115 |
# File 'lib/better_translate/configuration.rb', line 113 def max_tokens @max_tokens end |
#model ⇒ String?
Returns 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_key ⇒ String?
Returns OpenAI API key.
33 34 35 |
# File 'lib/better_translate/configuration.rb', line 33 def openai_key @openai_key end |
#output_folder ⇒ String
Returns Output folder for translated files.
62 63 64 |
# File 'lib/better_translate/configuration.rb', line 62 def output_folder @output_folder end |
#preserve_variables ⇒ Boolean
Returns Preserve interpolation variables during translation (default: true).
104 105 106 |
# File 'lib/better_translate/configuration.rb', line 104 def preserve_variables @preserve_variables end |
#provider ⇒ Symbol
Returns The translation provider (:chatgpt, :gemini, :anthropic).
30 31 32 |
# File 'lib/better_translate/configuration.rb', line 30 def provider @provider end |
#request_timeout ⇒ Integer
Returns Request timeout in seconds.
74 75 76 |
# File 'lib/better_translate/configuration.rb', line 74 def request_timeout @request_timeout end |
#retry_delay ⇒ Float
Returns Retry delay in seconds.
80 81 82 |
# File 'lib/better_translate/configuration.rb', line 80 def retry_delay @retry_delay end |
#source_language ⇒ String
Returns Source language code (e.g., "en").
50 51 52 |
# File 'lib/better_translate/configuration.rb', line 50 def source_language @source_language end |
#target_languages ⇒ Array<Hash>
Returns Target languages with :short_name and :name.
53 54 55 |
# File 'lib/better_translate/configuration.rb', line 53 def target_languages @target_languages end |
#temperature ⇒ Float
Returns 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_context ⇒ String?
Returns Translation context for domain-specific terminology.
68 69 70 |
# File 'lib/better_translate/configuration.rb', line 68 def translation_context @translation_context end |
#translation_mode ⇒ Symbol
Returns Translation mode (:override or :incremental).
65 66 67 |
# File 'lib/better_translate/configuration.rb', line 65 def translation_mode @translation_mode end |
#verbose ⇒ Boolean
Returns 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
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
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
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
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
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.)
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
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 |