A Free and Unlimited Google Translate API for Dart

translator_plus

Free Google Translate API for Dart

See it in pub: https://pub.dev/packages/translator_plus
and GitHub: https://github.com/HusamAwad2001/translator_plus

Usage

void main() async {
  final translator = GoogleTranslator();

  final input = "Здравствуйте. Ты в порядке?";

  translator.translate(input, from: 'ru', to: 'en').then(print);
  // prints Hello. Are you okay?
  
  var translation = await translator.translate("Dart is very cool!", to: 'pl');
  print(translation);
  // prints Dart jest bardzo fajny!

  print(await "example".translate(to: 'pt'));
  // prints exemplo
}

 

Using translate method passing the args from and to designates the language from text you're typing and the language to be translated

translator.translate("I love Brazil!", from: 'en', to: 'pt').then((s) {
    print(s);
  }); 
  // prints Eu amo o Brasil!

 

or you can omit from language and it'll auto-detect the language of source text

translator.translate("Hello", to: 'es').then(print);
// prints Hola

 

and also pass the value to a var using await

var translation = await translator.translate("I would buy a car, if I had money.", from: 'en', to: 'it');
print(translation);
// prints Vorrei comprare una macchina, se avessi i soldi.

 

The returned value is a Translation object which holds the translation stuff

var translation = await translator.translate('Translation', from: 'en', to: 'es');
print('${translation.source} (${translation.sourceLanguage}) == ${translation.text} (${translation.targetLanguage})');

// prints Translation (English) == Traducción (Spanish)

 

You can use the extension method directly on the string too

print(await "example".translate(to: 'pt'));
// prints exemplo

 

There is translateAndPrint method that prints directly

translator.translateAndPrint("This means 'testing' in chinese", to: 'zh-cn');
// prints 这意味着用中文'测试'

 

API

For full API docs take a look at https://pub.dev/documentation/translator_plus/latest/

License

MIT License

Copyright © 2023 Husam Dahliz

Disclaimer

This package is developed for educational purposes only. Do not depend on this package as it may break anytime as it is based on crawling the Google Translate website. Consider buying Official Google Translate API for other types of usage.

Use this package as a library

Depend on it

Run this command:

With Dart:

 $ dart pub add translator_plus

With Flutter:

 $ flutter pub add translator_plus

This will add a line like this to your package's pubspec.yaml (and run an implicit dart pub get):

dependencies:
  translator_plus: ^1.0.1

Alternatively, your editor might support dart pub get or flutter pub get. Check the docs for your editor to learn more.

Import it

Now in your Dart code, you can use:

import 'package:translator_plus/translator_plus.dart'; 

example/main.dart

import 'package:translator_plus/translator_plus.dart';

void main() async {
  final translator = GoogleTranslator();
  final input = "Здравствуйте. Ты в порядке?";

  // Using the Future API
  translator
      .translate(input, to: 'en')
      .then((result) => print("Source: $input\nTranslated: $result"));

  // Passing the translation to a variable
  var translation =
      await translator.translate("I would buy a car, if I had money.", from: 'en', to: 'it');

  // You can also call the extension method directly on the input
  print('Translated: ${await input.translate(to: 'en')}');

  // You can also replace the default base URL for countries where the default one doesn't work
  translator.baseUrl = "translate.google.com.hk";
  translator.translateAndPrint("This means 'testing' in chinese", to: 'zh-cn');
  //prints 这意味着用中文'测试'

  print("translation: $translation");
  // prints translation: Vorrei comprare una macchina, se avessi i soldi.
} 

Download details:

Author: HusamAwad2001

Source: https://github.com/HusamAwad2001/translator_plus

#flutter #android #ios 

A Free and Unlimited Google Translate API for Dart
1.45 GEEK