better_translate 0.4.0 → 0.4.1
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf5e2fea9a6f6702124591e888a922072386b7dc3eca153008828448eae45357
|
4
|
+
data.tar.gz: 5c11456be0bda2b86ea385235a769d405af58233c096d306f710d451eecf6f2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ca97b7c5796b961e80de8a3247aecaf7e781965dfed89197977be2fc59326f8800d6a4b82ed91fbff592c82958b23088e09493d50c42f82458a697628a2a607
|
7
|
+
data.tar.gz: 6404f6b92fa55708db6127ade139d6f3f12170ef5c6617553ccbe383be3cb0971d31b2f956b9ac1bf0f8b4bc65bc46f702f55be13cc49cfc11541af319c72811
|
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,19 @@ 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.4.1] - 2025-03-11
|
9
|
+
|
10
|
+
### Fixed
|
11
|
+
- Migliorati i test RSpec per garantire maggiore affidabilità:
|
12
|
+
- Corretti gli stub per i provider di traduzione
|
13
|
+
- Migliorata la gestione delle richieste HTTP nei test
|
14
|
+
- Ottimizzati i file YAML temporanei per i test di similarità
|
15
|
+
- Risolti problemi di compatibilità con WebMock
|
16
|
+
|
17
|
+
### Changed
|
18
|
+
- Sostituito l'approccio di stubbing specifico con pattern più flessibili
|
19
|
+
- Migliorata la struttura dei test per il SimilarityAnalyzer
|
20
|
+
|
8
21
|
## [0.4.0] - 2025-03-11
|
9
22
|
|
10
23
|
### Added
|
@@ -13,7 +13,7 @@ module BetterTranslate
|
|
13
13
|
body = {
|
14
14
|
model: "gpt-3.5-turbo",
|
15
15
|
messages: [
|
16
|
-
{ role: "system", content: "You are a professional translator. Translate the following text exactly from #{BetterTranslate.configuration.source_language} to #{target_lang_name} without
|
16
|
+
{ role: "system", content: "You are a professional translator. Translate the following text exactly from #{BetterTranslate.configuration.source_language} to #{target_lang_name}. Provide ONLY the direct translation without any explanations, alternatives, or additional text. Do not include the original text. Do not use markdown formatting. Do not add any prefixes or notes. Just return the plain translated text." },
|
17
17
|
{ role: "user", content: "#{text}" }
|
18
18
|
],
|
19
19
|
temperature: 0.3
|
@@ -30,8 +30,10 @@ module BetterTranslate
|
|
30
30
|
translated_text = json.dig("choices", 0, "message", "content")
|
31
31
|
translated_text ? translated_text.strip : text
|
32
32
|
else
|
33
|
-
raise "
|
33
|
+
raise "Errore HTTP #{response.code}: #{response.body}"
|
34
34
|
end
|
35
|
+
rescue StandardError => e
|
36
|
+
raise "Errore durante la traduzione con ChatGPT: #{e.message}"
|
35
37
|
end
|
36
38
|
end
|
37
39
|
end
|
@@ -1,33 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "net/http"
|
4
|
+
require "json"
|
5
|
+
require "uri"
|
6
|
+
|
1
7
|
module BetterTranslate
|
2
8
|
module Providers
|
3
9
|
class GeminiProvider < BaseProvider
|
4
|
-
|
5
|
-
|
10
|
+
GEMINI_API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent"
|
11
|
+
|
6
12
|
def translate_text(text, target_lang_code, target_lang_name)
|
7
|
-
|
8
|
-
|
9
|
-
"Content-Type" => "application/json",
|
10
|
-
"Authorization" => "Bearer #{@api_key}"
|
11
|
-
}
|
13
|
+
url = "#{GEMINI_API_URL}?key=#{@api_key}"
|
14
|
+
uri = URI(url)
|
12
15
|
|
16
|
+
headers = { "Content-Type" => "application/json" }
|
13
17
|
body = {
|
14
|
-
|
15
|
-
|
16
|
-
|
18
|
+
contents: [{
|
19
|
+
parts: [{
|
20
|
+
text: "Translate the following text to #{target_lang_name}. Provide ONLY the direct translation without any explanations, alternatives, or additional text. Do not include the original text. Do not use markdown formatting. Do not add any prefixes or notes. Just return the plain translated text:\n\n#{text}"
|
21
|
+
}]
|
22
|
+
}]
|
17
23
|
}
|
18
24
|
|
19
25
|
http = Net::HTTP.new(uri.host, uri.port)
|
20
26
|
http.use_ssl = true
|
21
|
-
request = Net::HTTP::Post.new(uri.path, headers)
|
27
|
+
request = Net::HTTP::Post.new(uri.path + "?" + uri.query, headers)
|
22
28
|
request.body = body.to_json
|
23
29
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
30
|
+
begin
|
31
|
+
response = http.request(request)
|
32
|
+
|
33
|
+
if response.is_a?(Net::HTTPSuccess)
|
34
|
+
json = JSON.parse(response.body)
|
35
|
+
|
36
|
+
if json["candidates"]&.any? && json["candidates"][0]["content"]["parts"]&.any?
|
37
|
+
translated_text = json["candidates"][0]["content"]["parts"][0]["text"]
|
38
|
+
|
39
|
+
# Pulizia minima del testo, dato che il prompt è già specifico
|
40
|
+
cleaned_text = translated_text.strip
|
41
|
+
.gsub(/[\*\`\n"]/, '') # Rimuovi markdown, newline e virgolette
|
42
|
+
.gsub(/\s+/, ' ') # Riduci spazi multipli a uno singolo
|
43
|
+
|
44
|
+
cleaned_text.empty? ? text : cleaned_text
|
45
|
+
else
|
46
|
+
raise "Risposta Gemini non valida: #{json}"
|
47
|
+
end
|
48
|
+
else
|
49
|
+
raise "Errore HTTP #{response.code}: #{response.body}"
|
50
|
+
end
|
51
|
+
rescue => e
|
52
|
+
raise "Errore durante la traduzione con Gemini: #{e.message}"
|
31
53
|
end
|
32
54
|
end
|
33
55
|
end
|
@@ -60,7 +60,7 @@ module BetterTranslate
|
|
60
60
|
when :chatgpt
|
61
61
|
Providers::ChatgptProvider.new(BetterTranslate.configuration.openai_key)
|
62
62
|
when :gemini
|
63
|
-
Providers::GeminiProvider.new(BetterTranslate.configuration.
|
63
|
+
Providers::GeminiProvider.new(BetterTranslate.configuration.gemini_key)
|
64
64
|
else
|
65
65
|
raise "Provider non supportato: #{@provider_name}"
|
66
66
|
end
|
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.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alessio Bussolari
|
@@ -179,19 +179,19 @@ dependencies:
|
|
179
179
|
- !ruby/object:Gem::Version
|
180
180
|
version: '3.19'
|
181
181
|
- !ruby/object:Gem::Dependency
|
182
|
-
name:
|
182
|
+
name: dotenv
|
183
183
|
requirement: !ruby/object:Gem::Requirement
|
184
184
|
requirements:
|
185
185
|
- - "~>"
|
186
186
|
- !ruby/object:Gem::Version
|
187
|
-
version: '
|
187
|
+
version: '2.8'
|
188
188
|
type: :development
|
189
189
|
prerelease: false
|
190
190
|
version_requirements: !ruby/object:Gem::Requirement
|
191
191
|
requirements:
|
192
192
|
- - "~>"
|
193
193
|
- !ruby/object:Gem::Version
|
194
|
-
version: '
|
194
|
+
version: '2.8'
|
195
195
|
description: |
|
196
196
|
BetterTranslate is a powerful Ruby gem for translating YAML files using AI providers.
|
197
197
|
|