better_translate 0.4.2 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 04ce5f4b9e73378af8eec2409688e26dfef6fb42268b8817800c8c3433877eaf
4
- data.tar.gz: 0b34308c1ef74dd3ad74b8cbc0b546cf831f2e43c3f3e2628cb636511ab33289
3
+ metadata.gz: '08c6af60f2eb66076d7f9fe0e603d13ae497f637380fba4a261961d570097312'
4
+ data.tar.gz: 7973a4254e17ae0a714e7fea96124916ffd27539e1610ef9ad1409627854af3b
5
5
  SHA512:
6
- metadata.gz: e63e571fa9f0a881c25d35d4cb31f5cfaa0fbed2e84d3847e50f9b6f811685b5a13666408b48f0e61666232916c8cbf803dda11a6e51bcca9813f1e97b696435
7
- data.tar.gz: 94f9664d32402395d9a575c2e1ae8f04b29781421fd7a4b266ae7bf0c7a2c21d345ab73762397d50028d01482255b0a10319c4799c25056256c39e0d43269df0
6
+ metadata.gz: 0bee7264dfe205b313a3326602c81590cda09ab283fc6a98145452c14c8d4ef4f756f1a2cfae6836c88a8fb9240f35549b7a8dc4ffdeb782d7fcfbc34d738bb0
7
+ data.tar.gz: ac9dc01081105b202448ac4dd0cec510b8f7bff7eb72426a009650a756cd9ff74ca97b2916fa569f14569f9963d3ce0798f7dadbfc1b7411e51ea975b645dd0a
data/CHANGELOG.md CHANGED
@@ -5,6 +5,24 @@ All notable changes to BetterTranslate will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.5.0] - 2025-03-12
9
+
10
+ ### Added
11
+ - Custom Translation Provider support:
12
+ - New generator: `rails generate better_translate:provider YourProviderName`
13
+ - Provider registration system via `BetterTranslate::Service.register_provider`
14
+ - Dynamic API key configuration for custom providers
15
+ - Comprehensive documentation and examples for creating custom providers
16
+ - Template files with implementation instructions
17
+ - Enhanced Service class:
18
+ - Provider registry for managing custom providers
19
+ - Improved error handling with better error messages
20
+ - Method to list all available providers
21
+ - Updated documentation:
22
+ - New section in README for custom providers
23
+ - Step-by-step guide for implementing custom translation services
24
+ - Example implementation for DeepL integration
25
+
8
26
  ## [0.4.2] - 2025-03-11
9
27
 
10
28
  ### Added
data/README.md CHANGED
@@ -19,7 +19,7 @@ BetterTranslate simplifies the translation of YAML files in your Ruby/Rails appl
19
19
 
20
20
  ## Why BetterTranslate? 🤔
21
21
 
22
- - 🌐 **AI-Powered Translation**: Leverage ChatGPT and Google Gemini for high-quality translations
22
+ - 🌐 **AI-Powered Translation**: Leverage ChatGPT, Google Gemini, and custom providers for high-quality translations
23
23
  - 🔄 **Smart Translation Modes**:
24
24
  - `Override`: Full file rewrite
25
25
  - `Incremental`: Update only new/modified keys
@@ -29,6 +29,7 @@ BetterTranslate simplifies the translation of YAML files in your Ruby/Rails appl
29
29
  - Dot notation support
30
30
  - 🛠 **Developer-Friendly**:
31
31
  - Rails generators included
32
+ - Custom provider support
32
33
  - Comprehensive test suite
33
34
  - LRU caching for performance
34
35
  - Progress tracking
@@ -47,7 +48,7 @@ BetterTranslate simplifies the translation of YAML files in your Ruby/Rails appl
47
48
  Add the gem to your Gemfile:
48
49
 
49
50
  ```ruby
50
- gem 'better_translate', '~> 0.4.2'
51
+ gem 'better_translate', '~> 0.5.0'
51
52
  ```
52
53
 
53
54
  Then run:
@@ -159,6 +160,12 @@ The gem includes generators to simplify tasks:
159
160
  rails generate better_translate:analyze
160
161
  ```
161
162
 
163
+ - **Create Custom Provider:**
164
+
165
+ ```bash
166
+ rails generate better_translate:provider YourProviderName
167
+ ```
168
+
162
169
  The `better_translate:analyze` generator will:
163
170
  - Scan all YAML files in your locales directory
164
171
  - Find similar translations using Levenshtein distance
@@ -172,6 +179,76 @@ The gem includes generators to simplify tasks:
172
179
  - Optimize your translation files
173
180
  - Reduce translation costs
174
181
 
182
+ ### Custom Translation Providers
183
+
184
+ BetterTranslate allows you to create and use custom translation providers, enabling integration with any translation API or service. This feature is particularly useful if you want to use a translation service not natively supported by BetterTranslate, such as DeepL, Microsoft Translator, Amazon Translate, or any other API-based translation service.
185
+
186
+ 1. **Generate a custom provider template:**
187
+
188
+ ```bash
189
+ rails generate better_translate:provider DeepL
190
+ ```
191
+
192
+ This creates a new provider class in `app/providers/deep_l_provider.rb` and a README with implementation instructions.
193
+
194
+ 2. **Implement the translation logic:**
195
+
196
+ Edit the generated provider file to implement the `translate_text` method with your API-specific code:
197
+
198
+ ```ruby
199
+ # app/providers/deep_l_provider.rb
200
+ module Providers
201
+ class DeepLProvider < BetterTranslate::Providers::BaseProvider
202
+ def initialize(api_key)
203
+ @api_key = api_key
204
+ end
205
+
206
+ def translate_text(text, target_lang_code, target_lang_name)
207
+ # Implement your API call to DeepL here
208
+ # Return the translated text as a string
209
+ end
210
+ end
211
+ end
212
+ ```
213
+
214
+ 3. **Register your provider:**
215
+
216
+ Create an initializer to register your custom provider:
217
+
218
+ ```ruby
219
+ # config/initializers/better_translate_providers.rb
220
+ # Require the provider file to ensure it's loaded
221
+ require Rails.root.join('app', 'providers', 'deep_l_provider')
222
+
223
+ BetterTranslate::Service.register_provider(
224
+ :deepl,
225
+ ->(api_key) { Providers::DeepLProvider.new(api_key) }
226
+ )
227
+ ```
228
+
229
+ Note: The `require` statement is important to ensure the provider class is loaded before it's used.
230
+
231
+ 4. **Update your configuration:**
232
+
233
+ Add your API key to the BetterTranslate configuration:
234
+
235
+ ```ruby
236
+ # config/initializers/better_translate.rb
237
+ BetterTranslate.configure do |config|
238
+ config.provider = :deepl
239
+ config.deepl_key = ENV['DEEPL_API_KEY']
240
+ # ... other configuration
241
+ end
242
+ ```
243
+
244
+ 5. **Use your custom provider:**
245
+
246
+ Your custom provider will now be used when you call `BetterTranslate.magic` or any other translation method.
247
+
248
+ 6. **Troubleshooting:**
249
+
250
+ If you encounter a `NameError` related to the `Providers` module, make sure the provider file is properly required in your initializer as shown in step 3. The `require` statement ensures that the provider class is loaded before it's used.
251
+
175
252
  ### Translation Helpers
176
253
 
177
254
  BetterTranslate provides helper methods to simplify translation tasks.
@@ -1,6 +1,8 @@
1
1
  module BetterTranslate
2
2
  # Service class that handles translation requests using the configured provider.
3
3
  # Implements a Least Recently Used (LRU) cache to avoid redundant translation requests.
4
+ # Supports built-in providers (ChatGPT, Gemini) and custom providers registered via
5
+ # the register_provider class method.
4
6
  #
5
7
  # @example
6
8
  # service = BetterTranslate::Service.new
@@ -9,6 +11,9 @@ module BetterTranslate
9
11
  # Maximum number of translations to keep in the LRU cache
10
12
  MAX_CACHE_SIZE = 1000
11
13
 
14
+ # Registry for custom providers
15
+ @@provider_registry = {}
16
+
12
17
  # Initializes a new Service instance.
13
18
  # Sets up the translation provider based on configuration and initializes the LRU cache.
14
19
  #
@@ -30,22 +35,22 @@ module BetterTranslate
30
35
  # @return [String] The translated text
31
36
  def translate(text, target_lang_code, target_lang_name)
32
37
  cache_key = "#{text}:#{target_lang_code}"
33
-
38
+
34
39
  # Prova a recuperare dalla cache
35
40
  cached = cache_get(cache_key)
36
41
  return cached if cached
37
-
42
+
38
43
  # Traduci e salva in cache
39
44
  start_time = Time.now
40
45
  result = provider_instance.translate_text(text, target_lang_code, target_lang_name)
41
46
  duration = Time.now - start_time
42
-
47
+
43
48
  BetterTranslate::Utils.track_metric("translation_request_duration", {
44
49
  provider: @provider_name,
45
50
  text_length: text.length,
46
51
  duration: duration
47
52
  })
48
-
53
+
49
54
  cache_set(cache_key, result)
50
55
  end
51
56
 
@@ -77,7 +82,7 @@ module BetterTranslate
77
82
  oldest_key = @cache_order.shift
78
83
  @translation_cache.delete(oldest_key)
79
84
  end
80
-
85
+
81
86
  @translation_cache[key] = value
82
87
  @cache_order.push(key)
83
88
  value
@@ -87,7 +92,8 @@ module BetterTranslate
87
92
 
88
93
  # Creates or returns a cached instance of the translation provider.
89
94
  # The provider is determined by the configuration and instantiated with the appropriate API key.
90
- # Supports ChatGPT and Gemini providers.
95
+ # Supports built-in providers (ChatGPT, Gemini) and custom providers registered via
96
+ # the register_provider class method.
91
97
  #
92
98
  # @return [BetterTranslate::Providers::BaseProvider] An instance of the configured translation provider
93
99
  # @raise [RuntimeError] If the configured provider is not supported
@@ -98,8 +104,41 @@ module BetterTranslate
98
104
  when :gemini
99
105
  Providers::GeminiProvider.new(BetterTranslate.configuration.google_gemini_key)
100
106
  else
101
- raise "Provider non supportato: #{@provider_name}"
107
+ if @@provider_registry.key?(@provider_name)
108
+ # Get the API key from configuration dynamically
109
+ api_key_method = "#{@provider_name}_key".to_sym
110
+ if BetterTranslate.configuration.respond_to?(api_key_method)
111
+ api_key = BetterTranslate.configuration.send(api_key_method)
112
+ @@provider_registry[@provider_name].call(api_key)
113
+ else
114
+ raise "API key configuration missing for provider: #{@provider_name}. Add config.#{@provider_name}_key to your BetterTranslate configuration."
115
+ end
116
+ else
117
+ raise "Provider not supported: #{@provider_name}. Available providers: #{available_providers.join(', ')}"
118
+ end
102
119
  end
103
120
  end
121
+
122
+ # Returns a list of all available provider names
123
+ # @return [Array<Symbol>] List of available provider names
124
+ def available_providers
125
+ [:chatgpt, :gemini] + @@provider_registry.keys
126
+ end
127
+
128
+ # Registers a custom provider for use with BetterTranslate
129
+ #
130
+ # @param name [Symbol] The name of the provider to register
131
+ # @param factory [Proc] A proc that takes an API key and returns a provider instance
132
+ # @return [Symbol] The name of the registered provider
133
+ #
134
+ # @example Register a custom DeepL provider
135
+ # BetterTranslate::Service.register_provider(
136
+ # :deepl,
137
+ # ->(api_key) { Providers::DeepLProvider.new(api_key) }
138
+ # )
139
+ def self.register_provider(name, factory)
140
+ @@provider_registry[name] = factory
141
+ name
142
+ end
104
143
  end
105
144
  end
@@ -9,5 +9,5 @@ module BetterTranslate
9
9
  # - PATCH version for backwards-compatible bug fixes
10
10
  #
11
11
  # @return [String] The current version in the format "MAJOR.MINOR.PATCH"
12
- VERSION = "0.4.2"
12
+ VERSION = "0.5.0"
13
13
  end
@@ -1,5 +1,5 @@
1
1
  BetterTranslate.configure do |config|
2
- # Choose the provider to use: :chatgpt or :gemini
2
+ # Choose the provider to use: :chatgpt, :gemini, or any custom provider registered with BetterTranslate::Service.register_provider
3
3
  config.provider = :chatgpt
4
4
 
5
5
  # API key for OpenAI
@@ -7,6 +7,13 @@ BetterTranslate.configure do |config|
7
7
 
8
8
  # API key for Google Gemini
9
9
  config.google_gemini_key = ENV.fetch("GOOGLE_GEMINI_KEY") { "YOUR_GOOGLE_GEMINI_KEY" }
10
+
11
+ # Custom provider API keys
12
+ # If you've created a custom provider using 'rails generate better_translate:provider YourProvider',
13
+ # add its API key here following the naming convention: config.your_provider_key
14
+ #
15
+ # Example for a custom DeepL provider:
16
+ # config.deepl_key = ENV.fetch("DEEPL_API_KEY") { "YOUR_DEEPL_API_KEY" }
10
17
 
11
18
  # Source language (for example "en" if the source file is in English)
12
19
  config.source_language = "en"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alessio Bussolari