google-translator for Dart

repository·master·Indexed 41 days ago

https://github.com/gabrielpacheco23/google-translator

A free Google Translate API wrapper for Dart that allows developers to translate strings using the GoogleTranslator class or String extensions. It supports automatic language detection, custom base URLs, and provides a LanguageList to verify supported ISO codes. Note: This package is for educational purposes and relies on web crawling rather than an official API.

Tokens
3.2K
Snippets
12
Records
13
Agent score
51%

What's inside google-translator

  1. Translate text using GoogleTranslator

    master

    To translate text, instantiate GoogleTranslator and use the translate method. You can specify the source language using the from parameter and the target language using the to parameter. If you omit the from parameter, the library will attempt to auto-detect the source language.

    The translate method returns a Translation object containing the translated text and metadata about the source and target languages.

    final translator = GoogleTranslator();
    
    // Explicitly defining source and target languages
    final translation = await translator.translate("I love Brazil!", from: 'en', to: 'pt');
    print(translation.text); // Eu amo o Brasil!
    
    // Auto-detecting source language
    final autoDetected = await translator.translate("Hello", to: 'es');
    print(autoDetected.text); // Hola
  2. Important Disclaimer regarding reliability

    master

    Warning

    This package is developed for educational purposes only. It works by crawling the Google Translate website and is not an official API. Because it relies on web crawling, it may break at any time. For production or critical usage, it is recommended to use the official Google Translate API.

  3. Print translations directly with translateAndPrint

    master

    If you want to output the translation directly to the console without handling the Translation object, use the translateAndPrint method.

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

    master

    The translate method returns a Translation object. This object provides access to the original text, the detected source language, the translated text, and the target language.

    Key properties:

    • source: The original text.
    • sourceLanguage: The language of the original text.
    • text: The translated text.
    • targetLanguage: The language the text was translated into.
    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)
  5. Check if a language is supported

    master

    Use LanguageList.contains(String codeOrLang) to verify if a specific ISO code or a language name is supported by the API. This method is case-insensitive regarding the name (it performs a camel-case check internally).

    // Checking by ISO code
    bool isSupportedCode = LanguageList.contains('en'); // true
    
    // Checking by language name
    bool isSupportedName = LanguageList.contains('English'); // true
  6. Configure the GoogleTranslator client type

    master

    When initializing GoogleTranslator, you can specify a client type via the ClientType enum to change how the request is sent to Google. This is useful if your IP is being blocked by the default client.

    • ClientType.siteGT: Uses the 't' client parameter (default).
    • ClientType.extensionGT: Uses the 'gtx' client parameter. This is intended for cases where the standard client might be blocking your IP.
    // Use the extension client to avoid IP blocking
    final translator = GoogleTranslator(client: ClientType.extensionGT);
  7. Access supported languages via LanguageList

    master

    The LanguageList class provides access to all languages supported by the Google Translate API. You can retrieve a Language object using its ISO code via the [] operator. If the code is not supported, it throws a LanguageNotSupportedException.

    // Accessing a language by its ISO code
    final spanish = LanguageList['es'];
    print(spanish.name); // Spanish
    
    // Automatic language detection code
    final auto = LanguageList['auto'];
    print(auto.name); // Automatic
  8. Translate text with GoogleTranslator.translate()

    master

    Use the translate method to convert text from one language to another. It returns a Future<Translation> object containing the translated text and metadata about the source and target languages.

    Parameters:

    • sourceText (String): The text you want to translate.
    • from (String, default: 'auto'): The source language code. Use 'auto' to attempt automatic language detection.
    • to (String, default: 'en'): The target language code.

    Exceptions:

    • Throws LanguageNotSupportedException if the provided from or to language codes are not recognized by the library.
    • Throws http.ClientException if the network request fails or returns a non-200 status code.
    final translator = GoogleTranslator();
    
    try {
      final Translation translation = await translator.translate(
        'Hello world', 
        from: 'en', 
        to: 'es'
      );
      print(translation.translatedText);
    } catch (e) {
      print('Translation failed: $e');
    }
  9. Reference: Supported language codes and names

    master

    The following mapping represents the languages supported by the Google Translate API within this package. Use these keys with LanguageList[code] to retrieve Language objects.

    'auto': 'Automatic',
    'af': 'Afrikaans',
    'sq': 'Albanian',
    'am': 'Amharic',
    'ar': 'Arabic',
    'hy': 'Armenian',
    'as': 'Assamese',
    'ay': 'Aymara',
    'az': 'Azerbaijani',
    'bm': 'Bambara',
    'eu': 'Basque',
    'be': 'Belarusian',
    'bn': 'Bengali',
    'bho': 'Bhojpuri',
    'bs': 'Bosnian',
    'bg': 'Bulgarian',
    'ca': 'Catalan',
    'ceb': 'Cebuano',
    'zh-cn': 'Chinese (Simplified)',
    'zh-tw': 'Chinese (Traditional)',
    'co': 'Corsican',
    'hr': 'Croatian',
    'cs': 'Czech',
    'da': 'Danish',
    'dv': 'Dhivehi',
    'doi': 'Dogri',
    'nl': 'Dutch',
    'en': 'English',
    'eo': 'Esperanto',
    'et': 'Estonian',
    'ee': 'Ewe',
    'fil': 'Filipino (Tagalog)',
    'fi': 'Finnish',
    'fr': 'French',
    'fy': 'Frisian',
    'gaa': 'Ga',
    'gl': 'Galician',
    'ka': 'Georgian',
    'de': 'German',
    'el': 'Greek',
    'gn': 'Guarani',
    'gu': 'Gujarati',
    'ht': 'Haitian Creole',
    'ha': 'Hausa',
    'haw': 'Hawaiian',
    'iw': 'Hebrew',
    'he': 'Hebrew (standard)',
    'hi': 'Hindi',
    'hmn': 'Hmong',
    'hu': 'Hungarian',
    'is': 'Icelandic',
    'ig': 'Igbo',
    'ilo': 'Ilocano',
    'id': 'Indonesian',
    'ga': 'Irish',
    'it': 'Italian',
    'ja': 'Japanese',
    'jv': 'Javanese',
    'kn': 'Kannada',
    'kk': 'Kazakh',
    'km': 'Khmer',
    'rw': 'Kinyarwanda',
    'gom': 'Konkani',
    'ko': 'Korean',
    'kri': 'Krio',
    'ku': 'Kurdish (Kurmanji)',
    'ckb': 'Kurdish (Sorani)',
    'ky': 'Kyrgyz',
    'lo': 'Lao',
    'la': 'Latin',
    'lv': 'Latvian',
    'ln': 'Lingala',
    'lt': 'Lithuanian',
    'lg': 'Luganda',
    'lb': 'Luxembourgish',
    'mk': 'Macedonian',
    'mai': 'Maithili',
    'mg': 'Malagasy',
    'ms': 'Malay',
    'ml': 'Malayalam',
    'mt': 'Maltese',
    'mi': 'Maori',
    'mr': 'Marathi',
    'mni-mtei': 'Meiteilon (Manipuri)',
    'lus': 'Mizo',
    'mn': 'Mongolian',
    'my': 'Myanmar (Burmese)',
    'ne': 'Nepali',
    'no': 'Norwegian',
    'ny': 'Nyanja (Chichewa)',
    'or': 'Odia (Oriya)',
    'om': 'Oromo',
    'ps': 'Pashto',
    'fa': 'Persian',
    'pl': 'Polish',
    'pt': 'Portuguese',
    'pa': 'Punjabi',
    'qu': 'Quechua',
    'ro': 'Romanian',
    'ru': 'Russian',
    'sm': 'Samoan',
    'sa': 'Sanskrit',
    'gd': 'Scots Gaelic',
    'nso': 'Sepedi',
    'sr': 'Serbian',
    'st': 'Sesotho',
    'sn': 'Shona',
    'sd': 'Sindhi',
    'si': 'Sinhala',
    'sk': 'Slovak',
    'sl': 'Slovenian',
    'so': 'Somali',
    'es': 'Spanish',
    'su': 'Sundanese',
    'sw': 'Swahili',
    'sv': 'Swedish',
    'tl': 'Tagalog (Filipino)',
    'tg': 'Tajik',
    'ta': 'Tamil',
    'tt': 'Tatar',
    'te': 'Telugu',
    'th': 'Thai',
    'ti': 'Tigrinya',
    'ts': 'Tsonga',
    'tr': 'Turkish',
    'tk': 'Turkmen',
    'ak': 'Twi (Akan)',
    'uk': 'Ukrainian',
    'ur': 'Urdu',
    'ug': 'Uyghur',
    'uz': 'Uzbek',
    'vi': 'Vietnamese',
    'cy': 'Welsh',
    'xh': 'Xhosa',
    'yi': 'Yiddish',
    'yo': 'Yoruba',
    'zu': 'Zulu',