added chat app

This commit is contained in:
tom.hempel
2025-09-30 18:07:12 +02:00
parent 9f67723754
commit 617ee0757d
178 changed files with 40482 additions and 0 deletions

View File

@ -0,0 +1,50 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
class DeepLService {
Future<String?> translateText({
required String apiKey,
required String text,
required String targetLang,
String sourceLang = 'AUTO',
bool useFreeApi = true,
}) async {
if (apiKey.isEmpty || text.trim().isEmpty) return null;
final base = useFreeApi ? 'https://api-free.deepl.com' : 'https://api.deepl.com';
final uri = Uri.parse('$base/v2/translate');
try {
final response = await http.post(
uri,
headers: {
'Authorization': 'DeepL-Auth-Key $apiKey',
'Content-Type': 'application/x-www-form-urlencoded',
},
body: {
'text': text,
'target_lang': targetLang,
if (sourceLang.isNotEmpty && sourceLang.toUpperCase() != 'AUTO')
'source_lang': sourceLang,
'preserve_formatting': '1',
},
);
if (response.statusCode == 200) {
final jsonBody = json.decode(utf8.decode(response.bodyBytes));
final translations = jsonBody['translations'] as List<dynamic>?;
if (translations != null && translations.isNotEmpty) {
return translations.first['text']?.toString();
}
return null;
} else {
// Swallow errors and return null to avoid blocking the main save flow
return null;
}
} catch (_) {
return null;
}
}
}