Reputation: 333
I am currently working on a project which involves translation, I am building an app using the framework Flutter and the language Dart.
The problem is that I can't figure out how to translate text with my app in dart.
I tried using all sorts of platforms like google translate API, Firebase, Yandex... But I don't have any money which is kind of a problem.
So I tried this https://learn.microsoft.com/fr-fr/azure//cognitive-services/translator/reference/v3-0-translate#request-url but I got this error message {"error":{"code":405000,"message":"The request method is not supported for the requested resource."}}
.
Then I tried using a dart package: https://pub.dev/packages/localize_and_translate, it worked, but it's to translate text that you already have in json files and I need to translate text that users will give me.
I also tried to get what was inside one of the google translate requests, to get an answer without having to manually go to the website, when I tried to analyse the request I only got this link : https://fonts.googleapis.com/css?lang=fr&family=Product+Sans%7CRoboto:400,700
.
Some help or ideas would definitely be very much appreciated.
Thanks for everything !
Upvotes: 1
Views: 6462
Reputation: 12335
One more package, uses Google translation, we use it in production, works well. https://pub.dev/packages/google_cloud_translation
class _MyHomePageState extends State<MyHomePage> {
late Translation _translation;
final String _text =
'Toda persona tiene derecho a la educación. La educación debe ser gratuita, al menos en lo concerniente a la instrucción elemental y fundamental. La instrucción elemental será obligatoria. La instrucción técnica y profesional habrá de ser generalizada; el acceso a los estudios superiores será igual para todos, en función de los méritos respectivos.';
TranslationModel _translated = TranslationModel(translatedText: '', detectedSourceLanguage: '');
@override
void initState() {
_translation = Translation(
apiKey: 'YOUR_API_KEY',
);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Translate demo'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Initial text',
style: Theme.of(context).textTheme.headline3,
),
Text(_text),
SizedBox(height: 30),
Text('Translated text', style: Theme.of(context).textTheme.headline3),
Text(_translated.translatedText, style: TextStyle(color: Colors.blueAccent)),
Text('Detected language - ${_translated.detectedSourceLanguage}',
style: TextStyle(color: Colors.red)),
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
_translated = await _translation.translate(text: _text, to: 'en');
setState(() {});
},
tooltip: 'Translate',
child: Icon(Icons.language),
),
);
}
}
Upvotes: 0
Reputation: 22477
You can use AWS Translate(One year free), which I have created an plugin for my production used apps. Earlier I used Android native and IOS native AWS official packages with Flutter MethodChannel
and later I decided to make it package so that others like you can get help from.
Here is that package: https://pub.dev/packages/aws_translate
Read more about AWS translate: https://aws.amazon.com/translate/
Here is a simple example of how to use it:
AwsTranslate awsTranslate = AwsTranslate(
poolId: poolId, // your pool id here
region: Regions.AP_NORTHEAST_2); // your region here
// from parameter is default to ``auto``
String textToTranslate = 'If you press on this text, I can translate this text for you.';
String translated = await awsTranslate.translateText(textToTranslate, to: 'es');
if (!mounted) return;
print(textToTranslate);
setState(() => textToTranslate = translated);
Example app for my AWS translate plugin can be found here: https://github.com/Blasanka/aws_translate_plugin/tree/master/example (You must have AWS account to get pool id and region).
If you just want to try out google translate(without any login but you cannot use in production apps), try https://pub.dev/packages/translator
Working example for translator can be found here: https://github.com/gabrielpacheco23/google-translator/blob/master/example/main.dart
Upvotes: 1