mistral_translator 0.1.0 → 0.2.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 +4 -4
- data/CHANGELOG.md +21 -0
- data/README.md +189 -121
- data/README_TESTING.md +33 -0
- data/SECURITY.md +157 -0
- data/docs/.nojekyll +2 -0
- data/docs/404.html +30 -0
- data/docs/README.md +153 -0
- data/docs/advanced-usage/batch-processing.md +158 -0
- data/docs/advanced-usage/error-handling.md +106 -0
- data/docs/advanced-usage/monitoring.md +133 -0
- data/docs/advanced-usage/summarization.md +86 -0
- data/docs/advanced-usage/translations.md +141 -0
- data/docs/api-reference/callbacks.md +231 -0
- data/docs/api-reference/configuration.md +74 -0
- data/docs/api-reference/errors.md +673 -0
- data/docs/api-reference/methods.md +539 -0
- data/docs/getting-started.md +179 -0
- data/docs/index.html +27 -0
- data/docs/installation.md +142 -0
- data/docs/migration-0.1.0-to-0.2.0.md +61 -0
- data/docs/rails-integration/adapters.md +84 -0
- data/docs/rails-integration/controllers.md +107 -0
- data/docs/rails-integration/jobs.md +97 -0
- data/docs/rails-integration/setup.md +339 -0
- data/examples/basic_usage.rb +129 -102
- data/examples/batch-job.rb +511 -0
- data/examples/monitoring-setup.rb +499 -0
- data/examples/rails-model.rb +399 -0
- data/lib/mistral_translator/adapters.rb +261 -0
- data/lib/mistral_translator/client.rb +103 -100
- data/lib/mistral_translator/client_helpers.rb +161 -0
- data/lib/mistral_translator/configuration.rb +171 -1
- data/lib/mistral_translator/errors.rb +16 -0
- data/lib/mistral_translator/helpers.rb +292 -0
- data/lib/mistral_translator/helpers_extensions.rb +150 -0
- data/lib/mistral_translator/levenshtein_helpers.rb +40 -0
- data/lib/mistral_translator/logger.rb +28 -4
- data/lib/mistral_translator/prompt_builder.rb +93 -41
- data/lib/mistral_translator/prompt_helpers.rb +83 -0
- data/lib/mistral_translator/prompt_metadata_helpers.rb +42 -0
- data/lib/mistral_translator/response_parser.rb +194 -23
- data/lib/mistral_translator/security.rb +72 -0
- data/lib/mistral_translator/summarizer.rb +41 -2
- data/lib/mistral_translator/translator.rb +174 -98
- data/lib/mistral_translator/translator_helpers.rb +268 -0
- data/lib/mistral_translator/version.rb +1 -1
- data/lib/mistral_translator.rb +51 -25
- metadata +39 -3
@@ -0,0 +1,539 @@
|
|
1
|
+
> **Navigation :** [🏠 Home](README.md) • [📖 API Reference](api-reference/methods.md) • [⚡ Advanced Usage](advanced-usage/translations.md) • [🛤️ Rails Integration](rails-integration/setup.md)
|
2
|
+
|
3
|
+
---
|
4
|
+
|
5
|
+
# Référence des Méthodes API
|
6
|
+
|
7
|
+
## Table des Matières
|
8
|
+
|
9
|
+
- [Méthodes Principales](#méthodes-principales)
|
10
|
+
- [Méthodes de Configuration](#méthodes-de-configuration)
|
11
|
+
- [Méthodes Utilitaires](#méthodes-utilitaires)
|
12
|
+
- [Méthodes Avancées](#méthodes-avancées)
|
13
|
+
- [Helpers et Extensions](#helpers-et-extensions)
|
14
|
+
|
15
|
+
---
|
16
|
+
|
17
|
+
## Méthodes Principales
|
18
|
+
|
19
|
+
### `translate(text, from:, to:, **options)`
|
20
|
+
|
21
|
+
Traduit un texte d'une langue vers une autre.
|
22
|
+
|
23
|
+
**Paramètres:**
|
24
|
+
|
25
|
+
- `text` (String) - Le texte à traduire
|
26
|
+
- `from` (String|Symbol) - Code de langue source (ex: "fr", "en")
|
27
|
+
- `to` (String|Symbol) - Code de langue cible
|
28
|
+
- `context` (String, optionnel) - Contexte pour améliorer la traduction
|
29
|
+
- `glossary` (Hash, optionnel) - Dictionnaire de termes à respecter
|
30
|
+
- `preserve_html` (Boolean, optionnel) - Préserver les balises HTML
|
31
|
+
|
32
|
+
**Retour:** String - Le texte traduit
|
33
|
+
|
34
|
+
**Exemple:**
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
MistralTranslator.translate(
|
38
|
+
"Bonjour le monde",
|
39
|
+
from: "fr",
|
40
|
+
to: "en",
|
41
|
+
context: "greeting",
|
42
|
+
glossary: { "monde" => "world" }
|
43
|
+
)
|
44
|
+
# => "Hello world"
|
45
|
+
```
|
46
|
+
|
47
|
+
**Exceptions:**
|
48
|
+
|
49
|
+
- `UnsupportedLanguageError` - Langue non supportée
|
50
|
+
- `EmptyTranslationError` - Traduction vide reçue
|
51
|
+
- `RateLimitError` - Limite de taux API atteinte
|
52
|
+
|
53
|
+
---
|
54
|
+
|
55
|
+
### `translate_auto(text, to:, **options)`
|
56
|
+
|
57
|
+
Traduit un texte avec détection automatique de la langue source.
|
58
|
+
|
59
|
+
**Paramètres:**
|
60
|
+
|
61
|
+
- `text` (String) - Le texte à traduire
|
62
|
+
- `to` (String|Symbol) - Code de langue cible
|
63
|
+
- `context` (String, optionnel) - Contexte pour la traduction
|
64
|
+
- `glossary` (Hash, optionnel) - Dictionnaire de termes
|
65
|
+
|
66
|
+
**Retour:** String - Le texte traduit
|
67
|
+
|
68
|
+
**Exemple:**
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
MistralTranslator.translate_auto("¡Hola mundo!", to: "fr")
|
72
|
+
# => "Salut le monde !"
|
73
|
+
```
|
74
|
+
|
75
|
+
---
|
76
|
+
|
77
|
+
### `translate_to_multiple(text, from:, to:, **options)`
|
78
|
+
|
79
|
+
Traduit un texte vers plusieurs langues simultanément.
|
80
|
+
|
81
|
+
**Paramètres:**
|
82
|
+
|
83
|
+
- `text` (String) - Le texte à traduire
|
84
|
+
- `from` (String|Symbol) - Code de langue source
|
85
|
+
- `to` (Array<String>) - Codes des langues cibles
|
86
|
+
- `use_batch` (Boolean, optionnel) - Utiliser le mode batch pour plus de 3 langues
|
87
|
+
- `context` (String, optionnel) - Contexte pour la traduction
|
88
|
+
- `glossary` (Hash, optionnel) - Dictionnaire de termes
|
89
|
+
|
90
|
+
**Retour:** Hash - Hash avec les codes de langue comme clés et traductions comme valeurs
|
91
|
+
|
92
|
+
**Exemple:**
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
results = MistralTranslator.translate_to_multiple(
|
96
|
+
"Hello world",
|
97
|
+
from: "en",
|
98
|
+
to: %w[fr es de it]
|
99
|
+
)
|
100
|
+
# => {
|
101
|
+
# "fr" => "Bonjour le monde",
|
102
|
+
# "es" => "Hola mundo",
|
103
|
+
# "de" => "Hallo Welt",
|
104
|
+
# "it" => "Ciao mondo"
|
105
|
+
# }
|
106
|
+
```
|
107
|
+
|
108
|
+
---
|
109
|
+
|
110
|
+
### `translate_batch(texts, from:, to:, **options)`
|
111
|
+
|
112
|
+
Traduit plusieurs textes en une fois pour optimiser les performances.
|
113
|
+
|
114
|
+
**Paramètres:**
|
115
|
+
|
116
|
+
- `texts` (Array<String>) - Les textes à traduire (max 20 éléments)
|
117
|
+
- `from` (String|Symbol) - Code de langue source
|
118
|
+
- `to` (String|Symbol) - Code de langue cible
|
119
|
+
- `context` (String, optionnel) - Contexte commun pour tous les textes
|
120
|
+
- `glossary` (Hash, optionnel) - Dictionnaire de termes
|
121
|
+
|
122
|
+
**Retour:** Hash - Hash avec les index comme clés et traductions comme valeurs
|
123
|
+
|
124
|
+
**Exemple:**
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
texts = ["Bonjour", "Merci", "Au revoir"]
|
128
|
+
results = MistralTranslator.translate_batch(texts, from: "fr", to: "en")
|
129
|
+
# => {
|
130
|
+
# 0 => "Hello",
|
131
|
+
# 1 => "Thank you",
|
132
|
+
# 2 => "Goodbye"
|
133
|
+
# }
|
134
|
+
```
|
135
|
+
|
136
|
+
---
|
137
|
+
|
138
|
+
## Méthodes de Résumé
|
139
|
+
|
140
|
+
### `summarize(text, language:, max_words:, **options)`
|
141
|
+
|
142
|
+
Crée un résumé d'un texte dans une langue donnée.
|
143
|
+
|
144
|
+
**Paramètres:**
|
145
|
+
|
146
|
+
- `text` (String) - Le texte à résumer
|
147
|
+
- `language` (String|Symbol) - Langue du résumé (défaut: "fr")
|
148
|
+
- `max_words` (Integer) - Nombre maximum de mots (défaut: 250)
|
149
|
+
- `style` (String, optionnel) - Style du résumé (formal, casual, academic)
|
150
|
+
- `context` (String, optionnel) - Contexte du document
|
151
|
+
|
152
|
+
**Retour:** String - Le résumé
|
153
|
+
|
154
|
+
**Exemple:**
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
article = "Un long article sur Ruby on Rails..."
|
158
|
+
resume = MistralTranslator.summarize(
|
159
|
+
article,
|
160
|
+
language: "fr",
|
161
|
+
max_words: 100,
|
162
|
+
style: "academic"
|
163
|
+
)
|
164
|
+
```
|
165
|
+
|
166
|
+
---
|
167
|
+
|
168
|
+
### `summarize_and_translate(text, from:, to:, max_words:, **options)`
|
169
|
+
|
170
|
+
Résume et traduit un texte simultanément.
|
171
|
+
|
172
|
+
**Paramètres:**
|
173
|
+
|
174
|
+
- `text` (String) - Le texte à résumer et traduire
|
175
|
+
- `from` (String|Symbol) - Langue source
|
176
|
+
- `to` (String|Symbol) - Langue cible
|
177
|
+
- `max_words` (Integer) - Nombre maximum de mots
|
178
|
+
- `style` (String, optionnel) - Style du résumé
|
179
|
+
- `context` (String, optionnel) - Contexte du document
|
180
|
+
|
181
|
+
**Retour:** String - Le résumé traduit
|
182
|
+
|
183
|
+
**Exemple:**
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
result = MistralTranslator.summarize_and_translate(
|
187
|
+
"Un long texte en français...",
|
188
|
+
from: "fr",
|
189
|
+
to: "en",
|
190
|
+
max_words: 150
|
191
|
+
)
|
192
|
+
```
|
193
|
+
|
194
|
+
---
|
195
|
+
|
196
|
+
### `summarize_tiered(text, language:, short:, medium:, long:, **options)`
|
197
|
+
|
198
|
+
Crée plusieurs résumés de longueurs différentes.
|
199
|
+
|
200
|
+
**Paramètres:**
|
201
|
+
|
202
|
+
- `text` (String) - Le texte à résumer
|
203
|
+
- `language` (String|Symbol) - Langue des résumés
|
204
|
+
- `short` (Integer) - Nombre de mots pour le résumé court
|
205
|
+
- `medium` (Integer) - Nombre de mots pour le résumé moyen
|
206
|
+
- `long` (Integer) - Nombre de mots pour le résumé long
|
207
|
+
- `style` (String, optionnel) - Style des résumés
|
208
|
+
- `context` (String, optionnel) - Contexte du document
|
209
|
+
|
210
|
+
**Retour:** Hash - Hash avec les clés `:short`, `:medium`, `:long`
|
211
|
+
|
212
|
+
**Exemple:**
|
213
|
+
|
214
|
+
```ruby
|
215
|
+
resumes = MistralTranslator.summarize_tiered(
|
216
|
+
"Un très long article...",
|
217
|
+
language: "fr",
|
218
|
+
short: 50,
|
219
|
+
medium: 150,
|
220
|
+
long: 300
|
221
|
+
)
|
222
|
+
# => {
|
223
|
+
# short: "Résumé court...",
|
224
|
+
# medium: "Résumé moyen...",
|
225
|
+
# long: "Résumé détaillé..."
|
226
|
+
# }
|
227
|
+
```
|
228
|
+
|
229
|
+
---
|
230
|
+
|
231
|
+
### `summarize_to_multiple(text, languages:, max_words:, **options)`
|
232
|
+
|
233
|
+
Crée un résumé dans plusieurs langues.
|
234
|
+
|
235
|
+
**Paramètres:**
|
236
|
+
|
237
|
+
- `text` (String) - Le texte à résumer
|
238
|
+
- `languages` (Array<String>) - Langues des résumés
|
239
|
+
- `max_words` (Integer) - Nombre maximum de mots par résumé
|
240
|
+
|
241
|
+
**Retour:** Hash - Hash avec les codes de langue comme clés
|
242
|
+
|
243
|
+
**Exemple:**
|
244
|
+
|
245
|
+
```ruby
|
246
|
+
resumes = MistralTranslator.summarize_to_multiple(
|
247
|
+
"Un long texte...",
|
248
|
+
languages: %w[fr en es],
|
249
|
+
max_words: 200
|
250
|
+
)
|
251
|
+
# => {
|
252
|
+
# "fr" => "Résumé en français...",
|
253
|
+
# "en" => "Summary in English...",
|
254
|
+
# "es" => "Resumen en español..."
|
255
|
+
# }
|
256
|
+
```
|
257
|
+
|
258
|
+
---
|
259
|
+
|
260
|
+
## Méthodes de Configuration
|
261
|
+
|
262
|
+
### `configure { |config| ... }`
|
263
|
+
|
264
|
+
Configure la gem avec un bloc.
|
265
|
+
|
266
|
+
**Exemple:**
|
267
|
+
|
268
|
+
```ruby
|
269
|
+
MistralTranslator.configure do |config|
|
270
|
+
config.api_key = ENV['MISTRAL_API_KEY']
|
271
|
+
config.model = "mistral-small"
|
272
|
+
config.retry_delays = [1, 2, 4, 8]
|
273
|
+
config.enable_metrics = true
|
274
|
+
end
|
275
|
+
```
|
276
|
+
|
277
|
+
### `configuration`
|
278
|
+
|
279
|
+
Accède à l'objet de configuration actuel.
|
280
|
+
|
281
|
+
**Retour:** Configuration - L'instance de configuration
|
282
|
+
|
283
|
+
### `reset_configuration!`
|
284
|
+
|
285
|
+
Remet la configuration aux valeurs par défaut.
|
286
|
+
|
287
|
+
---
|
288
|
+
|
289
|
+
## Méthodes Utilitaires
|
290
|
+
|
291
|
+
### `supported_locales`
|
292
|
+
|
293
|
+
Retourne la liste des codes de langue supportés.
|
294
|
+
|
295
|
+
**Retour:** Array<String> - Liste des codes (ex: ["fr", "en", "es"])
|
296
|
+
|
297
|
+
### `supported_languages`
|
298
|
+
|
299
|
+
Retourne une chaîne formatée des langues supportées.
|
300
|
+
|
301
|
+
**Retour:** String - Liste formatée (ex: "fr (français), en (english)")
|
302
|
+
|
303
|
+
### `health_check`
|
304
|
+
|
305
|
+
Vérifie la connectivité avec l'API Mistral.
|
306
|
+
|
307
|
+
**Retour:** Hash - Status et message
|
308
|
+
|
309
|
+
- `status` (:ok | :error)
|
310
|
+
- `message` (String) - Message descriptif
|
311
|
+
|
312
|
+
**Exemple:**
|
313
|
+
|
314
|
+
```ruby
|
315
|
+
health = MistralTranslator.health_check
|
316
|
+
if health[:status] == :ok
|
317
|
+
puts "API disponible"
|
318
|
+
else
|
319
|
+
puts "Erreur: #{health[:message]}"
|
320
|
+
end
|
321
|
+
```
|
322
|
+
|
323
|
+
### `version_info`
|
324
|
+
|
325
|
+
Retourne les informations de version détaillées.
|
326
|
+
|
327
|
+
**Retour:** Hash - Informations système
|
328
|
+
|
329
|
+
- `gem_version` - Version de la gem
|
330
|
+
- `api_version` - Version de l'API Mistral
|
331
|
+
- `supported_model` - Modèle supporté
|
332
|
+
- `ruby_version` - Version Ruby
|
333
|
+
- `platform` - Plateforme système
|
334
|
+
|
335
|
+
---
|
336
|
+
|
337
|
+
## Méthodes de Métriques
|
338
|
+
|
339
|
+
### `metrics`
|
340
|
+
|
341
|
+
Retourne les métriques de performance (si activées).
|
342
|
+
|
343
|
+
**Retour:** Hash - Métriques détaillées
|
344
|
+
|
345
|
+
- `total_translations` - Nombre total de traductions
|
346
|
+
- `total_characters` - Nombre total de caractères traités
|
347
|
+
- `total_duration` - Durée totale des traductions
|
348
|
+
- `average_translation_time` - Temps moyen par traduction
|
349
|
+
- `error_rate` - Taux d'erreur en pourcentage
|
350
|
+
- `translations_by_language` - Décompte par paire de langues
|
351
|
+
|
352
|
+
**Exemple:**
|
353
|
+
|
354
|
+
```ruby
|
355
|
+
MistralTranslator.configure { |c| c.enable_metrics = true }
|
356
|
+
|
357
|
+
# Après quelques traductions...
|
358
|
+
metrics = MistralTranslator.metrics
|
359
|
+
puts "#{metrics[:total_translations]} traductions en #{metrics[:total_duration]}s"
|
360
|
+
puts "Temps moyen: #{metrics[:average_translation_time]}s"
|
361
|
+
```
|
362
|
+
|
363
|
+
### `MistralTranslator.reset_metrics!`
|
364
|
+
|
365
|
+
Remet à zéro toutes les métriques.
|
366
|
+
|
367
|
+
---
|
368
|
+
|
369
|
+
## Méthodes Avancées
|
370
|
+
|
371
|
+
### Classes Principales
|
372
|
+
|
373
|
+
#### `MistralTranslator::Translator.new(client: nil)`
|
374
|
+
|
375
|
+
Crée une instance de traducteur avec client personnalisé.
|
376
|
+
|
377
|
+
**Méthodes d'instance:**
|
378
|
+
|
379
|
+
- `translate(text, from:, to:, **options)` - Traduction simple
|
380
|
+
- `translate_with_confidence(text, from:, to:, **options)` - Avec score de confiance
|
381
|
+
- `translate_to_multiple(text, from:, to:, **options)` - Multi-langues
|
382
|
+
- `translate_batch(texts, from:, to:, **options)` - Par lots
|
383
|
+
- `translate_auto(text, to:, **options)` - Auto-détection
|
384
|
+
|
385
|
+
#### `MistralTranslator::Summarizer.new(client: nil)`
|
386
|
+
|
387
|
+
Crée une instance de résumeur avec client personnalisé.
|
388
|
+
|
389
|
+
**Méthodes d'instance:**
|
390
|
+
|
391
|
+
- `summarize(text, language:, max_words:)` - Résumé simple
|
392
|
+
- `summarize_and_translate(text, from:, to:, max_words:)` - Résumé + traduction
|
393
|
+
- `summarize_tiered(text, language:, short:, medium:, long:)` - Multi-niveaux
|
394
|
+
- `summarize_to_multiple(text, languages:, max_words:)` - Multi-langues
|
395
|
+
|
396
|
+
#### `MistralTranslator::Client.new(api_key: nil, rate_limiter: nil)`
|
397
|
+
|
398
|
+
Client HTTP pour l'API Mistral.
|
399
|
+
|
400
|
+
**Méthodes d'instance:**
|
401
|
+
|
402
|
+
- `complete(prompt, max_tokens: nil, temperature: nil, context: {})` - Complétion
|
403
|
+
- `chat(prompt, max_tokens: nil, temperature: nil, context: {})` - Chat
|
404
|
+
- `translate_batch(requests, batch_size: 5)` - Traduction par lots
|
405
|
+
|
406
|
+
---
|
407
|
+
|
408
|
+
## Helpers et Extensions
|
409
|
+
|
410
|
+
### `MistralTranslator::Helpers`
|
411
|
+
|
412
|
+
Module avec des méthodes utilitaires avancées.
|
413
|
+
|
414
|
+
#### Méthodes disponibles:
|
415
|
+
|
416
|
+
##### `translate_batch_with_fallback(texts, from:, to:, **options)`
|
417
|
+
|
418
|
+
Traduction par lots avec stratégies de récupération.
|
419
|
+
|
420
|
+
**Options:**
|
421
|
+
|
422
|
+
- `fallback_strategy` (:individual) - Stratégie en cas d'échec
|
423
|
+
|
424
|
+
##### `translate_with_progress(items, from:, to:, **options, &block)`
|
425
|
+
|
426
|
+
Traduction avec callback de progression.
|
427
|
+
|
428
|
+
**Exemple:**
|
429
|
+
|
430
|
+
```ruby
|
431
|
+
items = {
|
432
|
+
title: "Titre",
|
433
|
+
content: "Contenu..."
|
434
|
+
}
|
435
|
+
|
436
|
+
results = MistralTranslator::Helpers.translate_with_progress(
|
437
|
+
items,
|
438
|
+
from: "fr",
|
439
|
+
to: "en"
|
440
|
+
) do |current, total, key, result|
|
441
|
+
puts "#{current}/#{total}: #{key} - #{result[:success] ? 'OK' : 'ERROR'}"
|
442
|
+
end
|
443
|
+
```
|
444
|
+
|
445
|
+
##### `smart_summarize(text, max_words:, target_language:, **options)`
|
446
|
+
|
447
|
+
Résumé intelligent avec détection automatique du format.
|
448
|
+
|
449
|
+
##### `translate_multi_style(text, from:, to:, **options)`
|
450
|
+
|
451
|
+
Traduction dans plusieurs styles.
|
452
|
+
|
453
|
+
**Options:**
|
454
|
+
|
455
|
+
- `styles` (Array) - Liste des styles (%i[formal casual academic])
|
456
|
+
|
457
|
+
##### `validate_locale_with_suggestions(locale)`
|
458
|
+
|
459
|
+
Validation de locale avec suggestions.
|
460
|
+
|
461
|
+
**Retour:** Hash
|
462
|
+
|
463
|
+
- `valid` (Boolean) - Locale valide ou non
|
464
|
+
- `locale` (String) - Locale normalisée (si valide)
|
465
|
+
- `suggestions` (Array) - Suggestions (si invalide)
|
466
|
+
|
467
|
+
##### `estimate_translation_cost(text, from:, to:, rate_per_1k_chars:)`
|
468
|
+
|
469
|
+
Estimation du coût de traduction.
|
470
|
+
|
471
|
+
**Retour:** Hash
|
472
|
+
|
473
|
+
- `character_count` - Nombre de caractères
|
474
|
+
- `estimated_cost` - Coût estimé
|
475
|
+
- `currency` - Devise ("USD")
|
476
|
+
|
477
|
+
##### `setup_rails_integration(**options)`
|
478
|
+
|
479
|
+
Configuration automatique pour Rails.
|
480
|
+
|
481
|
+
**Options:**
|
482
|
+
|
483
|
+
- `api_key` - Clé API (défaut: ENV['MISTRAL_API_KEY'])
|
484
|
+
- `enable_metrics` (Boolean) - Activer les métriques
|
485
|
+
- `setup_logging` (Boolean) - Configurer les logs Rails
|
486
|
+
|
487
|
+
---
|
488
|
+
|
489
|
+
### Extensions String (optionnelles)
|
490
|
+
|
491
|
+
Si activées, ajoutent des méthodes à la classe String:
|
492
|
+
|
493
|
+
```ruby
|
494
|
+
# Activation manuelle si souhaitée
|
495
|
+
String.include(MistralTranslator::StringExtensions)
|
496
|
+
|
497
|
+
# Puis utilisation:
|
498
|
+
"Bonjour".translate_to("en") # => "Hello"
|
499
|
+
"Long texte...".summarize(language: "fr", max_words: 50)
|
500
|
+
```
|
501
|
+
|
502
|
+
---
|
503
|
+
|
504
|
+
## Adaptateurs Rails
|
505
|
+
|
506
|
+
### `MistralTranslator::Adapters`
|
507
|
+
|
508
|
+
Support pour les gems d'internationalisation Rails.
|
509
|
+
|
510
|
+
#### Adaptateurs disponibles:
|
511
|
+
|
512
|
+
- `MobilityAdapter` - Pour la gem Mobility
|
513
|
+
- `GlobalizeAdapter` - Pour la gem Globalize
|
514
|
+
- `I18nAttributesAdapter` - Pour les attributs avec suffixes (\_fr, \_en)
|
515
|
+
- `CustomAdapter` - Pour méthodes personnalisées
|
516
|
+
|
517
|
+
#### Usage avec les modèles Rails:
|
518
|
+
|
519
|
+
```ruby
|
520
|
+
# Auto-détection de l'adaptateur
|
521
|
+
service = MistralTranslator::Adapters::RecordTranslationService.new(
|
522
|
+
user,
|
523
|
+
[:name, :description],
|
524
|
+
source_locale: :fr
|
525
|
+
)
|
526
|
+
success = service.translate_to_all_locales
|
527
|
+
|
528
|
+
# Ou via les helpers
|
529
|
+
MistralTranslator::RecordTranslation.translate_record(
|
530
|
+
user,
|
531
|
+
[:name, :description],
|
532
|
+
source_locale: :fr
|
533
|
+
)
|
534
|
+
```
|
535
|
+
|
536
|
+
---
|
537
|
+
|
538
|
+
**API-Reference Navigation:**
|
539
|
+
[← Methods](api-reference/methods.md) | [Errors](api-reference/errors.md) | [Callbacks](api-reference/callbacks.md) | [Configuration](api-reference/configuration.md) →
|
@@ -0,0 +1,179 @@
|
|
1
|
+
> **Navigation :** [🏠 Home](README.md) • [📖 API Reference](api-reference/methods.md) • [⚡ Advanced Usage](advanced-usage/translations.md) • [🛤️ Rails Integration](rails-integration/setup.md)
|
2
|
+
|
3
|
+
---
|
4
|
+
|
5
|
+
# Guide de Démarrage
|
6
|
+
|
7
|
+
Premiers pas avec MistralTranslator : exemples concrets pour débuter rapidement.
|
8
|
+
|
9
|
+
## 🚀 Première Traduction
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
require 'mistral_translator'
|
13
|
+
|
14
|
+
MistralTranslator.configure do |config|
|
15
|
+
config.api_key = ENV['MISTRAL_API_KEY']
|
16
|
+
end
|
17
|
+
|
18
|
+
result = MistralTranslator.translate("Bonjour le monde", from: "fr", to: "en")
|
19
|
+
puts result
|
20
|
+
# => "Hello world"
|
21
|
+
```
|
22
|
+
|
23
|
+
## 📝 Traductions de Base
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
# Français → Anglais
|
27
|
+
MistralTranslator.translate("Comment ça va ?", from: "fr", to: "en")
|
28
|
+
# => "How are you?"
|
29
|
+
|
30
|
+
# Anglais → Espagnol
|
31
|
+
MistralTranslator.translate("Good morning", from: "en", to: "es")
|
32
|
+
# => "Buenos días"
|
33
|
+
|
34
|
+
# Auto-détection de langue
|
35
|
+
MistralTranslator.translate_auto("Guten Tag", to: "fr")
|
36
|
+
# => "Bonjour"
|
37
|
+
```
|
38
|
+
|
39
|
+
## 🌍 Langues Supportées
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
# Vérifier les langues disponibles
|
43
|
+
MistralTranslator::LocaleHelper.supported_locales
|
44
|
+
# => ["fr", "en", "es", "pt", "de", "it", "nl", "ru", "mg", "ja", "ko", "zh", "ar"]
|
45
|
+
|
46
|
+
# Vérifier une langue spécifique
|
47
|
+
MistralTranslator::LocaleHelper.locale_supported?("ja")
|
48
|
+
# => true
|
49
|
+
```
|
50
|
+
|
51
|
+
## 🚨 Gestion d'Erreurs Simple
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
def safe_translate(text, from:, to:)
|
55
|
+
return "" if text.blank?
|
56
|
+
return text if from == to
|
57
|
+
|
58
|
+
MistralTranslator.translate(text, from: from, to: to)
|
59
|
+
rescue MistralTranslator::RateLimitError
|
60
|
+
"Limite API atteinte, réessayez plus tard"
|
61
|
+
rescue MistralTranslator::Error => e
|
62
|
+
Rails.logger.error "Translation error: #{e.message}"
|
63
|
+
text # Fallback vers original
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
## ⚡ Traduction Multiple
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
# Vers plusieurs langues
|
71
|
+
translator = MistralTranslator::Translator.new
|
72
|
+
results = translator.translate_to_multiple(
|
73
|
+
"Bienvenue",
|
74
|
+
from: "fr",
|
75
|
+
to: ["en", "es", "de"]
|
76
|
+
)
|
77
|
+
# => { "en" => "Welcome", "es" => "Bienvenido", "de" => "Willkommen" }
|
78
|
+
```
|
79
|
+
|
80
|
+
## 🎨 Contexte et Glossaire
|
81
|
+
|
82
|
+
_Compatibilité:_ v0.1.0 ne supporte pas `context`/`glossary` via l'API publique. Utilisez une instance `MistralTranslator::Translator`. En v0.2.0, ces options sont supportées par `translator.translate`.
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
translator = MistralTranslator::Translator.new
|
86
|
+
|
87
|
+
# Avec contexte
|
88
|
+
translator.translate(
|
89
|
+
"Batterie faible",
|
90
|
+
from: "fr", to: "en",
|
91
|
+
context: "Smartphone notification"
|
92
|
+
)
|
93
|
+
# => "Battery low"
|
94
|
+
|
95
|
+
# Avec glossaire
|
96
|
+
tech_glossary = { "IA" => "AI", "données" => "data" }
|
97
|
+
translator.translate(
|
98
|
+
"L'IA analyse les données",
|
99
|
+
from: "fr", to: "en",
|
100
|
+
glossary: tech_glossary
|
101
|
+
)
|
102
|
+
# => "AI analyzes data"
|
103
|
+
```
|
104
|
+
|
105
|
+
## 📊 Résumés
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
summarizer = MistralTranslator::Summarizer.new
|
109
|
+
|
110
|
+
# Résumé simple
|
111
|
+
summary = summarizer.summarize(long_text, language: "fr", max_words: 100)
|
112
|
+
|
113
|
+
# Résumé + traduction
|
114
|
+
english_summary = summarizer.summarize_and_translate(
|
115
|
+
french_text, from: "fr", to: "en", max_words: 150
|
116
|
+
)
|
117
|
+
```
|
118
|
+
|
119
|
+
## 🛤️ Rails Integration
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
# Dans un modèle
|
123
|
+
class Article < ApplicationRecord
|
124
|
+
def translate_to(language)
|
125
|
+
translator = MistralTranslator::Translator.new
|
126
|
+
translator.translate(
|
127
|
+
title,
|
128
|
+
from: I18n.locale.to_s,
|
129
|
+
to: language.to_s,
|
130
|
+
context: "Blog article title"
|
131
|
+
)
|
132
|
+
rescue MistralTranslator::Error
|
133
|
+
title
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# Dans un helper
|
138
|
+
module ApplicationHelper
|
139
|
+
def safe_translate(text, to:, context: nil)
|
140
|
+
translator = MistralTranslator::Translator.new
|
141
|
+
translator.translate(text, from: I18n.locale, to: to, context: context)
|
142
|
+
rescue MistralTranslator::Error
|
143
|
+
text
|
144
|
+
end
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
## 📈 Métriques
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
# Activer le suivi
|
152
|
+
MistralTranslator.configure do |config|
|
153
|
+
config.enable_metrics = true
|
154
|
+
end
|
155
|
+
|
156
|
+
# Consulter les stats
|
157
|
+
metrics = MistralTranslator.metrics
|
158
|
+
puts "Traductions: #{metrics[:total_translations]}"
|
159
|
+
puts "Temps moyen: #{metrics[:average_translation_time]}s"
|
160
|
+
```
|
161
|
+
|
162
|
+
## 💾 Cache Recommandé
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
def cached_translate(text, from:, to:)
|
166
|
+
cache_key = "translation:#{Digest::MD5.hexdigest(text)}:#{from}:#{to}"
|
167
|
+
|
168
|
+
Rails.cache.fetch(cache_key, expires_in: 24.hours) do
|
169
|
+
MistralTranslator.translate(text, from: from, to: to)
|
170
|
+
end
|
171
|
+
rescue MistralTranslator::Error
|
172
|
+
text
|
173
|
+
end
|
174
|
+
```
|
175
|
+
|
176
|
+
---
|
177
|
+
|
178
|
+
**Documentation Navigation:**
|
179
|
+
[← Installation](installation.md) | [Getting Started](getting-started.md) | [Migration Guide](migration-0.1.0-to-0.2.0.md) →
|