Hereβs your README.md
styled properly in markdown for use on GitHub or pub.dev, with headings, code formatting, emojis, and clear sections:
# π¦ simple_translator
A simple, **unofficial Google Translate API client for Dart**. This package provides a straightforward way to translate text between various languages using Google's translation service. It's suitable for both pure Dart and Flutter projects.
> β οΈ **Disclaimer**: This package uses an *unofficial* API. While it works reliably at the time of development, its functionality might be affected by future changes to Googleβs underlying translation service.
---
## β¨ Features
- π€ **Text Translation**: Translate text from a source language to a target language.
- π **Automatic Language Detection**: Automatically detects the source language if not specified.
- π¦ **String Extension**: Use `.translate()` directly on `String` values.
- π **Token Generation**: Handles required token generation internally.
- π **Language List**: Check supported languages easily.
- π¨ **Error Handling**: Throws meaningful exceptions for unsupported languages or network issues.
---
## π Installation
Add `simple_translator` and `http` to your `pubspec.yaml`:
```yaml
dependencies:
simple_translator: ^latest_version # Check pub.dev for latest version
http: ^0.13.0
Then run:
dart pub get
# or
flutter pub get
π Usage
β Basic Translation
import 'package:simple_translator/simple_translator.dart';
void main() async {
final translator = GoogleTranslator();
try {
var translation = await translator.translate(
"Hello world",
from: 'en',
to: 'es',
);
print('Original Text: ${translation.source}');
print('Translated Text: ${translation.text}');
print('Source Language: ${translation.sourceLanguage.name}');
print('Target Language: ${translation.targetLanguage.name}');
} catch (e) {
print('Translation error: $e');
}
}
π€ Automatic Language Detection
void main() async {
final translator = GoogleTranslator();
try {
var autoTranslation = await translator.translate(
"Bonjour le monde", // French
to: 'de', // Translate to German
);
print('Original Text: ${autoTranslation.source}');
print('Translated Text: ${autoTranslation.text}');
print('Detected Language: ${autoTranslation.sourceLanguage.name}');
print('Target Language: ${autoTranslation.targetLanguage.name}');
} catch (e) {
print('Translation error: $e');
}
}
π§© Using String Extension
import 'package:simple_translator/simple_translator.dart';
void main() async {
try {
var english = await "μλ
νμΈμ".translate(to: 'en');
print(english.text); // Hello
var french = await "How are you?".translate(to: 'fr');
print(french.text); // Comment allez-vous ?
} catch (e) {
print('Translation error: $e');
}
}
π Supported Languages
import 'package:simple_translator/simple_translator.dart';
void main() {
print(LanguageList.contains('es')); // true
print(LanguageList.contains('Spanish')); // true
print(LanguageList.contains('xyz')); // false
final english = LanguageList()['en'];
print(english.name); // English
print(english.code); // en
try {
LanguageList()['invalid_code'];
} on LanguageNotSupportedException catch (e) {
print(e.msg); // invalid_code is not a supported language.
}
}
βοΈ Client Type
You can change the client type for translation:
final translator = GoogleTranslator(client: ClientType.extensionGT);
ClientType.siteGT
is more stable.ClientType.extensionGT
may be blocked more frequently.
β οΈ Error Handling
The translate
method may throw:
LanguageNotSupportedException
: If thefrom
orto
language code is not recognized.http.ClientException
: For network issues or unofficial API errors.
Always use try-catch when calling translate()
.
π€ Author
Md. Rahul Reza π rahulreza.com π§ contact@rahulreza.com
π License
This project is licensed under the MIT License.
---
Libraries
- model/translation
- simple_translator
- To see a full list of supported languages, refer to the LanguageList class.
- token/google_token_gen
- token/token_provider_interface