Translator: Unleash the Power of Google Translate in Dart
Published on by Flutter News Hub
Harness the boundless capabilities of Google Translate with the Translator package for Dart. Easily translate text across languages, unlocking seamless communication and global reach for your applications.
Installation
Add the Translator package to your Flutter project using pub:
dependencies: translator: ^latest_version
Usage
Translating Text
Translate text using the translate method:
final translator = GoogleTranslator(); final translation = await translator.translate("Hello World!", from: 'en', to: 'es'); print(translation.text); // prints "¡Hola Mundo!"
Alternatively, use the extension method:
print(await "Hello World!".translate(to: 'fr')); // prints "Bonjour le monde !"
Detecting Language
Auto-detect the language of the source text by omitting the from parameter:
print(await "私は元気です".translate(to: 'en')); // prints "I am fine."
Print Translated Text
Simplify your translation workflow with the translateAndPrint method:
translator.translateAndPrint("This is a test", to: 'de'); // prints "Dies ist ein Test."
API
Explore the full API documentation at pub.dev/packages/translator.
Disclaimer
This package is intended for educational purposes and may break in the future as it relies on scraping the Google Translate website. Consider purchasing the Official Google Translate API for commercial use.
Support the Developer
Show your appreciation and support the developer through Ko-fi:
Example Code
Multi-Language Translation:
var translations = await translator.translate("你好", to: ['en', 'fr', 'es']); print(translations[0].text); // prints "Hello" print(translations[1].text); // prints "Bonjour" print(translations[2].text); // prints "Hola"
Translation with Options:
Customize the translation parameters:
var translation = await translator.translate("This is a test", to: 'fr', source: 'ko', model: 'nmt'); print(translation.text); // prints "Ceci est un test."
Additional Information
- The Translation object includes additional metadata such as the detected source language, confidence level, and more.
- The supported language codes can be found at ISO 639-1 Codes.