1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
package translation
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/sashabaranov/go-openai"
"google.golang.org/genai"
appconfig "codeberg.org/snonux/totalrecall/internal/config"
)
const (
// ProviderGemini routes translation requests to Gemini.
ProviderGemini Provider = "gemini"
// ProviderOpenAI routes translation requests to OpenAI.
ProviderOpenAI Provider = "openai"
defaultGeminiModel = "gemini-2.5-flash"
translationTimeout = 30 * time.Second
translationMaxTokens = 50
translationTemperature = 0.3
)
// Provider selects the translation backend.
type Provider string
// Config holds translator settings and API credentials.
type Config struct {
Provider Provider
OpenAIKey string
GoogleAPIKey string
OpenAIModel string
GeminiModel string
}
// DefaultConfig returns a translator configuration with Gemini as the default backend.
func DefaultConfig() *Config {
return &Config{
Provider: ProviderGemini,
OpenAIModel: openai.GPT4oMini,
GeminiModel: defaultGeminiModel,
}
}
// Translator handles Bulgarian and English translation using the configured backend.
type Translator struct {
provider Provider
openAIKey string
googleAPIKey string
openAIClient *openai.Client
geminiClient *genai.Client
geminiInitErr error
openAIModel string
geminiModel string
}
var newGeminiClient = genai.NewClient
// NewTranslator creates a new translator instance from the provided config.
func NewTranslator(config *Config) *Translator {
if config == nil {
config = DefaultConfig()
}
normalized := normalizeConfig(config)
translator := &Translator{
provider: normalized.Provider,
openAIKey: normalized.OpenAIKey,
googleAPIKey: normalized.GoogleAPIKey,
openAIModel: normalized.OpenAIModel,
geminiModel: normalized.GeminiModel,
}
if normalized.OpenAIKey != "" {
translator.openAIClient = openai.NewClient(normalized.OpenAIKey)
}
if normalized.GoogleAPIKey != "" {
client, err := newGeminiClient(context.Background(), &genai.ClientConfig{
APIKey: normalized.GoogleAPIKey,
})
if err == nil {
translator.geminiClient = client
} else {
translator.geminiInitErr = err
}
}
return translator
}
// TranslateWord translates a Bulgarian word to English.
//
// Example:
//
// translator := translation.NewTranslator(&translation.Config{
// Provider: translation.ProviderGemini,
// GoogleAPIKey: os.Getenv("GOOGLE_API_KEY"),
// })
// english, err := translator.TranslateWord("ябълка")
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(english)
func (t *Translator) TranslateWord(word string) (string, error) {
return t.translate(fmt.Sprintf(
"Translate the Bulgarian word '%s' to English. Respond with only the English translation, nothing else.",
word,
))
}
// TranslateEnglishToBulgarian translates an English word to Bulgarian.
func (t *Translator) TranslateEnglishToBulgarian(word string) (string, error) {
return t.translate(fmt.Sprintf(
"Translate the English word '%s' to Bulgarian. Respond with only the Bulgarian translation in Cyrillic script, nothing else.",
word,
))
}
func (t *Translator) translate(prompt string) (string, error) {
switch normalizeProvider(t.provider) {
case ProviderGemini:
return t.translateWithGemini(prompt)
case ProviderOpenAI:
return t.translateWithOpenAI(prompt)
default:
return "", fmt.Errorf("unknown translation provider: %s", t.provider)
}
}
func (t *Translator) translateWithOpenAI(prompt string) (string, error) {
if t.openAIKey == "" {
return "", fmt.Errorf("OpenAI API key not found")
}
if t.openAIClient == nil {
return "", fmt.Errorf("OpenAI client not initialized")
}
ctx, cancel := context.WithTimeout(context.Background(), translationTimeout)
defer cancel()
req := openai.ChatCompletionRequest{
Model: t.openAIModel,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: prompt,
},
},
MaxTokens: translationMaxTokens,
Temperature: translationTemperature,
}
resp, err := t.openAIClient.CreateChatCompletion(ctx, req)
if err != nil {
return "", fmt.Errorf("OpenAI API error: %w", err)
}
if len(resp.Choices) == 0 {
return "", fmt.Errorf("no translation returned")
}
translation := strings.TrimSpace(resp.Choices[0].Message.Content)
if translation == "" {
return "", fmt.Errorf("no translation returned")
}
return translation, nil
}
func (t *Translator) translateWithGemini(prompt string) (string, error) {
if t.googleAPIKey == "" {
return "", fmt.Errorf("google API key not found")
}
if t.geminiInitErr != nil {
return "", fmt.Errorf("gemini client initialization failed: %w", t.geminiInitErr)
}
if t.geminiClient == nil {
return "", fmt.Errorf("gemini client not initialized")
}
ctx, cancel := context.WithTimeout(context.Background(), translationTimeout)
defer cancel()
temp := float32(translationTemperature)
resp, err := t.geminiClient.Models.GenerateContent(ctx, t.geminiModel, []*genai.Content{
genai.NewContentFromText(prompt, genai.RoleUser),
}, &genai.GenerateContentConfig{
Temperature: &temp,
MaxOutputTokens: translationMaxTokens,
})
if err != nil {
return "", fmt.Errorf("gemini API error: %w", err)
}
translation := strings.TrimSpace(resp.Text())
if translation == "" {
return "", fmt.Errorf("no translation returned")
}
return translation, nil
}
// SaveTranslation saves the translation to a file in the word directory.
func SaveTranslation(wordDir, word, translation string) error {
outputFile := filepath.Join(wordDir, "translation.txt")
content := fmt.Sprintf("%s = %s\n", word, translation)
if err := os.WriteFile(outputFile, []byte(content), 0644); err != nil {
return fmt.Errorf("failed to write translation file: %w", err)
}
return nil
}
// TranslationCache stores translations in memory for batch operations.
type TranslationCache struct {
translations map[string]string
}
// NewTranslationCache creates a new translation cache.
func NewTranslationCache() *TranslationCache {
return &TranslationCache{
translations: make(map[string]string),
}
}
// Add adds a translation to the cache.
func (tc *TranslationCache) Add(word, translation string) {
tc.translations[word] = translation
}
// Get retrieves a translation from the cache.
func (tc *TranslationCache) Get(word string) (string, bool) {
translation, ok := tc.translations[word]
return translation, ok
}
// GetAll returns all cached translations.
func (tc *TranslationCache) GetAll() map[string]string {
result := make(map[string]string)
for k, v := range tc.translations {
result[k] = v
}
return result
}
func normalizeConfig(config *Config) *Config {
normalized := *config
normalized.Provider = normalizeProvider(normalized.Provider)
if normalized.Provider == "" {
normalized.Provider = ProviderGemini
}
if normalized.OpenAIModel == "" {
normalized.OpenAIModel = openai.GPT4oMini
}
if normalized.GeminiModel == "" {
normalized.GeminiModel = defaultGeminiModel
}
return &normalized
}
// normalizeProvider delegates to the shared config.NormalizeProvider so the
// normalization rule (lowercase, trim, default to "gemini") has one home.
func normalizeProvider(provider Provider) Provider {
return Provider(appconfig.NormalizeProvider(string(provider)))
}
|