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 the from or to 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