| # 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()) | |
| ``` | |