File size: 6,713 Bytes
d14edbf
 
 
 
 
9594324
d14edbf
 
 
 
 
9594324
d14edbf
 
 
 
 
 
9594324
d14edbf
 
9594324
d14edbf
 
9594324
d14edbf
 
 
 
 
 
 
9594324
d14edbf
 
 
 
 
 
 
 
 
 
 
 
 
9594324
d14edbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9594324
d14edbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9594324
d14edbf
 
 
 
9594324
d14edbf
 
 
 
9594324
d14edbf
 
 
 
9594324
d14edbf
 
 
 
9594324
d14edbf
 
 
 
9594324
d14edbf
 
 
 
9594324
d14edbf
 
 
 
 
 
 
 
 
9594324
d14edbf
 
 
 
 
 
 
9594324
d14edbf
 
 
 
 
 
 
9594324
d14edbf
 
 
 
 
 
 
 
 
 
9594324
d14edbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9594324
d14edbf
 
 
 
 
 
 
9594324
d14edbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9594324
d14edbf
 
 
 
 
 
 
 
 
 
 
 
 
 
9594324
d14edbf
 
 
 
9594324
d14edbf
 
 
 
 
 
 
 
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
# Esempi di Richieste API - Local Translation Server

## Health Check

```bash
curl -X GET "http://localhost:7860/"
```

## Ottenere le lingue supportate

```bash
curl -X GET "http://localhost:7860/languages"
```

## Traduzione singola (GET con parametri)

```bash
# Traduzione semplice
curl -X POST "http://localhost:7860/translate-single?text=Ciao%20mondo&target_language=en"

# Traduzione con HTML
curl -X POST "http://localhost:7860/translate-single?text=%3Cp%3ECiao%20%3Cstrong%3Emondo%3C/strong%3E%3C/p%3E&target_language=fr"

# Traduzione con placeholder
curl -X POST "http://localhost:7860/translate-single?text=Ciao%20%7Bname%7D%2C%20come%20stai%3F&target_language=de"
```

## Traduzione di oggetti locale complessi

### Esempio base

```bash
curl -X POST "http://localhost:7860/translate?target_language=en" \
  -H "Content-Type: application/json" \
  -d '{
    "locales": {
      "welcome": "Benvenuto!",
      "save_button": "Salva",
      "cancel_button": "Annulla"
    }
  }'
```

### Esempio con HTML e placeholder

```bash
curl -X POST "http://localhost:7860/translate?target_language=fr" \
  -H "Content-Type: application/json" \
  -d '{
    "locales": {
      "welcome_message": "Benvenuto nella nostra applicazione, {username}!",
      "notification_html": "<div class=\"alert alert-info\"><strong>Info:</strong> Hai {count} nuovi messaggi</div>",
      "button_save": "Salva documento",
      "error_required": "Questo campo è obbligatorio",
      "success_message": "<p>Operazione completata con <em>successo</em>!</p>"
    }
  }'
```

### Esempio per applicazione web complessa

```bash
curl -X POST "http://localhost:7860/translate?target_language=de" \
  -H "Content-Type: application/json" \
  -d '{
    "locales": {
      "nav_home": "Home",
      "nav_products": "Prodotti",
      "nav_contact": "Contatti",
      "hero_title": "Benvenuto nel futuro",
      "hero_subtitle": "La tecnologia che cambia il mondo",
      "cta_button": "Scopri di più",
      "form_name_label": "Nome completo",
      "form_email_label": "Indirizzo email",
      "form_message_label": "Il tuo messaggio",
      "form_submit": "Invia messaggio",
      "validation_required": "Questo campo è obbligatorio",
      "validation_email": "Inserisci un indirizzo email valido",
      "success_notification": "<div class=\"notification is-success\"><strong>Grazie!</strong> Il tuo messaggio è stato inviato</div>",
      "error_notification": "<div class=\"notification is-danger\"><strong>Errore:</strong> Si è verificato un problema</div>",
      "user_greeting": "Ciao {firstName}, bentornato!",
      "items_count": "Hai {itemCount} elementi nel carrello",
      "last_login": "Ultimo accesso: {lastLoginDate}",
      "footer_copyright": "© 2024 La Nostra Azienda. Tutti i diritti riservati."
    }
  }'
```

### Traduzione verso lingue diverse

```bash
# Inglese
curl -X POST "http://localhost:7860/translate?target_language=en" \
  -H "Content-Type: application/json" \
  -d '{"locales": {"greeting": "Buongiorno!", "goodbye": "Arrivederci!"}}'

# Francese
curl -X POST "http://localhost:7860/translate?target_language=fr" \
  -H "Content-Type: application/json" \
  -d '{"locales": {"greeting": "Buongiorno!", "goodbye": "Arrivederci!"}}'

# Tedesco
curl -X POST "http://localhost:7860/translate?target_language=de" \
  -H "Content-Type: application/json" \
  -d '{"locales": {"greeting": "Buongiorno!", "goodbye": "Arrivederci!"}}'

# Spagnolo
curl -X POST "http://localhost:7860/translate?target_language=es" \
  -H "Content-Type: application/json" \
  -d '{"locales": {"greeting": "Buongiorno!", "goodbye": "Arrivederci!"}}'

# Russo
curl -X POST "http://localhost:7860/translate?target_language=ru" \
  -H "Content-Type: application/json" \
  -d '{"locales": {"greeting": "Buongiorno!", "goodbye": "Arrivederci!"}}'

# Giapponese
curl -X POST "http://localhost:7860/translate?target_language=ja" \
  -H "Content-Type: application/json" \
  -d '{"locales": {"greeting": "Buongiorno!", "goodbye": "Arrivederci!"}}'

# Cinese
curl -X POST "http://localhost:7860/translate?target_language=zh" \
  -H "Content-Type: application/json" \
  -d '{"locales": {"greeting": "Buongiorno!", "goodbye": "Arrivederci!"}}'
```

## Test degli errori

### Lingua non supportata

```bash
curl -X POST "http://localhost:7860/translate?target_language=xyz" \
  -H "Content-Type: application/json" \
  -d '{"locales": {"test": "Ciao"}}'
```

### Body malformato

```bash
curl -X POST "http://localhost:7860/translate?target_language=en" \
  -H "Content-Type: application/json" \
  -d '{"wrong_field": "test"}'
```

### Parametro mancante

```bash
curl -X POST "http://localhost:7860/translate" \
  -H "Content-Type: application/json" \
  -d '{"locales": {"test": "Ciao"}}'
```

## Esempi usando JavaScript (fetch)

```javascript
// Traduzione di oggetti locale
const translateLocales = async () => {
  const response = await fetch(
    "http://localhost:7860/translate?target_language=en",
    {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        locales: {
          welcome: "Benvenuto!",
          save_button: "Salva",
          cancel_button: "Annulla",
        },
      }),
    }
  );

  const data = await response.json();
  console.log(data);
};

// Ottenere lingue supportate
const getSupportedLanguages = async () => {
  const response = await fetch("http://localhost:7860/languages");
  const data = await response.json();
  console.log("Lingue supportate:", data.supported_languages);
};

// Traduzione singola
const translateSingle = async () => {
  const response = await fetch(
    "http://localhost:7860/translate-single?text=Ciao mondo&target_language=en",
    {
      method: "POST",
    }
  );
  const data = await response.json();
  console.log(data);
};
```

## Esempi usando Python (requests)

```python
import requests

# Traduzione di oggetti locale
def translate_locales():
    url = "http://localhost:7860/translate"
    params = {"target_language": "en"}
    data = {
        "locales": {
            "welcome": "Benvenuto!",
            "save_button": "Salva",
            "cancel_button": "Annulla"
        }
    }

    response = requests.post(url, json=data, params=params)
    print(response.json())

# Ottenere lingue supportate
def get_supported_languages():
    response = requests.get("http://localhost:7860/languages")
    print(response.json())

# Traduzione singola
def translate_single():
    url = "http://localhost:7860/translate-single"
    params = {
        "text": "Ciao mondo",
        "target_language": "en"
    }

    response = requests.post(url, params=params)
    print(response.json())
```