simple_aws_translate 0.0.7
simple_aws_translate: ^0.0.7 copied to clipboard
A simple implementation of the AWS Translation plugin for testing purposes only. NOT for production use as it requires embedding AWS credentials. Developed by Afandi Yusuf.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:simple_aws_translate/simple_aws_translate.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final TextEditingController textController = TextEditingController();
String resultText = "";
String sourceLanguage = "auto";
String targetLanguage = "en";
bool isTranslating = false;
String? errorMessage;
// Available language codes
final List<Map<String, String>> languages = [
{"code": "auto", "name": "Auto-detect"},
{"code": "en", "name": "English"},
{"code": "es", "name": "Spanish"},
{"code": "fr", "name": "French"},
{"code": "de", "name": "German"},
{"code": "it", "name": "Italian"},
{"code": "pt", "name": "Portuguese"},
{"code": "ja", "name": "Japanese"},
{"code": "ko", "name": "Korean"},
{"code": "zh", "name": "Chinese"},
{"code": "ru", "name": "Russian"},
{"code": "ar", "name": "Arabic"},
{"code": "id", "name": "Indonesian"},
];
@override
void initState() {
super.initState();
initTranslateService();
}
// Initialize the AWS Translate service
Future<void> initTranslateService() async {
// IMPORTANT: Replace these with your actual AWS credentials
// For production, use secure storage or environment variables
SimpleAwsTranslate.instance.init(
accessKey: "your_access_key_here",
secretKey: "your_secret_key_here",
region: AwsRegion.apSoutheast1, // Default Singapore region
);
}
// Translate the input text
Future<void> translateText() async {
final text = textController.text;
if (text.isEmpty) {
setState(() {
errorMessage = "Please enter text to translate";
});
return;
}
setState(() {
isTranslating = true;
resultText = "Translating...";
errorMessage = null;
});
try {
final result = await SimpleAwsTranslate.instance.translateText(
text,
from: sourceLanguage,
to: targetLanguage,
);
setState(() {
resultText = result;
isTranslating = false;
});
} on AwsTranslationException catch (e) {
setState(() {
resultText = "";
errorMessage = "Translation error: ${e.message}";
isTranslating = false;
});
} catch (e) {
setState(() {
resultText = "";
errorMessage = "Unexpected error: $e";
isTranslating = false;
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
),
home: Scaffold(
appBar: AppBar(
title: const Text('AWS Translate Example'),
elevation: 2,
),
// Warning banner for testing only
bottomNavigationBar: Container(
color: Colors.red.shade700,
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 16),
child: const Text(
'⚠️ FOR TESTING PURPOSES ONLY - DO NOT USE IN PRODUCTION ⚠️',
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Source language dropdown
Row(
children: [
const Text('From: ', style: TextStyle(fontWeight: FontWeight.bold)),
const SizedBox(width: 8),
Expanded(
child: DropdownButton<String>(
isExpanded: true,
value: sourceLanguage,
onChanged: (value) {
setState(() {
sourceLanguage = value!;
});
},
items: languages.map((lang) => DropdownMenuItem<String>(
value: lang["code"],
child: Text(lang["name"]!),
)).toList(),
),
),
],
),
// Target language dropdown
Row(
children: [
const Text('To: ', style: TextStyle(fontWeight: FontWeight.bold)),
const SizedBox(width: 8),
Expanded(
child: DropdownButton<String>(
isExpanded: true,
value: targetLanguage,
onChanged: (value) {
setState(() {
targetLanguage = value!;
});
},
items: languages.where((lang) => lang["code"] != "auto").map((lang) => DropdownMenuItem<String>(
value: lang["code"],
child: Text(lang["name"]!),
)).toList(),
),
),
],
),
const SizedBox(height: 16),
// Input text field
TextField(
controller: textController,
decoration: const InputDecoration(
labelText: "Enter text to translate",
border: OutlineInputBorder(),
),
maxLines: 4,
),
const SizedBox(height: 16),
// Translate button
ElevatedButton(
onPressed: isTranslating ? null : translateText,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Text(
isTranslating ? "Translating..." : "Translate",
style: const TextStyle(fontSize: 16),
),
),
),
const SizedBox(height: 24),
// Error message
if (errorMessage != null)
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.red.shade100,
borderRadius: BorderRadius.circular(4),
),
child: Text(
errorMessage!,
style: TextStyle(color: Colors.red.shade900),
),
),
// Result section
if (resultText.isNotEmpty && errorMessage == null) ...[
const SizedBox(height: 16),
const Text(
"Translation Result:",
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.grey.shade100,
borderRadius: BorderRadius.circular(4),
border: Border.all(color: Colors.grey.shade300),
),
child: Text(
resultText,
style: const TextStyle(fontSize: 16),
),
),
],
],
),
),
),
);
}
}